Layout Beginner

Responsive images without the background-image hack

Cropped responsive images were done with background-image and background-size: cover on a div. No semantic img element, no alt text, and no native lazy loading. object-fit brings the same cropping behavior to real img elements.

5 lines
1<img src="photo.jpg" alt="..." loading="lazy">
2img {
3  object-fit: cover;
4  width: 100%; height: 200px;
5}
Old 7 lines
1<!-- div instead of img: no alt, not semantic -->
2<div class="card-image"></div>
3.card-image {
4  background-image: url(photo.jpg);
5  background-size: cover;
6  background-position: center;
7}
Widely available Since 2022 96% global usage

This feature is well established and works across many devices and browser versions. It has been available across browsers since 2022.

Safe to use without fallbacks.

32+
36+
10.1+
16+
same height, different aspect ratio images
Portrait
source
object-fit: cover
Landscape
source
object-fit: cover
Square
source
object-fit: cover
All images fill 140Γ—140 β€” no background-image hack
Kinsta

Your first month is free

Managed WordPress hosting for faster sites.

Learn more
⚑

Semantic HTML

A real img element with alt text. Screen readers and search engines see the image correctly.

✦

Native lazy loading

img supports loading="lazy" natively. Background images require Intersection Observer to achieve the same.

∞

object-position

Control which part of the image is visible with object-position. Same concept as background-position.

Lines Saved
7 β†’ 5
Real img element
Old Approach
background-image
Div with background-size: cover
Modern Approach
object-fit: cover
Native img with CSS cropping

How it works

Cropped images in card layouts used background-image on a div with background-size: cover. Visually it worked, but it meant no <img> element, no alt attribute, no native lazy loading, and no srcset for responsive images.

object-fit: cover applies directly to an <img> element. The image fills its container and is cropped to fit, just like background-size: cover. object-position controls which part stays visible, matching background-position. The image stays semantic, accessible, and gets native browser optimization.

New CSS drops.

Join 600+ readers who've survived clearfix hacks.

ESC