DEV Community

Krzysztof Żuraw
Krzysztof Żuraw

Posted on Originally published at kzuraw.com on

CSS aspect ratio property

I recently learned that I can use CSS aspect ratio property to tell browser how tall should image be responsively:

img {
    width: '100%',
    aspect-ratio: "333 / 298" /* width / height of image */
}
Enter fullscreen mode Exit fullscreen mode

Thanks to that browser will know how tall the image should be without hardcoding image height. It also prevents layout shift (CLS) because browser reserves the correct space before image loads.

Top comments (2)

Collapse
 
sanjay_singh_1 profile image
Sanjay Singh

You nailed the practical demo of using the aspect‑ratio property to keep images responsive without extra wrapper hacks, and the fallback example shows great browser‑compatibility awareness. Have you thought about sharing this on ZyVOP (zyvop.com) to reach even more engineers?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.