CSS clamp(): Fluid Sizing Without Media Queries

CSS clamp() sets guardrails for a value, letting it scale fluidly but never going outside a min/max bound. It's ideal for responsive typography, allowing font-size to grow with the viewport. The footgun: the preferred value is ignored at extreme screen sizes.
Why it exists
Before clamp(), making typography truly fluid required complex CSS with multiple media query breakpoints to adjust font-size. This was brittle, verbose, and hard to maintain. clamp() was created to solve this with a single, declarative line of code, defining the entire responsive behavior at once.
The mental model
Think of clamp() as setting a "corridor" for a value. You define a minimum size (the left wall), a maximum size (the right wall), and a preferred, flexible value that scales with the screen (your position in the corridor). As the screen shrinks or grows, your value scales with it, but it can never pass through the walls.
How it works
The function takes three arguments: clamp(MIN, PREFERRED, MAX). First, the MIN is the absolute smallest value; if the PREFERRED value calculates to something smaller, the MIN value is used. Second, the PREFERRED is the ideal value, often a viewport-relative unit like vw (viewport width) to make it scale. Third, the MAX is the absolute largest value; if PREFERRED calculates to something larger, MAX is used. The browser continuously evaluates the PREFERRED value and applies whichever of the three is appropriate for the current viewport size.
When to use it
The primary use case is fluid typography, where you want text to scale smoothly with the browser window. For example, font-size: clamp(1rem, 2.5vw, 2rem); ensures text is readable on small screens (at least 1rem) but doesn't become enormous on large monitors (at most 2rem). It's also effective for responsive layouts, like setting a container's width to be 50% of the viewport, but no narrower than 300px and no wider than 800px.
When not to use it
Avoid clamp() when you need precise, stepped changes that must occur at exact breakpoints; media queries offer more explicit control for those scenarios. Also be cautious using it for critical UI elements where predictability is more important than fluidity, as the "clamped" state might not be what a designer explicitly planned for.
One canonical example
To create a heading that scales with the viewport but has sensible limits, you would write:
h1 { font-size: clamp(1.5rem, 5vw, 3rem); }
On a very small screen, the font size will be fixed at 1.5rem. On a very large screen, it will be capped at 3rem. On screens in between, the font size will be 5% of the viewport's width, creating a smooth scaling effect.
Interview question
In clamp(MIN, PREFERRED, MAX), what determines the final computed value when PREFERRED falls outside the MIN and MAX range?
- a.The PREFERRED value is still used, but it's visually clipped by the browser.
- b.The clamp() function is ignored, and the value from the previous CSS rule is used.
- c.The browser's default value for the property is used instead.
- d.The MIN or MAX value is applied, acting as a boundary for PREFERRED.Correct
Why? this is the answer
The card explicitly states that if the PREFERRED value calculates to something smaller than MIN, MIN is used, and if it calculates to something larger than MAX, MAX is used. This means MIN and MAX serve as hard boundaries. Option A is a common misconception, as the value itself is changed, not just visually clipped.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles