CSS background-position Property
Example
How to position a background-image:
body
{
background-image
:
url
(
'smiley.gif'
)
;
background-repeat
:
no-repeat
;
background-attachment
:
fixed
;
background-position
:
center
;
}
Try it »
Tag Definition and Usage Notes
The background-position property sets the starting position of a background image.
NoteFor this to work in Firefox and Opera, background-attachment must be set to "fixed".
| Default value: | 0% 0% |
|---|---|
| Inherited: | no |
| Version: | CSS1 |
| JavaScript syntax: | object object.style.backgroundPosition="center" |
Browser support
The numbers in the table indicate the first browser version that supports this property.
| property | |||||
|---|---|---|---|---|---|
| background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Note: IE8 and earlier browsers do not support multiple background images on a single element.
Attribute values
| Value | Description |
|---|---|
| left top |
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom | 如果仅指定一个关键字,其他值将会是"center" |
| x% y% | The first value is the horizontal position, the second is vertical. Top-left is 0% 0%. Bottom-right is 100% 100%. If only one value is specified, the other will be 50%. Default value: 0% 0% | | xpos ypos | The first value is the horizontal position, the second is vertical. Top-left is 0. Units can be pixels (0px 0px) or any other CSS unit. If only one value is specified, the other will be 50%. You can mix % and positions | | inherit | Specifies that the background-position setting should be inherited from the parent element |
Online example
How to set a page background image
This example demonstrates how to set a background-image on a page.
How to position a background image using %
This example demonstrates how to use % to set an image position on a page.
How to position a background image using pixels
This example demonstrates how to use pixels to set an image position on a page.
Related Articles
CSS Tutorial: CSS Background
CSS Reference Manual: background-image property