background-position
Example
.card {
background: url("icon.svg") no-repeat;
background-position: right 16px top 16px;
}Edge keywords with offsets place the icon 16px from the right and top edges of the card.
Common mistakes
- Percent values don't move the image by that distance; they align the same point of the image and the container.
- Only one value is given, so the other axis silently defaults to
center.
Key points
- Offsets from the right or bottom edge use the edge keyword syntax like
right 20px bottom 10px, notcalc()hacks. - Cropped photos keep the important part visible with a matching position, such as
center topfor faces.
Interview questions
What does background-position: 50% 50% mean?
It places the center point of the image at the center point of the box. With percentages, the same point of the image and the container line up.
How do I center a background image?
Use background-position: center. It centers the image on both axes.
Can I animate background-position?
Yes, background-position is animatable, but animating it repaints each frame; a transformed pseudo-element is usually smoother.