- CSS background-position Property
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
- Example
- Example
- Example
- Example
- background-position
- Try it
- Syntax
- Values
- Regarding Percentages
- Formal definition
- Formal syntax
- Examples
- Positioning background images
- HTML
- CSS
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
CSS background-position Property
The background-position property sets the starting position of a background image.
Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.
Default value: | 0% 0% |
---|---|
Inherited: | no |
Animatable: | yes. Read about animatable Try it |
Version: | CSS1 |
JavaScript syntax: | object.style.backgroundPosition=»center» Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS Syntax
Property Values
Value | Description | Demo |
---|---|---|
left top left center left bottom right top right center right bottom center top center center center bottom | If you only specify one keyword, the other value will be «center» | Demo ❯ |
x% y% | The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. Default value is: 0% 0% | Demo ❯ |
xpos ypos | The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
How to position a background-image to be centered at top:
body <
background-image: url(‘w3css.gif’);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
>
Example
How to position a background-image to be bottom right:
body <
background-image: url(‘w3css.gif’);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
>
Example
How to position a background-image using percent:
body <
background-image: url(‘w3css.gif’);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
>
Example
How to position a background-image using pixels:
body <
background-image: url(‘w3css.gif’);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
>
Example
Use different background properties to create a «hero» image:
.hero-image <
background-image: url(«photographer.jpg»); /* The image used */
background-color: #cccccc; /* Used if the image is unavailable */
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
>
background-position
The background-position CSS property sets the initial position for each background image. The position is relative to the position layer set by background-origin .
Try it
Syntax
/* Keyword values */ background-position: top; background-position: bottom; background-position: left; background-position: right; background-position: center; /* values */ background-position: 25% 75%; /* values */ background-position: 0 0; background-position: 1cm 2cm; background-position: 10ch 8em; /* Multiple images */ background-position: 0 0, center; /* Edge offsets values */ background-position: bottom 10px right 20px; background-position: right 3em bottom 10px; background-position: bottom 10px right; background-position: top right 10px; /* Global values */ background-position: inherit; background-position: initial; background-position: revert; background-position: revert-layer; background-position: unset;
The background-position property is specified as one or more values, separated by commas.
Values
1-value syntax: the value may be:
- The keyword value center , which centers the image.
- One of the keyword values top , left , bottom , right . This specifies an edge against which to place the item. The other dimension is then set to 50%, so the item is placed in the middle of the edge specified.
- A or . This specifies the X coordinate relative to the left edge, with the Y coordinate set to 50%.
2-value syntax: one value defines X and the other defines Y. Each value may be:
- One of the keyword values top , left , bottom , right . If left or right are given here, then this defines X and the other given value defines Y. If top or bottom are given, then this defines Y and the other value defines X.
- A or . If the other value is left or right , then this value defines Y, relative to the top edge. If the other value is top or bottom , then this value defines X, relative to the left edge. If both values are or values, then the first defines X and the second Y.
- Note that: If one value is top or bottom , then the other value may not be top or bottom . If one value is left or right , then the other value may not be left or right . This means, e.g., that top top and left right are not valid.
- Order: when pairing keywords, placement is not important as the browser can reorder it, the value top left or left top will yield the same result. With pairing or with a keyword the placement is important, the value defining X should come first followed by Y, the value right 20px is not the same as 20px right the former is valid but the latter is invalid. The value left 20% or 20% bottom is valid as X and Y values are clearly defined and the placement is right.
- The default value is left top or 0% 0% .
3-value syntax: Two values are keyword values, and the third is the offset for the preceding value:
- The first value is one of the keyword values top , left , bottom , right , or center . If left or right are given here, then this defines X. If top or bottom are given, then this defines Y and the other keyword value defines X.
- The or value, if it is the second value, is the offset for the first value. If it is the third value, it is the offset for the second value.
- The single length or percentage value is an offset for the keyword value that precedes it. The combination of one keyword with two or values is not valid.
4-value syntax: The first and third values are keyword value defining X and Y. The second and fourth values are offsets for the preceding X and Y keyword values:
- The first value and third values one of the keyword values top , left , bottom , right . If left or right are given here, then this defines X. If top or bottom are given, then this defines Y and the other keyword value defines X.
- The second and fourth values are or values. The second value is the offset for the first keyword. The fourth value is the offset for the second keyword.
Regarding Percentages
The percentage offset of the given background image’s position is relative to the container. A value of 0% means that the left (or top) edge of the background image is aligned with the corresponding left (or top) edge of the container, or the 0% mark of the image will be on the 0% mark of the container. A value of 100% means that the right (or bottom) edge of the background image is aligned with the right (or bottom) edge of the container, or the 100% mark of the image will be on the 100% mark of the container. Thus a value of 50% horizontally or vertically centers the background image as the 50% of the image will be at the 50% mark of the container. Similarly, background-position: 25% 75% means the spot on the image that is 25% from the left and 75% from the top will be placed at the spot of the container that is 25% from the container’s left and 75% from the container’s top.
Essentially what happens is the background image dimension is subtracted from the corresponding container dimension, and then a percentage of the resulting value is used as the direct offset from the left (or top) edge.
(container width - image width) * (position x%) = (x offset value) (container height - image height) * (position y%) = (y offset value)
Using the X axis for an example, let’s say we have an image that is 300px wide and we are using it in a container that is 100px wide, with background-size set to auto:
100px - 300px = -200px (container & image difference)
So that with position percentages of -25%, 0%, 50%, 100%, 125%, we get these image-to-container edge offset values:
-200px * -25% = 50px -200px * 0% = 0px -200px * 50% = -100px -200px * 100% = -200px -200px * 125% = -250px
So with these resultant values for our example, the left edge of the image is offset from the left edge of the container by:
- + 50px (putting the left image edge in the center of the 100-pixel-wide container)
- 0px (left image edge coincident with the left container edge)
- -100px (left image edge 100px to the left of the container, in this example that means the middle 100px image area is centered in the container)
- -200px (left image edge 200px to the left of the container, in this example that means the right image edge is coincident with the right container edge)
- -250px (left image edge 250px to the left of the container, in this example that puts the right edge of the 300px-wide image in the center of the container)
It’s worth mentioning that if your background-size is equal to the container size for a given axis, then a percentage position for that axis will have no effect because the «container-image difference» will be zero. You will need to offset using absolute values.
Formal definition
- background-position-x : A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
- background-position-y : A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
Formal syntax
background-position =
#
=
[ left | center | right | top | bottom | ] |
[ left | center | right | ] [ top | center | bottom | ] |
[ center | [ left | right ] ? ] && [ center | [ top | bottom ] ? ]
=
|
Examples
Positioning background images
Each of these three examples uses the background property to create a yellow, rectangular element containing a star image. In each example, the star is in a different position. The third example illustrates how to specify positions for two different background images within one element.
HTML
div class="exampleone">Example Onediv> div class="exampletwo">Example Twodiv> div class="examplethree">Example Threediv>
CSS
/* Shared among all s */ div background-color: #ffee99; background-repeat: no-repeat; width: 300px; height: 80px; margin-bottom: 12px; > /* These examples use the `background` shorthand property */ .exampleone background: url("startransparent.gif") #ffee99 2.5cm bottom no-repeat; > .exampletwo background: url("startransparent.gif") #ffee99 left 4em bottom 1em no-repeat; > /* Multiple background images: Each image is matched with the corresponding position, from first specified to last. */ .examplethree background-image: url("startransparent.gif"), url("catfront.png"); background-position: 0px 0px, right 3em bottom 2em; >
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.