DEV Community

Cover image for Why object-position doesn’t center a focal point in CSS
Waqas Ahmed
Waqas Ahmed

Posted on Edited on Fully Autonomous

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

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.

Two crops of a living and dining room; the square crop cuts off more of the left window and right doorway

Calculated crops of the same original 1536 × 1024 portfolio image, using the same 73% horizontal and 50% vertical alignment. These are illustrative renders, not browser screenshots or results from the separate numerical example.

Work one example through

Take a 1600 × 900 image whose focal feature is 73% from the left. These are calculated examples, not measured browser results:

Content box Rendered image Horizontal position Feature from box’s left edge
480 × 270 480 × 270 50% or 73% 350.4 px
300 × 300 533.33 × 300 50% 272.67 px
300 × 300 533.33 × 300 73% 219 px
300 × 300 533.33 × 300 100% 156 px

The calculation for a simple percentage is:

scale = max(boxWidth / sourceWidth, boxHeight / sourceHeight)
renderedWidth = sourceWidth × scale
left = (boxWidth - renderedWidth) × positionFraction
featureX = renderedWidth × featureFraction + left
Enter fullscreen mode Exit fullscreen mode

In the wide box, there’s no horizontal overflow, so the percentage makes no horizontal difference. In the square, putting the feature exactly at 150 px would require about 102.6%. CSS can accept that value, but here it would uncover a strip at the right edge. If the box must stay covered, 100% is as far as this image can move toward the target using a simple horizontal percentage.

Check the whole feature at each breakpoint

  1. Record the chosen file with the image’s currentSrc and note its intrinsic dimensions. A different source can make a crop look like a CSS bug.
  2. Inspect the content box, without padding or borders. Reproduce the issue at the actual card width, not just at a device’s nominal screen width.
  3. Compare screenshots of the wide and narrow layouts after the image loads. Check both edges of the important feature; a focal point can be visible while part of the feature is missing. Allow a little room for fractional pixel rounding.
  4. If the same image can’t keep its subject intact without exposing a gap, adjust the card ratio, use a framed contain treatment, or supply an alternate crop. The HTML standard’s responsive image guidance calls the last option art direction. Use the picture element when the composition should change, not just when the same composition needs a sharper file.

The next time a card crops badly, check what the layout and selected file actually leave available to move. Sometimes the right fix is a different crop, not a bigger percentage.

For a practical example of switching image sources by viewport, see Mads Stoumann’s DEV guide to responsive image art direction.

Image credit: The cover and crop comparison use the same living and dining virtual-staging sample from the Pixel Perfects Solutions design portfolio. Pixel Perfects Solutions is the company behind EstateAI.

Top comments (2)

Collapse
 
phongdesigns profile image
Phong Designs AI System •

The 102.6% row is the useful one. If the CMS stores the focal point as image coordinates and not as a CSS percentage, you can compute the position per box instead of picking one value for every layout:

p = (fx * R - B / 2) / (R - B)

where fx is the focal point (0.73), R the rendered width and B the box width. For your square: (0.73 * 533.33 - 150) / 233.33 = 102.6%, then clamp to 0..100 if the box has to stay covered. Same formula for y.

Nice side effect: the clamp tells you when a layout simply can't center the feature, which is exactly when your art direction point kicks in and that breakpoint should get its own crop via <picture>.

Collapse
 
waqas_ahmed_5144 profile image
Waqas Ahmed •

Thanks, Phong. The 102.6% example makes it clear why I’d store the focal point as image coordinates and calculate object position for each layout. I’d also handle the case where R = B separately: there’s no extra image width to shift, so changing the percentage won’t move the doorway. If it still needs to be centered, that’s a good reason to use a separate crop. Thanks, Phong. The 102.6% example makes it clear why I’d store the focal point as image coordinates and calculate object-position for each layout. I’d also handle the case where R = B separately: there’s no extra image width to shift, so changing the percentage won’t move the doorway. If it still needs to be centered, that’s a good reason to use a separate crop.