- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Object Position
While the object-fit property defines how an image or video scales to fit its container, the object-position property determines where that content sits within the box. Think of object-fit as the zoom and object-position as the camera pan.
By default, browsers center replaced elements (like <img> or <video>) within their content boxes. However, if you are using object-fit: cover, you might find that the most important part of your image—like a person's face or a product—is being cropped out. object-position allows you to shift the focal point to ensure the right part of the image remains visible.
background-position for CSS background images, you already know how object-position works! They use the exact same coordinate logic, but object-position is specifically for HTML tags like <img> and <video>.
Basic Syntax
The property typically takes two values: the horizontal (x) position and the vertical (y) position. If you only provide one value, the second defaults to 50% (center).
.object-fit-image {
width: 100%;
height: 100%;
/* Define how the image stretches/crops */
object-fit: cover;
/* Define the focal point: x-axis y-axis */
object-position: 50% 10%;
}
object-position with object-fit: cover or object-fit: contain. If the image isn't being resized or cropped, moving its position usually won't have a visible effect because the image already fills the space perfectly.
Values for object-position
Horizontal Position (X-axis):
- Keywords:
left,center,right. - Percentages:
0%(left),50%(center),100%(right). - Length values: Precise units like
20pxor2remoffset from the left edge.
Vertical Position (Y-axis):
- Keywords:
top,center,bottom. - Percentages:
0%(top),50%(center),100%(bottom). - Length values: Precise units like
10pxor1.5remoffset from the top edge.
object-position: top left; is valid, but object-position: 20% 80%; means 20% from the left and 80% from the top.
Example Usage
In real-world web development, this is most commonly used for "Hero" headers or profile avatars where the subject isn't perfectly centered in the original photo.
.object-fit-container {
width: 300px;
height: 200px;
border: 2px solid #333;
overflow: hidden;
border-radius: 8px;
}
.object-fit-image {
width: 100%;
height: 100%;
/* Crop the image to fill the container */
object-fit: cover;
/* Shift the "camera" to focus on the bottom of the image */
object-position: center bottom;
}
HTML
<div class="object-fit-container">
<img src="landscape.jpg" alt="Mountain Landscape" class="object-fit-image">
</div>
In this example:
- .object-fit-container acts as a fixed-size window or "frame."
- object-fit: cover ensures the image fills that frame without leaving empty gaps, even if the aspect ratios don't match.
- object-position: center bottom; ensures that if the image must be cropped, the browser cuts off the top of the image and keeps the bottom (perhaps where the foreground detail is) in view.
100px), the image might move entirely out of the frame if the container is smaller than that value. Percentages are generally much safer for responsive layouts.
Additional Considerations
- Responsive Design: You can change the
object-positioninside Media Queries. For example, on a mobile screen, you might wantobject-position: center, but on wide desktops, you might shift it toleft centerto avoid overlapping with text. - Art Direction: This property is a lifesaver when using CMS-uploaded images where you can't control the original cropping. It gives developers a way to perform "programmatic art direction."
- Browser Support:
object-positionis supported in all modern browsers (Chrome, Firefox, Safari, Edge). If you must support Internet Explorer, you will need a polyfill or fallback tobackground-image.
object-position: center top; is often a safer default than the standard center center, as it prevents heads from being accidentally cropped out of the frame.
By mastering object-position, you gain full control over the visual balance of your UI, ensuring that your key content remains the center of attention regardless of the screen size.