CSS properties allow authors to specify the foreground color and
background of an element. Backgrounds may be colors or
images. Background properties allow authors to position a background image,
repeat it, and declare whether it should be fixed with respect
to the viewport or scrolled
along with the document.
See the section on color units
for the syntax of valid color values.
Authors may specify the background of an element (i.e., its
rendering surface) as either a color or an image. In terms of the box model, "background" refers to
the background of the content, padding and border areas. Border colors
and styles are set with the border properties. Margins
are always transparent.
Background properties are not inherited, but the parent box's
background will shine through by default because of the initial
'transparent' value on 'background-color'.
The background of the root element becomes the background of the canvas and covers the entire canvas, anchored at the same point as it would be if it was painted only for the root element itself. The root element does not paint this background again.
For HTML documents, however, we recommend that authors specify the
background for the BODY element rather than the HTML element.
For HTML documents whose root HTML element has computed values of
'transparent' for 'background-color' and 'none'
for 'background-image',
user agents must instead use the computed value of those properties
from that HTML element's first BODY element child when painting
backgrounds for the canvas, and must not paint a background for that
BODY element.
This does not apply to XHTML documents.
According to these rules, the canvas underlying the following
HTML document will have a "marble" background:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<TITLE>Setting the canvas background</TITLE>
<STYLE type="text/css">
BODY { background: url("http://example.com/marble.png") }
</STYLE>
<P>My background is marble.
Note that the rule for the BODY element will work even though the BODY tag has been omitted in the HTML source since the HTML parser will infer the missing tag.
Backgrounds of elements that form a stacking context (see the
'z-index' property) are painted at the bottom of the element's
stacking context, below anything in that stacking context.
This property sets the background color of an element, either a
<color> value or the keyword
'transparent', to make the underlying colors shine through.
This property sets the background image of an element. When
setting a background image, authors should also specify a background
color that will be used when the image is unavailable. When the image
is available, it is rendered on top of the background color. (Thus,
the color is visible in the transparent parts of the image).
Values for this property are either <uri>, to specify the
image, or 'none', when no image is used.
Example(s):
body { background-image: url("marble.png") }
p { background-image: none }
If a background image is specified, this property specifies whether
the image is repeated (tiled), and how. All tiling covers the content, padding and border areas of a box.
The tiling and positioning of the
background-image on inline elements is undefined in this specification.
A future level of CSS may define the tiling and positioning of the
background-image on inline elements.
Values have the following meanings:
repeat
The image is repeated both horizontally and vertically.
repeat-x
The image is repeated horizontally only.
repeat-y
The image is repeated vertically only.
no-repeat
The image is not repeated: only one copy of the image is drawn.
Example(s):
body {
background: white url("pendant.png");
background-repeat: repeat-y;
background-position: center;
}
If a background image is specified, this property specifies
whether it is fixed with regard to the viewport ('fixed') or scrolls along
with the containing block ('scroll').
Note that there is only one viewport per view. If an element has
a scrolling mechanism (see 'overflow'), a 'fixed' background doesn't
move with the element, and a 'scroll' background doesn't move with
the scrolling mechanism.
Even if the image is fixed, it is still only visible when it is in
the content, padding or border area of the element. Thus, unless
the image is tiled ('background-repeat: repeat'), it may be invisible.
Example(s):
This example creates an infinite vertical band
that remains "glued" to the viewport when the element is scrolled.
body {
background: red url("pendant.png");
background-repeat: repeat-y;
background-attachment: fixed;
}
User agents that do not support 'fixed' backgrounds (for example due
to limitations of the hardware platform) should ignore declarations
with the keyword 'fixed'. For example:
body {
background: white url(paper.png) scroll; /* for all UAs */
background: white url(ledger.png) fixed; /* for UAs that do fixed backgrounds */
}
With a value pair of '0% 0%', the upper left corner of the image
is aligned with the upper left corner of the box's padding edge. A value pair of
'100% 100%' places the lower right corner of the image in the lower
right corner of padding area. With a value pair of '14% 84%', the point
14% across and 84% down the image is to be placed at the point 14%
across and 84% down the padding area.
With a value pair of '2cm 1cm', the upper left corner of the image
is placed 2cm to the right and 1cm below the upper left corner of the
padding area.
top left and left top
Same as '0% 0%'.
top,
top center, and
center top
Same as '50% 0%'.
right top and top right
Same as '100% 0%'.
left, left center,
and center left
Same as '0% 50%'.
center and center center
Same as '50% 50%'.
right, right center,
and center right
Same as '100% 50%'.
bottom left and left bottom
Same as '0% 100%'.
bottom,
bottom center, and
center bottom
Same as '50% 100%'.
bottom right
and right bottom
Same as '100% 100%'.
If only one percentage or length value is given, it sets the
horizontal position only, and the vertical position will be 50%. If two
values are given, the horizontal position comes first. Combinations of
keyword, length and percentage values are allowed, (e.g., '50% 2cm' or 'center 2cm' or 'center 10%'). For combinations of keyword and non-keyword values, 'left' and 'right' may only be used as the first value, and 'top' and 'bottom' may only be used as the second value. Negative positions are allowed.
Example(s):
body { background: url("banner.jpeg") right top } /* 100% 0% */
body { background: url("banner.jpeg") top center } /* 50% 0% */
body { background: url("banner.jpeg") center } /* 50% 50% */
body { background: url("banner.jpeg") bottom } /* 50% 100% */
The tiling and positioning of the
background-image on inline elements is undefined in this specification.
A future level of CSS may define the tiling and positioning of the
background-image on inline elements.
If the background image is fixed within the viewport (see the
'background-attachment'
property), the image is placed relative to the viewport instead of the
element's padding area. For example,
Given a valid declaration,
the 'background' property
first sets all the individual background properties to their initial
values, then assigns explicit values given in the declaration.
Example(s):
In the first rule of the following example, only a value for 'background-color' has been
given and the other individual properties are set to their initial
value. In the second rule, all individual properties have been
specified.
BODY { background: red }
P { background: url("chess.png") gray 50% repeat fixed }
For information about gamma issues, please consult the
Gamma Tutorial in the PNG specification ([PNG10]).
Note.
In the computation of gamma correction, UAs displaying on a CRT
may assume an ideal CRT and ignore any effects on apparent gamma
caused by dithering. That means the minimal handling they need to do
on current platforms is:
PC using MS-Windows
none
Unix using X11
none
Mac using QuickDraw
apply gamma 1.45 [ICC32] (ColorSync-savvy applications
may simply pass the sRGB ICC profile to ColorSync
to perform correct color correction)
SGI using X
apply the gamma value from
/etc/config/system.glGammaVal (the default value being
1.70; applications running on Irix 6.2 or above may simply pass
the sRGB ICC profile to the color management system)
NeXT using NeXTStep
apply gamma 2.22
"Applying gamma" means that each of the three R, G and B must be
converted to R'=Rgamma, G'=Ggamma,
B'=Bgamma, before being handed to the OS.
This may be done rapidly by building a 256-element lookup table
once per browser invocation thus:
for i := 0 to 255 do
raw := i / 255.0;
corr := pow (raw, gamma);
table[i] := trunc (0.5 + corr * 255.0)
end
which then avoids any need to do transcendental math per color
attribute, far less per pixel.