background-position accepts two values, horizontal offset first, then vertical, using keywords like left, center, and right paired with top, center, and bottom, or explicit length/percentage values measured from the top-left corner by default. A percentage value is calculated relative to the difference between the element's size and the background image's size, not simply a percentage of the element's own dimensions, which is why background-position: 50% 50% correctly centers an image regardless of its actual size, rather than placing it at the literal midpoint of the container.
1Understanding Background-position
background-position accepts two values, horizontal offset first, then vertical, using keywords like left, center, and right paired with top, center, and bottom, or explicit length/percentage values measured from the top-left corner by default. A percentage value is calculated relative to the difference between the element's size and the background image's size, not simply a percentage of the element's own dimensions, which is why background-position: 50% 50% correctly centers an image regardless of its actual size, rather than placing it at the literal midpoint of the container.
background-position: center center, or simply center, reliably centers a background image both horizontally and vertically regardless of the image's or container's actual size, since percentage positioning accounts for the size difference between the two rather than just the container's raw dimensions.
.hero {
background-image: url("hero.jpg");
background-position: center center;
background-repeat: no-repeat;
}2Practical Example
Here is a real-world application of Background-position showing how it is used in production CSS code.
.icon-box {
background-image: url("icon.png");
background-position: right 10px top 10px;
background-repeat: no-repeat;
}3Best Practices
Follow these guidelines when working with Background-position:
1. Use the center keyword, or 50% 50%, to reliably center a background image regardless of the element's or image's exact dimensions
2. Pair background-position with background-size: cover for a common, responsive full-bleed background image pattern that stays centered and properly cropped at any container size
3. Use explicit length values, like background-position: 20px 10px, when you need a precise, fixed offset rather than a keyword-based or percentage-based position
Tip: background-position: center center, or simply center, reliably centers a background image both horizontally and vertically regardless of the image's or container's actual size, since percentage positioning accounts for the size difference between the two rather than just the container's raw dimensions.
.hero {
background-image: url("hero.jpg");
background-position: center center;
background-repeat: no-repeat;
}