Object-position for Focal Point Control
What you'll learn: How to control which part of an image remains visible when it's cropped using object-fit.
When you use object-fit: cover or object-fit: contain, the browser decides which part of the image to show by default (usually centering it). But what if the important part of your image—a person's face, a product detail, or a landscape's horizon—isn't in the center? That's where object-position comes in.
How It Works
object-position lets you specify exactly which part of an image should be visible within its container. Think of it like adjusting a picture frame over a larger photo—you're choosing what shows through the frame.
You can use keywords like top, bottom, left, right, or center:
img {
width: 300px;
height: 200px;
object-fit: cover;
object-position: top right; /* Show the top-right portion */
}
Or use percentage values for precise control:
img {
object-fit: cover;
object-position: 75% 25%; /* Horizontal 75%, Vertical 25% */
}
The first value controls horizontal positioning (left to right), and the second controls vertical (top to bottom). 50% 50% is the default center position.
Real-World Example
Imagine a portrait photo cropped into a wide banner. By default, the person's face might get cut off. Using object-position: 50% 20% keeps the face centered horizontally but shifts focus toward the top 20% of the image—right where faces typically are.
Key Takeaway: object-position gives you precise control over which part of an image remains visible when cropped, ensuring important focal points stay in view.