13 Colors and Backgrounds

Contents

  1. Foreground color: the 'color' property
  2. The background
    1. Background properties: 'background-color', 'background-image', 'background-repeat', 'background-attachment', 'background-position', and 'background'

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 the 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 legal color values.

13.1 Foreground color: the 'color' property

'color'

Property name:  'color'
Value:  <color> | inherit
Initial:  depends on user agent
Applies to:  all elements
Inherited:  yes
Percentage values:  N/A
Media groups:  visual

This property describes the foreground color of an element's text content. There are different ways to specify red:

  EM { color: red }              /* predefined color name */
  EM { color: rgb(255,0,0) }     /* RGB range 0-255   */

13.2 The background

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 and the padding area. Border colors and styles are set with the border properties. Margins are always transparent so the background of the parent box always shines through.

Background properties do not inherit, but the parent box's background will shine through by default because of the initial 'transparent' value on 'background-color'.

The background of the box generated by the root element covers the entire canvas.

For HTML documents, however, we recommend that authors specify the background for the BODY element rather than the HTML element. User agents should observe the following precedence rules to fill in the background: if the value of the 'background' property for the HTML element is different from 'transparent' then use it, else use the value of the 'background' property for the BODY element. If the resulting value is 'transparent', the rendering is undefined.

According to these rules, the canvas underlying the following HTML document will have a "marble" background and the background of the box generated by the BODY element will be red:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <HTML>
  <HEAD>
  <STYLE type="text/css">
     { background: url(http://style.com/marble.png) }
  </STYLE>
  <TITLE>Setting the canvas background</TITLE>
  </HEAD>
  <BODY style="background: red">
  <P>My background is red.
  </BODY>
  </HTML>

Note. The document language may not define an element that gives direct access to the canvas.

13.2.1 Background properties: 'background-color', 'background-image', 'background-repeat', 'background-attachment', 'background-position', and 'background'

'background-color'

Property name:  'background-color'
Value:  <color> | transparent | inherit
Initial:  transparent
Applies to:  all elements
Inherited:  no
Percentage values:  N/A
Media groups:  visual

This property sets the background color of an element. Values have the following meanings:

<color>
Specifies a color name or value.
transparent
The background is transparent, so all underlying colors shine through.

  H1 { background-color: #F00 }

'background-image'

Property name:  'background-image'
Value:  <uri> | none | inherit
Initial:  none
Applies to:  all elements
Inherited:  no
Percentage values:  N/A
Media groups:  visual

This property sets the background image of an element to be an image. 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.

Values have the following meanings:

<uri>
Specifications the location of the image.
none
No background image is used.

  BODY { background-image: url(marble.gif) }
  P { background-image: none }

'background-repeat'

Property name:  'background-repeat'
Value:  repeat | repeat-x | repeat-y | no-repeat | inherit
Initial:  repeat
Applies to:  all elements
Inherited:  no
Percentage values:  N/A
Media groups:  visual

If a background image is specified, this property specifies whether the image is repeated, and how. 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.

  BODY { 
    background: red url(pendant.gif);
    background-repeat: repeat-y;
  }

In the example above, the image will only be repeated vertically.

'background-attachment'

Property name:  'background-attachment'
Value:  scroll | fixed | inherit
Initial:  scroll
Applies to:  all elements
Inherited:  no
Percentage values:  N/A
Media groups:  visual

If a background image is specified, this property specifies whether it is fixed with regard to the viewport or scrolls along with the document. Values have the following meanings:

fixed
The image is fixed with respect to the viewport.
scroll
The image is scrolled along with the document.

  BODY { 
    background: red url(pendant.gif);
    background-repeat: repeat-y;
    background-attachment: fixed;
  }

User agents may treat 'fixed' as 'scroll'. However, it is recommended they interpret 'fixed' correctly, at least for the HTML and BODY elements, since there is no way for an author to provide an image only for those browsers that support 'fixed'. See the section on conformance for details.

'background-position'

Property name:  'background-position'
Value:  [ [<percentage> | <length> ]{1,2} | [top | center | bottom] || [left | center | right] ] | inherit
Initial:  0% 0%
Applies to:  block-level and replaced elements
Inherited:  no
Percentage values:  refer to the size of the element itself
Media groups:  visual

If a background image has been specified, this property specifies its initial position. Values have the following meanings:

<percentage> <percentage>
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 content edge. A value pair of '100% 100%' places the lower right corner of the image in the lower right corner of the box. 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 box.
<length> <length>
With a value pair of '2cm 2cm', the upper left corner of the image is placed 2cm to the right and 2cm below the upper left corner of the box.
'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, the vertical position will be 50%. If two values are given, the horizontal position comes first. Combinations of length and percentage values are allowed, (e.g., '50% 2cm'). Negative positions are allowed. Keywords cannot be combined with percentage values or length values (all possible combinations are given above).

Examples:

  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% */

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 box. For example,

  BODY { 
    background-image: url(logo.png);
    background-attachment: fixed;
    background-position: 100% 100%;
  } 

In the example above, the image is placed in the lower-right corner of the viewport.

'background'

Property name:  'background'
Value:  [<'background-color'> || <'background-image'> || <'background-repeat'> || <'background-attachment'> || <'background-position'>] | inherit
Initial:  not defined for shorthand properties
Applies to:  all elements
Inherited:  no
Percentage values:  allowed on 'background-position'
Media groups:  visual

The 'background' property is a shorthand property for setting the individual background properties (i.e., 'background-color', 'background-image', 'background-repeat', 'background-attachment' and 'background-position') at the same place in the style sheet.

The 'background' property always sets all the individual background properties. The 'background' property helps authors remember to specify all aspects of a background which they might otherwise neglect by using the individual background properties.

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 }