Make an image scale fluidly without exceeding its original size

Tests intrinsic sizing awareness. Good answer: max-width: 100% scales the image down with its container but never stretches it beyond intrinsic size; add height: auto to preserve aspect ratio. Red flag: width: 100% or omitting height, causing distortion.
What's really being asked
This question checks whether you know how replaced elements like images interact with their containers. Specifically, it probes your understanding of intrinsic dimensions, the difference between width and max-width, and how to preserve aspect ratios during responsive scaling.
The full answer
First, state the rule max-width: 100%. Explain that this caps the rendered width at the containing block's width, so the image scales down on small screens, but because no width value forces growth, the image never exceeds its own intrinsic width when the container is larger. Second, add height: auto. This tells the browser to compute the height from the intrinsic aspect ratio as the width changes, preventing distortion. Third, note that these rules are usually applied via a direct img selector or a utility class. Fourth, mention that max-width overrides width if they conflict, but min-width overrides max-width, so avoid mixing in min-width: 100% or width: 100% which would break the constraint.
The mistakes people make
Proposing width: 100% is the most frequent error. That makes the image always fill its container, which violates the requirement to stay at or below original size. Another mistake is omitting height: auto, which leaves the default height attribute or CSS height in place and causes the image to squash or stretch vertically. Some candidates suggest max-width in pixels, but without knowing the image's natural width this is fragile and not fluid. A fourth red flag is mentioning object-fit: cover or contain, which solves cropping and fitting problems but does not prevent upscaling beyond intrinsic size.
What usually comes next
An interviewer might ask what happens if you also set width: 100% alongside max-width: 100%. They might ask how you would handle images that need to fill a grid cell exactly, or how srcset and sizes interact with these CSS rules. They may also ask about the performance implications of scaling down a large intrinsic image versus serving multiple resolutions.
A concrete example
Imagine a product photo with a natural width of 800 pixels placed inside an article that can be 1200 pixels wide on desktop or 320 pixels wide on mobile. With max-width: 100% and height: auto, the photo renders at 800 pixels on desktop because its intrinsic width is smaller than the container. On mobile it scales to 320 pixels wide and the height adjusts to maintain the aspect ratio. If you had used width: 100% instead, the photo would blow up to 1200 pixels on desktop, becoming blurry and consuming unnecessary layout space.
Interview question
You need a product photo to scale down on mobile but never exceed its natural 800px width on desktop. Which CSS should you use?
- a.width: 100%; height: auto
- b.max-width: 100%; height: autoCorrect
- c.max-width: 800px; height: auto
- d.max-width: 100%; height: 100%
Why? this is the answer
max-width: 100% lets the image shrink within its container but caps it at its intrinsic size, while height: auto preserves the aspect ratio. Using width: 100% instead would force the image to stretch to the full container width, causing it to exceed its original dimensions and become blurry.
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