object-positionThe object-position property controls which part of a replaced element (like an image, video, or <canvas>) is visible inside its box when it is resized using object-fit. Instead of always centering the image, you can focus on the top, bottom, corners, or even specific percentage positions.
<img>, <video>, and <picture>.object-fit (like cover or contain).top, left, center…) or percentages (20% 80%).Basic syntax of object-position with object-fit is:
/* x-position y-position for the replaced element */
img {
object-fit: cover;
object-position: center center; /* keywords */
}
img.hero {
object-fit: cover;
object-position: 30% 70%; /* percentages (x% y%) */
}
All boxes below use the same image with different object-position values. Imagine these as profile pictures, banners, or thumbnails.
The object-position property takes two values:
left, center, right, or a percentage.top, center, bottom, or a percentage.Example: object-position: 10% 90%; means:
/* Focus different areas of the same image */
img.profile {
width: 200px;
height: 150px;
object-fit: cover;
object-position: center; /* same as center center */
}
img.profile-top {
width: 200px;
height: 150px;
object-fit: cover;
object-position: top center; /* show more of the top area */
}
img.profile-custom {
width: 200px;
height: 150px;
object-fit: cover;
object-position: 20% 80%; /* custom focus point (x% y%) */
}
object-fitobject-position becomes most useful when combined with different object-fit values:
| object-fit | object-position | Effect |
|---|---|---|
| cover | center | Crops and centers image inside the box. |
| cover | top left | Shows the top-left corner of the image. |
| contain | bottom | Fits full image and aligns it to the bottom. |
| none | center | Shows image at original size, aligned to center. |
/* Comparing object-fit and object-position combinations */
.card-img-cover {
width: 220px;
height: 140px;
object-fit: cover;
object-position: top left; /* crop from top-left */
}
.card-img-contain {
width: 220px;
height: 140px;
object-fit: contain;
object-position: bottom; /* image stays fully visible, aligned bottom */
}
.card-img-original {
width: 220px;
height: 140px;
object-fit: none;
object-position: center; /* original size, centered inside box */
}
If you apply the styles above to three images inside equal-sized boxes:
This combination lets you design professional-looking cards, hero sections, profile pictures, and thumbnails where the focus point is always correct, regardless of screen size.
object-position with a meaningful object-fit value like cover or contain.top, left, bottom, center) for quick alignment.50% 75%) when you need precise focus control on a subject.object-fit: none; and object-position to understand raw cropping behavior.object-fit: cover and try different object-position values.object-position to focus on different areas of an image (top, center, bottom, corners).object-fit to a <div> and observe that it does not work (it must be a replaced element).object-position: right top;.object-fit: none; and move the image to the bottom-left using object-position: left bottom; and observe cropping.
<!-- Simple playground for object-fit and object-position -->
<div style="width: 200px; height: 150px; border: 2px dashed #999; overflow: hidden;">
<img src="your-image.jpg"
style="width: 100%; height: 100%; object-fit: cover; object-position: center center;">
</div>
<!-- Try changing object-fit and object-position values above -->