Key points
aspect-ratiosets the frame;object-fit: covercrops the image instead of distorting it.- Keeping
widthandheightin the HTML reserves space and prevents layout shift. - CSS cropping does not shrink the download, so generate a matching image size on the server too.
“My portfolio cards don’t line up” is one of the most common requests we get a few weeks after a site launches. The client uploaded one portrait photo, one landscape shot and one very wide screenshot, and the grid went ragged. We used to work around it with background images or the padding hack. A few lines of modern CSS now do the job properly.
The core rule
aspect-ratio forces a width-to-height ratio whatever the file’s real dimensions. On its own it would squash the photo; object-fit: cover fills the box by cropping, just like a background image would.
.card img {
display: block;
width: 100%;
height: auto; /* lets aspect-ratio drive the height */
aspect-ratio: 4 / 3;
object-fit: cover; /* crop instead of stretching */
object-position: 50% 30%;
background: #eee; /* placeholder while loading */
}
.card--logo img {
object-fit: contain; /* never crop a logo */
padding: 1rem;
}
/* Embeds: replaces the old padding-bottom: 56.25% trick */
.embed iframe {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
border: 0;
}
object-position decides which part survives. For portraits and team photos, shifting the focus upwards stops heads being cut off, which is the first thing clients notice. Partner logos are the opposite case: use contain, because a cropped logo looks far worse than one with some breathing room.
Keep width and height on the img
Some themes still strip these attributes “for responsiveness”. That backfires: browsers use them to work out the ratio and reserve space before the image downloads, so text does not jump around while the page loads.
<!-- width/height let the browser reserve space before the file arrives -->
<img src="project-800x600.jpg" width="800" height="600"
alt="Homepage of the redesigned site" loading="lazy">
The one requirement is height: auto in your CSS, otherwise the attribute height is applied literally. When you set aspect-ratio in CSS it wins over the ratio derived from the attributes, which is exactly what a grid needs.
In WordPress: crop on the server as well
object-fit does nothing to the file itself: a 3000-pixel-tall photo cropped in CSS weighs exactly as much as before. In our themes we register an image size hard-cropped to the card ratio.
// functions.php: a hard-cropped size matching the card ratio
add_action( 'after_setup_theme', function () {
add_image_size( 'card', 800, 600, true );
} );
// In the template: WordPress outputs width, height, srcset and sizes
echo wp_get_attachment_image( get_post_thumbnail_id(), 'card', false, [
'sizes' => '(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw',
] );
New sizes only apply to new uploads, so regenerate existing thumbnails, for instance with wp media regenerate --image_size=card. Also match sizes to your actual grid, or the browser will pick a larger file than it needs.
When it is not enough
- Set both
widthandheightin CSS andaspect-ratiois ignored. - If the subject sits somewhere different in every photo, automatic cropping stays a compromise; let editors choose a featured image in the right shape.
- Embedded videos follow the same logic:
aspect-ratio: 16 / 9on theiframereplaces the old padding-bottom wrapper.
FAQ
Is aspect-ratio well supported?
Yes, every current browser has supported it for years. In a very old browser the image simply shows at its natural height.
Wouldn’t a background image be easier?
Not for content. An img keeps alt text, native lazy loading and srcset, none of which a CSS background handles as cleanly.
Grids that don’t line up, or images that weigh too much? We build polished, fast WordPress themes designed around the content your team actually publishes.



