XML technologies / CSS / Advanced websites with CSS / CSS property reference / background-position

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

By default, the top-left corner of a background image (defined with background-image) is always aligned flush with the top-left corner of the element to which it is assigned, including padding. The property background-position lets you define a different position for the background image.

Inherited: no

See also: background-image

Values

One position value, or two separated by a space. Positions can be specified in any valid CSS unit, such as pixels, points, millimeters or percentages. Alternatively, use the constants in the following table.

 

Table: Value constants for background-position.

Vertical

Horizontal

top
center
bottom

left
center
right

If only one value is specified for background-position, it controls horizontal positioning; vertical positioning automatically receives the value 50%. With two values, the first describes the horizontal position and the second the vertical position of the background image. Negative values are allowed, but are not useful in practice.

If a single constant is specified, the other dimension is automatically set to center.

Absolute and relative lengths may be combined, but lengths and constants may not.

Default value: 0 0

Compatibility

CSS version: 2

Works in Internet Explorer version 4 and later, Netscape version 6 and later, Opera and Mozilla.

In Internet Explorer 4 for Windows, background-position causes an error when processing the property background-repeat.

Examples

The following style rule centers the background image in body:

body {
  background-position: center;
}

The following style rules align the background image flush with the bottom-right corner of body:

body {
 background-position: 100% 100%;
}
body {
 background-position: bottom right;
}

The following style rule positions the image’s left edge horizontally 20 pixels from the left edge of the element body. Vertically, the background image is centered:

body {
  background-position: 20px;
}

The following style rule centers the background image horizontally and positions its top edge vertically 20 pixels from the top edge of the element body:

body {
 background-position: 50% 20px;
}

The following style rule is not allowed because the declaration of background-position combines a length with a constant:

body {
 background-position: 20px center; /* Das geht nicht! */
}

  

 

 

 

back to top