13 Colors and Backgrounds

Contents

  1. Foreground color: the 'color' property
  2. 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, repeated it, and declare whether it should be fixed or scrolled.

See the section on color units for the syntax of legal color values.

13.1 Foreground color: the 'color' property

The following property specifies the foreground color of an element's content. The 'color' property inherits normally.

'color'

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

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

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

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

Authors may specify the background of an element (i.e., its rendering surface) as either a color or an image.

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

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 element always shines through.

'background-color'

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

This property sets the background color of an element.

  H1 { background-color: #F00 }

'background-image'

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

This property sets the background image of an element, whose location is given by a <url>. When setting a background image, one should also set a background color that will be used when the image is unavailable. When the image is available, it is overlaid on top of the background color.

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

'background-repeat'

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

If a background image is specified, the value of 'background-repeat' determines how/if the image is repeated.

A value of 'repeat' means that the image is repeated both horizontally and vertically. The 'repeat-x' ('repeat-y') value makes the image repeat horizontally (vertically), to create a single band of images from one side to the other. With a value of '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
Initial:scroll
Applies to:all elements
Inherited:no
Percentage values:N/A

If a background image is specified, the value of 'background-attachment' determines if it is fixed with regard to the canvas or if it scrolls along with the content.

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

UAs may treat 'fixed' as 'scroll'. However, it is recommended they interpret 'fixed' correctly, at least on 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]
Initial:0% 0%
Applies to:block-level and replaced elements
Inherited:no
Percentage values:refer to the size of the element itself

If a background image has been specified, the value of 'background-position' specifies its initial position.

With a value pair of '0% 0%', the upper left corner of the image is placed in the upper left corner of the box that surrounds the content of the element (i.e., not the box that surrounds the padding, border or margin). A value pair of '100% 100%' places the lower right corner of the image in the lower right corner of the element. 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 element.

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 element.

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.

One can also use keyword values to indicate the position of the background image. Keywords cannot be combined with percentage values, or length values. The possible combinations of keywords and their interpretations are as follows:

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 with regard to the canvas (see the 'background-attachment' property above), the image is placed relative to the canvas instead of the element. E.g.,

  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 canvas.

'background'

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

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 }