Background-position - CSS Property

This page is also about physical property(ies):
- background-position-x
Sets how the background image is positioned horizontally.
- background-position-y
Defines how the background image is positioned in the vertical direction.
And the logical properties:
- background-position-block
Sets the background positioning in the block direction.
- background-position-inline
Sets the background positioning along the lines.
background-position
background-position-x
background-position-y

Summary of characteristics of the background-position property

Quick description
Defines how the background image is positioned.
Status
Standard
Predefined values
bottom | center | left | right | top
Percentages
Calculated relative to the size of the background area minus the size of the background image.
Initial value
0
Inherited by default
No.
Repeatable list.
Per grammar: serialization in the order of syntax. (1)
W3C Module
CSS Backgrounds and Borders Module
 🡇  
 🡅  
Document status: CR (Candidate Recommendation)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)
(1) Predefined values can be written in any order, but as soon as a numeric value is present, the grammatical order must be followed.
For example, top left is valid, while 0% left is not.

Syntax diagram of background-position.

background-position - Syntax DiagramSyntax diagram of the background-position CSS property, which sets the position of the background image. left left right right center center x | % x | % , , x | % x | % top top bottom bottom center center y | % y | % y | % y | %background-position:;background-position:;
Syntax diagram of the background-position property.
The links in the diagram provide more details about the values.
Download the diagram in SVG.

In the diagram, the bold terms are the predefined words of CSS, the other terms are described below:

  • x and y are numerical values followed by one of CSS dimension units.
  • % are percentages that resolve in one dimension. They are calculated based on the width of the background area minus the image width for the first one, and the height of the background area minus the image height for the second.
  • The first group of values is about horizontal positioning, and the second group is about vertical positioning.
  • The whole syntax can be repeated as many times as there are defined backgrounds, with each occurrence separated by a comma.

Description of the background-position property.

background-position defines how the background is positioned relative to the element. If the background is repeated across the entire surface, the background-position property simply causes an offset of the pattern. It's a shorthand property to replace the properties:

background-position-x
Sets how the background image is positioned horizontally.
background-position-y
Defines how the background image is positioned in the vertical direction.

Two values can therefore be used with background-position, the first for horizontal positioning and the second for vertical positioning. When only one of the two values is specified, the other is assumed to be center.

By default (if background-position is not used) the background is positioned at the top left of the element.

Refer to the page on les arrière-plans for general explanations about backgrounds. And further down on that page for information on precise background positioning.

Values for background-position.

  • background-position: left; background-position: center; background-position: right;

    These three values define the horizontal positioning of the background. Without any additional indication, the vertical positioning will then be set to center.

  • background-position: top; background-position: center; background-position: bottom;

    These three values define the vertical positioning of the background. Without any further indication, the horizontal positioning will be set to center.

  • background-position: right top;

    To specify the background positioning in both directions, you need to provide two values, separated by a space. As long as they are predefined values, they can be entered in any order: in fact, right and left clearly indicate horizontal positioning, while top and bottom correspond to vertical positioning.

  • background-position: right 10px bottom 20%;

    The values we saw earlier can be followed by a positive or negative number, with a unit of measurement. See CSS dimension units or a percentage.
    Positive values move the background toward the center of the element, while negative values move it outward. Check out the explanations further down on this page about precise background positioning.

    As soon as a numeric value is present, the order of entry is important: first horizontal positioning, then vertical positioning.

  • background-position: 10% 30px;

    Two numerical values, positive or negative, with a unit of measurement or two percentages. They define the horizontal and vertical positioning of the background, respectively.

    This syntax is equivalent to left 10% top 30px

  • background-position: right top, center top;

    The comma is used to separate information about different backgrounds. Refer to our explanations on arrière-plans multiples.

  • background-position: initial; (0) background-position: inherit; background-position: revert; background-position: revert-layer; background-position: revert-rule; background-position: unset;

    See the following pages for more details: initial, inherit, revert, revert-layer, revert-rule, unset.

To get into the details.

To be precise, background-position positions the background relative to a reference point, which is itself defined by the background-origin property. By default, this reference point is located in the top-left corner of the element, excluding the border.

The left top values position the background at this reference point, while the right bottom values position it at the symmetrical point, located at the bottom right of the element.

Default reference point
for left and top
background-position property
Default reference point
for right and bottom

The following syntax will position the left side of the background image 10 pixels to the right of the first reference point, and the bottom of the image 20 pixels above the second reference point.

background-position: top 10px bottom 20px;

Things get complicated if these dimensions are given in percentages. You first need to define what the background area is: it's the area between the two reference points.

The horizontal percentage is calculated relative to the width of the background area minus the background image width.
The vertical percentage is calculated relative to the height of the background area minus the background image height.

This complication has advantages: 50% corresponds to a background image centered on the element, which is pretty intuitive. On the other hand, left 100% is equivalent to right.

Animation of the background-position property.

Background positioning properties can be easily animated. The example below is an animation along the horizontal axis (property background-position-x). The impression of a continuous scroll comes from the fact that the element's width (300 pixels) is an exact multiple of the image's width (150 pixels). The effect is enhanced by a linear easing function (animation-timing-function:linear).

Access the background-position property via a program

Change the background position in JavaScript.

Here's how to change the value of background-position. JavaScript accepts two syntaxes:

  • With the typical CSS notation, in kebab-case (a dash to separate the words).
  • And with camel-case notation, more common in JavaScript (a capital letter to separate words).
Javascript
let el = document.getElementById('id'); let value = el.style['background-position']; // or let el = document.getElementById('id'); let value = el.style.backgroundPosition;

Read the background position in Javascript.

This code reads the value of background-position assigned in the HTML code, via the style attribute (or through Javascript code like the one above). But it doesn’t pick up the value of the property if it has been set through a CSS selector.

Javascript
let el = document.getElementById('id'); let value = el.style['background-position']; // or let el = document.getElementById('id'); let value = el.style.backgroundPosition;

Read the computed value of background-position in Javascript.

The computed value is the one that comes from the cascade of inheritances: the value on the element itself, the inherited value, the initial value. The value is returned as two dimensions in pixels, or as two percentages, even if the property was assigned with a predefined term like center, for example.

Javascript
let el = document.getElementById('id'); let value = window.getComputedStyle(el).getPropertyValue('background-position');

Read or write the value of the background-position property with JQuery.

JQuery

// Read the value:
$('#id').prop('background-position');
// Write the value:
$('#id').prop('background-position', 'center top');
$('#id').prop('{background-position': 'center top'});

Read the computed value of the background-position property with JQuery.

The value is returned as either two percentages or two pixel dimensions, the first for horizontal positioning and the second for vertical positioning.

JQuery
let value = $('#id').css('background-position');

Other codes.

The page Javascript and CSS gives you other examples of Javascript and JQuery code.

Test it yourself.

The buttons below apply the entered value to the property background-position and then display either the value as it was applied or the computed value. This second option allows you to see how the value of background-position is stored (serialized). In particular, you can see that predefined values are stored as percentages, and all dimension units are converted to pixels.

Simulator.

This simulator is useful for understanding the logic behind percentage calculations. The image is 150x150 pixels, and on a PC screen, the element is about 750 pixels wide.
If we request a horizontal position at 30%, the calculation is as follows:
30% x (750 - 150) = 180 pixels, which is roughly 24% of the element's width.
Give it a try: you’ll see that the left edge of the image is indeed about 24% on the scale. But in any case, 50% places the image in the middle of the element.

background-position-x :
background-position-y :
Non-repeating background image
Repeating background image

Browsers compatibility with background-position.

No compatibility issues to report: all browsers properly support the background-position property.

Column 1
Support for the background-position property to position the background.
Column 2
Correct handling of the background-position syntax to position the background relative to one or two of the sides of the element.
Example: background-position:right 10px bottom 20px;.
1
background-position
property
2
Position
relative to sides
Estimated overall support.
96%
97%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

Baidu Browser

QQ Browser

Opéra Mobile

Firefox

Chrome

Safari

Edge

Opéra

Chrome pour Androïd

Safari sur IOS

Samsung Internet

UC Browser pour Androïd

Androïd Brower

Firefox pour Androïd

KaiOS Browser

Opéra mini

Historic of the background-position property.

  • Cascading Style Sheets specification - Version 1

    This first level of the CSS specification describes the mechanism of cascading style sheets. Authors and readers can add styles to documents. Mainly intended for formatting HTML documents, CSS allows defining colors, fonts, text formatting, spacing (margins), etc. The CSS language is easy to understand, readable, and easy to write; it uses common terms from desktop publishing.

    Regarding background-position Initial definition of the background-position property.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    This level 2 of the specification describes the mechanism of cascading styles in CSS. This language is used to format HTML or XML documents. It supports media-specific style sheets so that authors can adapt the presentation of their documents for visual browsers, auditory devices, printers, braille devices, portable devices, etc.

    Regarding background-position No change to the background-position property.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Backgrounds and Borders Module Level 3

    Level 3 of the specification also explains how to position the background relative to the background area, and how to crop it.
    Regarding borders, the concept of image borders is introduced, as well as the ideas of rounded corners and box shadows.

    Regarding background-position No change to the background-position property.
    WD
    August 02, 2002
    Working Draft.
    CR
    December 17, 2009
    Candidate Recommendation.
    PR
    REC

See also, about backgrounds.

Everything about backgrounds and borders is described in the W3C specification "CSS Backgrounds and Borders Module". Here are the main properties described in this standard.

Properties:

background
Summary of background properties.
background-attachment
Background image scrolling mode.
background-clip
Defines the extent of the background, especially in relation to the border and padding.
background-color
Sets the background color.
background-image
Defines the background image or gradient (multiple images or gradients can coexist).
background-origin
Defines a reference to position the background image.
Background-position
Defines how the background image is positioned.
Background-position-block
Sets the background positioning in the block direction.
Background-position-inline
Sets the background positioning along the lines.
Background-position-x
Sets how the background image is positioned horizontally.
Background-position-y
Defines how the background image is positioned in the vertical direction.
background-repeat
Defines how the background image is repeated.
background-size
Sizing the background image.
border
A shorthand property that defines the set of border parameters: width, style and color.
border-bottom
Sets the parameters of the bottom border of the element (thickness, line style, color).
border-bottom-color
Sets the border line color for the bottom of the element.
border-bottom-left-radius
Sets the radius of the rounded corner at the bottom left.
border-bottom-right-radius
Sets the radius of the rounded corner in the lower right.
border-bottom-style
Sets the line style for the border below the element.
border-bottom-width
Sets the thickness of the border line located below the element.
border-color
Sets the border color for all four sides of the element.
border-end-end-radius
Sets the radius of the corner rounding at the end of line and end of block.
border-end-start-radius
Sets the radius of the corner rounding at the end of line and beginning of the block.
border-image
Langue française
Shorthand of border properties made with images.
border-image-outset
Langue française
Sets the overflow of the border image.
border-image-repeat
Langue française
Defines how the border image is repeated or expanded to cover the desired area.
border-image-slice
Langue française
Defines how the border image is clipped.
border-image-source
Sets the path and file name for the image used to build the border.
border-image-width
Langue française
Sets the thickness of the border when the border is made with an image.
border-left
Sets the three parameters of the left border of the element (thickness, line style, color).
border-left-color
Sets the color of the left border line of the element.
border-left-style
Sets the border style for the left side of the element.
border-left-width
Sets the thickness of the border on the left side of the element.
border-radius
Radius of rounded corners.
border-right
Sets the three parameters of the right border of the element (thickness, line style, color).
border-right-color
Sets the color of the border line on the right side of the element.
border-right-style
Sets the line style for the right border of the element.
border-right-width
Sets the thickness of the border on the right side of the element.
border-start-end-radius
Sets the radius of the corner rounding at the beginning of the line and the end of the block.
border-start-start-radius
Sets the radius of the corner rounding at the beginning of the line and the beginning of the block.
border-style
Sets the line type for some or all four borders around the element (solid, double, dotted...).
border-top
Sets the parameters of the top border of the element (thickness, line style, color).
border-top-color
Sets the color of the border line above the element.
border-top-left-radius
Sets the radius of the rounded corner at the top left.
border-top-right-radius
Sets the radius of the rounded corner in the upper-right.
border-top-style
Sets the line style for the left border of the element.
border-top-width
Sets the thickness of the border line above the element.
border-width
Sets the thickness of the border line around the element.
box-shadow
Applies a shading effect to the frames by indicating all the parameters: color, shadow shift, blur, etc.