π What is object-position?
object-position is a CSS property that controls the alignment or focal pointof replaced elements (like <img>, <video>,<picture>) inside their container. It works together with object-fit to determine which part of the media should remain visible when the element is cropped.
Note
β Works only on replaced elements (not div backgrounds)
π― Why Use object-position?
- Control the cropping area when using object-fit: cover
- Ensure important content stays visible (faces, products, text)
- Perfect for hero banners, profile pictures, gallery thumbnails
- Fine-tune media alignment inside responsive containers
π Syntax
object-position Syntax
object-position: <position> | <x> <y>;Supports keywords, lengths, and percentages.
π Supported Position Values
| Type | Examples |
|---|---|
| Keywords | top, center, bottom, left, right |
| Percentages | 0% 0%, 50% 50%, 100% 100% |
| Lengths | 10px 20px, 2rem 5rem |
Note
π¨ Example 1 β Focusing on the Top
Top Focus
img {
object-fit: cover;
object-position: top;
}Useful for images where the subject is near the top (portraits, headers).
π¨ Example 2 β Focus on Left Center
Left Center
img {
object-fit: cover;
object-position: left center;
}Keeps the left half of the image always visible.
π¨ Example 3 β Center Cropping
Center Example
img {
object-fit: cover;
object-position: 50% 50%; /* default */
}This is the default β image is centered horizontally & vertically.
π¨ Example 4 β Focus on Bottom Right
Bottom Right Focus
img {
object-fit: cover;
object-position: 100% 100%;
}Crops from the top-left corner, keeping the bottom-right area visible.
π§ How object-position Works with object-fit
object-fit decides how the image scales.object-position decides which part stays visible.
| object-fit | object-position Meaning |
|---|---|
| cover | Controls cropping area |
| contain | Controls alignment inside extra empty space |
| none | Moves image relative to container |
π± Example β Profile Photo Cropping
Profile Photo Example
.profile img {
width: 150px;
height: 150px;
object-fit: cover;
object-position: 50% 20%; /* focus face area */
}π¬ Example β Video Cropping Focus
Video Example
video {
width: 100%;
height: 300px;
object-fit: cover;
object-position: center top;
}Keeps action scenes (like heads) in view.
π§© Advanced β Using object-position with Animations
Animated Cropping
img {
object-fit: cover;
animation: pan 5s infinite alternate;
}
@keyframes pan {
from { object-position: 0% 50%; }
to { object-position: 100% 50%; }
}Creates a βpan effectβ on images β popular in hero banners.
β οΈ Common Mistakes
- β Using object-position without object-fit β no visible effect in most cases
- β Expecting it to work on background images (use background-position instead)
- β Using percentages incorrectly (they refer to the media, not container)
- β Forgetting to set container width + height β object-fit wonβt crop correctly
Note
π₯ Summary
object-position gives you precise artistic control over how media is displayed inside containers β especially when cropping occurs. Whether you're designing galleries, hero sections, or profile thumbnails, object-position ensures the important areas stay visible.
Learn more βMDN Docs π