The CSS object-position property is a powerful tool that allows developers to precisely control how images, videos, and other replaced elements are positioned within their containers. Working hand-in-hand with the object-fit property, it provides fine-grained control over visual presentation in responsive web design.
What is CSS Object-Position?
The object-position property specifies the alignment of replaced elements (like images and videos) inside their content box. It’s particularly useful when the aspect ratio of the content doesn’t match the container, allowing you to choose which part of the content to display prominently.
Basic Syntax
object-position: <position>;
/* Examples */
object-position: center;
object-position: top right;
object-position: 50% 25%;
object-position: 10px 20px;
Understanding Object-Position Values
The object-position property accepts various types of values that determine how content is positioned within its container.
Keyword Values
CSS provides several predefined keywords for common positioning scenarios:
- center – Centers the content both horizontally and vertically
- top – Aligns content to the top edge
- bottom – Aligns content to the bottom edge
- left – Aligns content to the left edge
- right – Aligns content to the right edge
Percentage Values
Percentage values offer precise control over positioning. The first percentage represents horizontal position, the second represents vertical position:
Length Values
You can use absolute units (px, em, rem) or relative units to specify exact positioning distances from the top-left corner of the container.
Practical Examples with Visual Demonstrations
Example 1: Basic Image Positioning
Here’s how different object-position values affect image display:
object-position: center;
object-position: top left;
object-position: bottom right;
Complete CSS Code
.image-container {
width: 300px;
height: 200px;
border: 2px solid #ddd;
overflow: hidden;
}
.positioned-image {
width: 100%;
height: 100%;
object-fit: cover;
}
/* Different positioning examples */
.center { object-position: center; }
.top-left { object-position: top left; }
.bottom-right { object-position: bottom right; }
.custom-percent { object-position: 75% 25%; }
.custom-pixels { object-position: 20px 10px; }
Interactive Object-Position Demo
Try Different Positioning Values
Current: center
Choose Position:
Working with Object-Fit and Object-Position
The object-position property works best when combined with object-fit. Here’s how different object-fit values interact with positioning:
object-fit: cover
Scales and crops to fill container
object-fit: contain
Scales to fit entirely within container
Real-World Use Cases
1. Portrait Image in Landscape Container
When displaying portrait images in landscape containers, object-position helps focus on the subject:
.portrait-in-landscape {
width: 400px;
height: 200px;
object-fit: cover;
object-position: center top; /* Focus on the top portion */
}
/* For face-focused cropping */
.face-focus {
object-position: 50% 25%; /* Horizontal center, upper quarter */
}
2. Video Thumbnails
Video thumbnails often need precise positioning to show the most relevant frame:
.video-thumbnail {
width: 300px;
height: 169px; /* 16:9 aspect ratio */
object-fit: cover;
object-position: center;
border-radius: 8px;
}
/* For action scenes, focus on center-right */
.action-thumbnail {
object-position: 60% center;
}
3. Responsive Image Cards
In card layouts, images need consistent positioning across different screen sizes:
.image-card {
width: 100%;
height: 250px;
object-fit: cover;
object-position: center;
}
@media (max-width: 768px) {
.image-card {
height: 200px;
object-position: center 30%; /* Adjust for mobile viewing */
}
}
Advanced Techniques and Tips
Dynamic Positioning with CSS Custom Properties
You can create dynamic positioning systems using CSS custom properties (variables):
:root {
--img-pos-x: 50%;
--img-pos-y: 50%;
}
.dynamic-image {
object-position: var(--img-pos-x) var(--img-pos-y);
transition: object-position 0.3s ease;
}
/* JavaScript can update these values */
.dynamic-image.focus-left {
--img-pos-x: 25%;
}
.dynamic-image.focus-top {
--img-pos-y: 25%;
}
Accessibility Considerations
When using object-position, consider these accessibility best practices:
- Alt text – Ensure alt text describes the visible portion of the image</li








