The CSS reference
"background-position" - css property (v. css 1)
- Indicates the starting position of a background image.
- You can use a couple of % values or lengths to specify a position location
- A specified position relates to the upper left corner of the box that surrounds the content of the element.
- It is possible to combine a length and a % value to specify a position.
- Alternatively you can use two keywords to specify a position location.
- Initial value: 0% 0%
- The property is not inherited
- HTML usage: block-level and replaced elements
- Note: You cannot combine % or length with a keyword.
-
Browsers that support the background-position - property :
Possible property values:
Values | Comments | CSS |
---|---|---|
percentage | If only one percentage or length value is given, it sets the horizontal position only, the vertical position will be 50%. | 1 |
percentage percentage | The first percentage is for the horizontal position and the last percentage is for the vertical position. | 1 |
length | The length value is for the horizontal position only, the vertical position will be 50%. Length are given in units - ref. unit table belove. | 1 |
length length | The first length is for the horizontal position and the last length is for the vertical position. Length are given in units - ref. unit table belove. | 1 |
percentage length | The percentage is for the horizontal position and the length is for the vertical position. Length are given in units - ref. unit table belove. | 1 |
length percentage | The length is for the horizontal position and the percentage is for the vertical position. Length are given in units - ref. unit table belove. | 1 |
top left | left top |
The horizontal position will be at 0% and the vertical position will be at 0%. (this is the default) | 1 |
top | center top | top center |
The horizontal position will be at 50% and the vertical position will be at 0%. | 1 |
top right | right top |
The horizontal position will be at 100% and the vertical position will be at 0%. | 1 |
left | left center | center left |
The horizontal position will be at 0% and the vertical position will be at 50%. | 1 |
center | center center |
The horizontal position will be at 50% and the vertical position will be at 50%. | 1 |
right | right center | center right |
The horizontal position will be at 100% and the vertical position will be at 50%. | 1 |
left bottom | bottom left |
The horizontal position will be at 0% and the vertical position will be at 100%. | 1 |
bottom | bottom center | center bottom |
The horizontal position will be at 50% and the vertical position will be at 100%. | 1 |
bottom right | right bottom |
The horizontal position will be at 100% and the vertical position will be at 100%. | 1 |
inherit | Will inherit the position from parent specification. This property value is new with CSS 2. | 2 |
Property Value | Comment |
---|---|
50% | If only one percentage or length value is given, it sets the
horizontal position only, the vertical position will be 50%. Example:
|
50% 50% | Same result as above. |
center | Same result as above. |
center center | Same result as above. |
0% | The background image is placed at the
left side of the box that surrounds the content of the element,
the vertical position will still be 50%. Example:
|
left | Same result as above. |
left center | Same result as above. |
center left | Same result as above. |
50% 0% | The background image is placed at the
top of the box that surrounds the content of the element,
the horizontal position will be 50%. |
top | Same result as above. |
top center | Same result as above. |
center top | Same result as above. |
50% 100% | The background image is placed at the
bottom of the box that surrounds the content of the element,
the horizontal position will be 50%. |
bottom | Same result as above. |
bottom center | Same result as above. |
center bottom | Same result as above. |
100% 0% | The background image is placed at the
top of the box that surrounds the content of the element,
the horizontal position will be 100%. |
top right | Same result as above. |
right top | Same result as above. |
10px 20px | The background image is placed at
10px from the left side and 20px from the top of the box
that surrounds the content of the element. |
- The format of a length value is an optional sign character ('+' or '-', with '+' being the default) immediately followed by a number immediately followed by a unit identifier.
Possible unit identifier:
unit identifier | Comments | CSS |
---|---|---|
em | The relative unit 'em' is relative to the font size of the element itself. | 1 |
ex | The relative unit 'ex' is relative to the font size of the element itself. | 1 |
px | Pixels, relative to the viewing device | 1 |
in | inches -> 1 inch is equal to 2.54 centimeters. | 1 |
cm | centimeters. | 1 |
mm | millimeters. | 1 |
pt | points -> the points used by CSS2 are equal to 1/72th of an inch. | 1 |
pc | picas -> 1 pica is equal to 12 points. | 1 |
Property "background-position" example:
<html>
<head>
<style type="text/css">
body { background-image: url(images/Trophy.png);
background-color: #cdcdcd;
background-repeat: no-repeat;
background-position: 50% -50%;}
</style>
</head>
<body>
</body>
</html>
Javascript access:
// To SET values ([o] is the target object)
[o].style.backgroundPosition="background-position value"
// To GET values you must first get the computed style object
// To get that object in IE or Opera:
var cStyle=[o].currentStyle;
// To get that object in Firefox, Chrome or Safari (w3c-type):
var cStyle=window.getComputedStyle([o],null);
// To GET the property values:
var value=cStyle.backgroundPosition;
// IE will return 'undefined'
// Opera will return value pairs in pixel.
// Firefox, Chrome and Safari will return value pairs in %.
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.