CSS Clamp Calculator · Guides

One Line for the Hero Image

CSS clamp() for fluid image width replaces a stack of media queries with one line that holds the image between a floor and a ceiling.

Published October 25, 2026

Most responsive image code I read is three media queries doing the job of one line. A hero image gets a fixed pixel width on desktop, a percentage on tablet, and another percentage on phones, and every breakpoint is a place where the layout can jump. CSS clamp() for fluid image width does the same job in one declaration, and it does it smoothly, because the middle value keeps moving between the breakpoints you would have written by hand.

The line looks like this: img.hero { width: clamp(280px, 60vw, 900px); height: auto; } Read it left to right. The image is never narrower than 280 pixels, never wider than 900, and between those two rails it tracks 60 percent of the viewport width. On a 375 pixel phone, 60vw is 225 pixels, so the 280 floor wins and the image stays readable. On a 1,200 pixel laptop, 60vw is 720, so the image sits at 720. On a 1,600 pixel monitor, 60vw is 960, so the 900 ceiling wins and the image stops growing before it turns into a billboard.

CSS clamp() for fluid image width

Two details decide whether this works or quietly breaks. First, always pair it with height: auto. Without that, the browser stretches the image to whatever height the layout implies and your aspect ratio is gone. Second, set width and height attributes in the HTML too. The attributes give the browser the aspect ratio before the image loads, which is what keeps the page from jumping as images arrive. Clamp handles the size. The attributes handle the shift.

The trap is the container. Viewport units measure the viewport, not the card the image sits in. Put that same hero line inside a 320 pixel sidebar on a desktop monitor and 60vw is still around 960 pixels, so the 900 ceiling wins and the image blows out its card. In a narrow container, use a percentage as the preferred value, like width: clamp(200px, 100%, 800px), or reach for container query units, which measure the container instead of the viewport. I use vw for full width heroes and percentages or cqw for anything inside a card, and I do not mix the two up anymore.

One more place where clamp earns its keep: card grids. A card image at width: clamp(160px, 100%, 420px) fills its card on every screen, never shrinks below a usable size on a narrow phone, and never exceeds the card on a wide monitor. No breakpoints, no jump, and the same line works when the card itself moves between a one column phone layout and a three column desktop grid.

Frequently asked questions

What does clamp() do for image width?

It sets a minimum, a preferred fluid value, and a maximum in one declaration, so the image scales smoothly between the floor and the ceiling instead of jumping at breakpoints.

Should I use vw or a percentage inside clamp() for images?

Use vw for full width heroes that track the viewport. Use a percentage or container query units for images inside cards, because vw ignores the container width and can overflow a narrow card.

Why do I need height auto with clamp() image width?

Without height auto, the browser does not preserve the aspect ratio as the width changes, and the image can stretch or squash. Height auto keeps the ratio intact at every width.

Does clamp() replace srcset for responsive images?

No. Clamp controls the displayed size. Srcset controls which file the browser downloads at that size. Use both: clamp for layout, srcset for bandwidth.

Run your own numbers before you decide.

Use the CSS Clamp Calculator to build your own fluid image width and export it to Tailwind.

Related guides