- !important
- Animation
- Background image
- Blur() function
- Border color
- Border radius
- Border width
- Borders
- Box model
- Box shadow
- Class attribute
- Clip path
- Color
- Comment
- Cursor
- Display property
- First-child selector
- Flexbox
- Font family
- Font size
- Font style
- Font weight
- Gap
- Gradient
- Grid layout
- Height
- ID selector
- Letter spacing
- Linking a style sheet
- Margin
- Media query
- Minmax() function
- N-th-child selector
- Object fit
- Opacity
- Outline
- Overflow property
- Padding
- Pixels
- Pointer events
- Position property
- Pseudo-classes
- Pseudo-elements
- Rotate
- Rounding an image
- Scale()
- Selectors
- Specificity
- Text align
- Text shadow
- Text wrap
- Transition property
- Translate() property
- Units
- Variable
- white-space
- Width
- Z-index
CSS
CSS Object Fit: Syntax, Usage, and Examples
The CSS object-fit
property controls how content like images or videos is resized and positioned within a container. It’s especially useful when working with fixed-size frames or dynamic layouts where media needs to fill or fit in a box without stretching or distortion. Whether you’re building galleries, hero sections, or responsive thumbnails, CSS object fit gives you visual control over how media behaves inside its bounds.
How to Use CSS Object Fit
The syntax is simple:
selector {
object-fit: value;
}
Apply this to elements that use the object-fit
property such as <img>
, <video>
, or <iframe>
. The most commonly used values are:
fill
– Default value. Stretches the content to fill the container, even if that distorts the image.contain
– Scales the content to fit within the container without cropping or distortion.cover
– Scales the content to fill the container while preserving aspect ratio. Crops anything that doesn't fit.none
– Keeps the original size; doesn’t scale at all.scale-down
– Chooses betweennone
orcontain
, whichever results in smaller content.
Example usage:
img {
object-fit: cover;
}
This makes sure the image fills its container, keeping its proportions but cropping anything that doesn’t fit.
When to Use Object Fit CSS
Use this property when you want precise control over how media fits inside a container without writing extra JavaScript or cropping images manually. It’s perfect for modern designs that rely on clean image grids, responsive cards, and consistent aspect ratios.
Responsive Image Grids
When you're displaying images of different dimensions in uniform boxes, apply object fit CSS to keep your layout consistent:
.gallery img {
width: 100%;
height: 200px;
object-fit: cover;
}
This keeps images the same size without distorting or squishing them.
Hero Banners or Background Media
Hero sections with fixed heights often rely on object-fit to keep things centered and framed:
.hero img {
width: 100%;
height: 400px;
object-fit: cover;
}
The image stays centered and fills the container even if it’s cropped.
Avatar or Profile Images
Use object-fit to avoid stretched or oval-looking avatars:
.avatar {
width: 100px;
height: 100px;
border-radius: 50%;
object-fit: cover;
}
This keeps face images square or round, with equal focus on framing and proportion.
Examples of Object Fit CSS in Action
Covering a Container Without Distortion
.card img {
width: 100%;
height: 300px;
object-fit: cover;
}
This ensures that regardless of the original aspect ratio, the image always fills the box.
Fitting Content Without Cropping
.video-preview {
width: 400px;
height: 225px;
object-fit: contain;
}
This keeps the full content in view, even if it leaves padding around the edges.
Preventing Image Resize
.logo {
object-fit: none;
}
This preserves the original size and stops scaling completely. The image will overflow or align according to object-position
.
Using Scale-Down for Smart Sizing
.thumbnail {
object-fit: scale-down;
}
This is useful when you want small media that either fits naturally or scales down without ever becoming larger than its actual size.
Learn More About CSS Object-Fit
Differences Between Cover and Contain
Both cover
and contain
preserve aspect ratio, but behave differently:
cover
fills the container and crops anything that overflows.contain
fits the content inside the container without cropping, leaving empty space if needed.
This is a key decision in design: use cover
for visual impact, and contain
when full visibility matters.
Using CSS Object-Fit with Object Position
Pair object-fit
with object-position
for better control of alignment:
img {
object-fit: cover;
object-position: top;
}
This makes the image fill the container and aligns the crop from the top. Other values for object-position
include left
, right
, center
, or percentages like 50% 25%
.
img {
object-position: 50% 0%;
}
This aligns the image to the center horizontally and to the top vertically.
CSS Object-Fit and Browser Support
Object fit is well supported in modern browsers, including Chrome, Firefox, Safari, and Edge. Older versions of Internet Explorer don’t support it. For full compatibility, especially with older browsers, consider using background images with background-size
and background-position
for similar effects.
Fallback example:
.card {
background-image: url('fallback.jpg');
background-size: cover;
background-position: center;
}
Use this as a backup when object-fit support isn’t guaranteed.
Object-Fit CSS for Videos and Iframes
You can apply object fit to videos or iframes as long as you explicitly set their height and width:
video {
width: 100%;
height: 300px;
object-fit: cover;
}
Great for background loops, content previews, or embedded media players where cropping is acceptable.
Applying Object-Fit in Flexbox and Grid Layouts
If you're using Flexbox or Grid to structure your layout, you can still apply object-fit directly to the media elements inside:
.grid-item img {
width: 100%;
height: 100%;
object-fit: cover;
}
This ensures clean, uniform boxes without distortion or weird cropping behavior.
Object-Fit and Accessibility
When using object-fit, remember that cropping part of the image might cut off meaningful content. Be cautious with cover
if your image contains faces, text, or information that needs to be seen in full. Always provide descriptive alt text and test your designs with real content.
<img src="headshot.jpg" alt="Portrait of Jane Doe, software developer" />
This keeps your design inclusive, even when using aggressive cropping.
Using CSS Object-Fit in Card Components
Modern card designs often combine images, titles, and descriptions. Object-fit helps standardize the media display, even with varying image dimensions.
.card img {
width: 100%;
height: 150px;
object-fit: cover;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
This creates consistent, stylish card headers across your UI.
Animating Object-Fit with Transitions
While object-fit isn’t directly animatable, you can still use it in combination with opacity, scale, or transform transitions for smooth effects:
.zoom-card img {
object-fit: cover;
transition: transform 0.3s ease;
}
.zoom-card:hover img {
transform: scale(1.05);
}
This gives the illusion of dynamic resizing and adds visual interest.
The CSS object fit property gives you an elegant, intuitive way to control how images and videos scale inside containers. Whether you're working on a photo grid, a full-screen hero section, or product cards, object fit CSS helps maintain aspect ratios, avoid distortion, and create clean layouts. Combine it with object-position for precise control, and use fallback methods for older browsers.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.