Responsive Images in CSS with object-fit: cover

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.

Old way 7 lines
<!-- div instead of img: no alt, not semantic --><div class="card-image"></div>.card-image   {  background-image: url(photo.jpg);  background-size: cover;  background-position: center;}
5 lines
<img src="photo.jpg" alt="..." loading="lazy">img   {  object-fit: cover;  width: 100%;  height: 200px;}
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

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.

ESC