background-position
Sets the position of the background image within the element.
- Initial value
0% 0%- Inherited
- No
- Animatable
- yes, as a length or percentage
- Applies to
- all elements
- JavaScript
element.style.backgroundPosition = "center top"
Example
#.search {
background: url("/icons/search.svg") no-repeat left 12px center / 16px;
padding-left: 36px;
}<input placeholder="Search the documentation" style="padding: 8px 12px 8px 40px; border-radius: 8px; border: 1px solid #444; width: 100%; background: #111 url('/collections/nextjs.png') no-repeat left 12px center / 18px;">Definition and usage
#The background-position property specifies where the background image sits in the element. It accepts keywords (top, center, bottom, left, right), percentages or lengths, first for the horizontal axis, then for the vertical one.
Percentages work cleverly: 0% 0% puts the image’s top left corner at the element’s top left corner, and 100% 100% puts the image’s bottom right corner at the element’s bottom right corner. 50% 50% centres the image.
With background-size: cover, the position decides which part of the photo stays visible after cropping. It is worth matching it to the most important part of the photo.
Syntax
#background-position: <x-position> <y-position>?;Values
#- The top left corner. The default value.
- The centre of the element on both axes.
- The bottom right corner.
with offset
16px from the right and 16px from the top.percentages
A point at 25% horizontally and 75% vertically.
Values in practice
#left top
.demo { background-position: left top; }center
.demo { background-position: center; }right bottom
.demo { background-position: right bottom; }right 16px top 16px
.demo { background-position: right 16px top 16px; }25% 75%
.demo { background-position: 25% 75%; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Place an icon in a text field with
background-position: left 12px centerand make room with padding. - With
cover, set the position to the most important part of the photo, for examplecenter topfor portraits.
Related entries
#- background-sizeSets the size of the background image: covering everything, fitting entirely, or specific dimensions.
- background-imageSets an image or gradient as an element’s background.
- background-repeatDecides whether and how the background image repeats to fill the element.
- object-positionSets the position of an image or video within its box.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.