日本語

How to Use CSS object-position to Control Which Part of an Image Shows

​​CSS​HTML

When you fit an image into a box with object-fit: cover, the overflowing part is cropped. object-position decides which part is kept. If the point you want to show is at 80% across and 30% down the image, object-position: 80% 30% keeps that point inside the box, whether the box is tall or wide. This helps when a thumbnail or card image is cropped from the center and a person's face or a product gets cut off.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I prepared test images with a different color at each position, and used the pixel colors on the rendered screen to find which part of the image appeared at the edges of the box. I did not test Firefox or Safari, photographic images, or the <video> element. The browser version support figures are the values from the compatibility data (@mdn/browser-compat-data 8.1.8).

Only one direction gets cut off

cover scales the image, keeping its aspect ratio, until there is no gap left in the box. As a result, the overflow is in only one direction, either vertical or horizontal.

In the test, I put a 600×400 image into a 300×300 img.

css
.thumb {
  width: 300px;
  height: 300px;
  object-fit: cover;
}

The image is drawn at 450×300, so 150px overflows horizontally. Vertically it fits exactly, so nothing is cut there. In fact, changing only the vertical value, as in 50% 0% or 50% 100%, did not change which part of the image appeared at the top and bottom edges of the box compared with the default 50% 50%.

object-position takes two values in the order "horizontal vertical." When adjusting the values, first work out from the image and box shapes which direction is being cropped. If the box is taller than the image's proportions, the horizontal value matters. If the box is wider, the vertical value matters.

Percentages line up the same fraction of the image and the box

The initial value of object-position is 50% 50%, which lines up the center of the image with the center of the box. 0% lines up the left edge of the image with the left edge of the box, and 100% lines up the right edge of the image with the right edge of the box.

Diagram showing a 300×300 box and an image scaled to 450×300, drawn with the horizontal object-position value at 0%, 50%, and 100%. At 0%, the image's left edge aligns with the box's left edge. At 50%, the image overflows 75px on each side. At 100%, the image's right edge aligns with the box's right edge. Pink lines mark the points at the same fraction of the image and the box
Image and box positions when the horizontal value is 0%, 50%, and 100%

The specification treats this value the same way as background-position. According to the CSS Backgrounds specification, a horizontal percentage is a fraction of "box width minus image width." In the example above, that is 300 − 450 = −150px, so with 50% the left edge of the image sits at −75px relative to the left edge of the box, and with 100% it sits at −150px. The specification also gives an example where 75% 50% places the point at 75% across and 50% down the image onto the point at 75% across and 50% down the box.

In the test environment, the positions of the image shown at the left and right edges of the box (as x coordinates in the original 600px image) were as follows.

Horizontal value Position shown at left edge Position shown at right edge
0% 0 397
25% 49 446
50% (default) 101 498
75% 150 545
100% 202 597

Between 0% and 100%, the image moves across the whole box with no gap. The keywords left and right computed to 0% 50% and 100% 50%, respectively. center top computed to 50% 0%.

Write the position of the point you want to show

Because of this behavior, if you measure the position of the point you want to show as a fraction of the image's width and height, and write those numbers directly in object-position, that point is always inside the box. The 80% position of the image lines up with the 80% position of the box.

In the test, I put a 600×400 image with a cat's face drawn at 80% across and 30% down into three boxes of different shapes. To allow a different focal point for each image, I took the values through custom properties.

css
.thumb {
  object-fit: cover;
  /* Focal point for each image. Center if not specified */
  object-position: var(--focus-x, 50%) var(--focus-y, 50%);
}
html
<img class="thumb" src="cat.png" alt="" style="--focus-x: 80%; --focus-y: 30%;">
Screen showing the image without a value in the top row and with 80% 30% specified in the middle row, placed into a tall box (150×300) and a square box (300×300). In the tall box with no value, the cat's face does not appear at all, and in the square box the right side of the face is cut off. With 80% 30%, the face is inside both boxes. The bottom row shows a wide box (400×150): without a value, the ear is cut off at the top edge, and with 80% 30% almost the whole ear, including its tip, is visible
Comparison of no value (50% 50%) and the focal point 80% 30%

I counted the pink pixels of the cat's face shown in each box and compared them with the count when the whole image was drawn at the same scale.

Box No value 80% 30%
Tall 150×300 0% (face center outside the box) 87.5%
Square 300×300 69.0% 100%
Wide 400×150 74.4% 98.9%

In the tall box, with no value the face center was 210px from the left of the box, which is outside the 150px-wide box. With 80% 30%, the face center was 120px from the left of the box, that is, at 80% of the box width. However, since the box is narrow compared with the size of the face, the tip of the right ear is slightly cut off. The focal point is guaranteed to be inside the box, but how much of the area around it fits depends on the size of the box.

Specifying the distance from an edge

Values written as lengths such as 10px 20px place the top-left corner of the image 10px right and 20px down from the top-left corner of the box. In the 300×300 box above, -75px 0px displayed the same as 50%, and -150px 0px displayed the same as 100%. However, a value written as a length keeps the same distance even when the box size changes.

If you want the distance from the right or bottom edge, combine keywords and lengths into a four-value form.

css
.thumb {
  object-position: right 30px top 0;
}

In the test environment, the computed value was calc(100% - 30px) 0px. The right edge of the image sits 30px inside the right edge of the box, so even with cover a 30px gap appears on the right, and the img's background color shows through. 120% 50% gave the same display. Conversely, left 30px top 0, 30px 50%, and -20% 50% left a 30px gap on the left side. The object-position section of the CSS Images specification also states that the parts not covered by the image show the element's background.

Warning

Three-value forms such as right 30px top, which work with background-position, are not valid for object-position. In the test environment, right 30px top and right 30px center were ignored as invalid values, and CSS.supports('object-position', 'right 30px top') returned false (it returns true for background-position). The <position> type in the CSS Values specification accepts only one, two, or four values, and the three-value form is accepted only by background-position. Invalid values are ignored and the default 50% 50% remains if nothing else is specified, so this mistake is easy to miss.

Animate on hover to reveal the hidden part

object-position can be animated with transition. The CSS Images specification specifies that animation handling is the same as for background-position.

css
.pan {
  width: 300px;
  height: 300px;
  object-fit: cover;
  object-position: 0% 50%;
  transition: object-position 2s linear;
}

.pan:hover {
  object-position: 100% 50%;
}

In the test environment, hovering moved the computed value to about 25% after 500ms, about 50% after 1000ms, and about 75% after 1500ms, and it reached 100% 50% about 2.3 seconds later, just after the 2-second transition ended. The position of the image shown at the left edge of the box also moved from 0 to 202 in x coordinates. About 2.3 seconds after the mouse left, it had returned to 0% 50%. You could use this to put a landscape photo into a square card and reveal the hidden right side on hover.

Browser support

According to the compatibility data, object-position is supported from Chrome 32, Edge 79, Firefox 36, and Safari and Safari on iOS 10. Edge 16 to 18 had partial support, limited to the <img> element.

​Posted by 86