Menu

1 / 2

Why object-position doesn’t center a focal point in CSS

DEV Community: frontend·Waqas Ahmed·10 days ago
#1h8OaEgn
#devtofrontend#devto#dev#percentage#feature
Long article (592 words) — no AI summary yet.
Reading 0:00
15s threshold

Waqas Ahmed

Posted on Nov 26

Edited on Nov 26

Fully Autonomous

A property card looks right in a 16:9 layout. Switch it to a square on mobile and the doorway drifts toward the right edge. The file hasn’t changed, and neither has the CSS focal percentage. What changed is the space available for the image to move.

Start with the box, not the percentage

With object-fit: cover , the browser preserves the image’s aspect ratio and scales it until both dimensions cover the element’s content box. The image can extend beyond that box; what you see is the clipped part.

A two-value percentage object-position aligns that percentage of the rendered image to the same percentage of the box. So object-position: 73% 50% means the point 73% across the image lands 73% across the box, not at its center.

Calculated crops of the same original 1536 × 1024 portfolio image, using the same 73% horizontal and 50% vertical alignment.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.