background-position

Term side of the Card.
Topic: Background
Unit title: background-position
Categories: Property
Explanation side of the Card

Explanation: The property that sets the starting position of a background image within an element. It allows precise control over where the image appears, using coordinates, keywords or percentages.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

css
.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, not calc() hacks.
  • Cropped photos keep the important part visible with a matching position, such as center top for 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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy