background-position property
Specifies the position of the background of the object.
![]() ![]() |
Syntax
background-position: [ [ percentage | length | left | center | right ] [ percentage | length | top | center | bottom ]? ] | [ [ left | center | right ] || [ top | center | bottom ] ]
Property values
One or two of the following values.
percentage-
Integer, followed by a percent sign (%). The value is a percentage of the width or height of the object.
length-
Floating-point number, followed by a supported length unit.
Horizontal alignment value.
left-
Horizontal alignment is to the left.
center-
Horizontal alignment is centered.
right-
Horizontal alignment is to the right.
Vertical alignment value.
top-
Vertical alignment is at the top.
center-
Vertical alignment is centered.
bottom-
Vertical alignment is at the bottom.
CSS information
| Applies To | All elements |
|---|---|
| Media | visual |
| Inherited | no |
| Initial Value | 0% 0% |
Standards information
- CSS Backgrounds and Borders Module Level 3, Section 3.6
- CSS 2.1, Section 5.3.6
Remarks
If only one value is set, that value applies to the horizontal coordinate, and the vertical is set to "50%". If both values are set, the first value applies to the horizontal coordinate and the second value applies to the vertical.
Setting the values to "0% 0%" (initial value) positions the background-image to the upper left corner of the element's content block, which includes the padding.
Setting the background position using pixels positions the upper-left of the image at the specified x and y coordinates within the parent element. As the coordinates increase, the image moves to the right and down the visible area. By contrast, setting the background position with percentages uses a corresponding point on the image. At a position of "50% 50%" the image is effectively centered within the visible area.
This property can be set with the other background properties using the background composite property.
Starting in Windows Internet Explorer 9, the background of an element can have multiple layers. The number of layers is determined by the number of comma-separated values in the background-image property. Each of the images is sized, positioned, and tiled according to the corresponding value in the other background properties (background-attachment, background-clip, background-origin, background-position, background-repeat, and background-size). The first image in the list is the layer closest to the user, the next one is painted behind the first, and so on.
Examples
The following examples use the background-position CSS property and the backgroundPosition scripting property to specify the position of a background image.
This example uses a call to an embedded (global) style sheet to move the sphere.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/background-position.htm
<!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>MyBackgroundPosition</title> <style> .style1 { background-position: top center; } .style2 { background-position: bottom right; } </style> </head> <body onload="oSpan.className='style1'"> <span id="oSpan" onmouseout="this.className='style1'" onmouseover="this.className='style2'" style="font-size: 14pt; width: 250px;"> . . . </span> </body> </html>
This example uses an inline style to move the sphere.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/backgroundPosition.htm
<span onmouseover="this.style.backgroundPosition='bottom right'">...</span>
The following example demonstrates the difference between setting the background position by pixels and setting it by percentages.
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/backgroundPositionX.htm
See also
Send comments about this topic to Microsoft
Build date: 11/29/2012


