In CSS, if an img has only its width set and height left at the default auto, what height does it render at and why?
answer
- the element's content comes from elsewhere
- natural width, height and ratio
- auto defers to the file
- both pinned means stretched
basics
~10 sAn img is a replaced element carrying an intrinsic width, height and ratio from the image file, so height: auto resolves through that ratio. The rendered height scales proportionally with whatever width you set.
solid answer
~40 sAn `img` is a *replaced element*: its content comes from an external resource that has its own natural dimensions, called intrinsic sizes. A bitmap has an intrinsic width, an intrinsic height, and therefore an intrinsic aspect ratio. When you set `width: 400px` and leave `height: auto`, the browser resolves the auto height through that ratio, so an 800x600 source renders 400x300. If you pin both `width` and `height` to values that disagree with the ratio, nothing is preserved automatically — the initial `object-fit: fill` stretches the pixels, and you need `object-fit: contain` or `cover` to keep the picture undistorted. Current browsers also read the HTML `width` and `height` attributes to derive a default ratio before the file loads, which is what lets the layout reserve the right space up front.
code
html · 1 line<img src="photo.jpg" width="800" height="600" alt="Harbour at dusk">go deeper
Be ready to say that an image carries its own natural width and height, and that setting one dimension with the other left at auto keeps the proportions. Know the pairing max-width: 100% with height: auto.
Explain the resolution order: both auto uses intrinsic sizes, one auto is computed through the intrinsic ratio, neither auto ignores the ratio entirely. Mention the 300x150 default object size for resources with no intrinsic dimensions.
Show that you keep the HTML width and height attributes for the pre-load ratio while still writing height: auto in CSS, and that you reach for object-fit rather than accepting a stretched image in a fixed-size slot.
Frame it as a contract between markup, CSS and the network: whoever ships images needs a rule that guarantees space is reserved before bytes arrive, enforced in a shared image component or lint rule rather than left to each author's memory.
## Replaced elements and intrinsic size Most boxes on a page are sized by CSS and by the content flowing inside them. A **replaced element** is different: its rendering is not described by the document's own content but by an external resource — an image file, a video stream, a plugin surface. `img`, `video`, `canvas`, `iframe`, `embed` and `object` are the common ones. Because the resource exists independently of the page, it can bring its own dimensions with it. The spec calls these **intrinsic sizes**, and a box can have up to three: - an **intrinsic width** (a 800px-wide bitmap), - an **intrinsic height** (600px), - an **intrinsic aspect ratio** (4:3). A raster image has all three. An SVG without `width`/`height` attributes but with a `viewBox` typically has only the ratio. A `canvas` or `iframe` has none of them from the resource itself. ## How auto resolves The default `width` and `height` of a replaced element are both `auto`, and the sizing algorithm fills the gaps in a fixed order: - **Both auto** — use the intrinsic width and height (the image renders at its natural pixel size). - **One auto, one specified** — compute the auto one from the specified one through the intrinsic ratio. - **Neither auto** — use both as given; the ratio is simply not consulted. So this: ```css img { width: 400px; height: auto; } ``` with an 800x600 source produces a used height of 300px. Halve the width, halve the height. This is why `height: auto` is the value you want on responsive images, and why `img { max-width: 100%; height: auto; }` is a decades-old staple: the max-width caps the image inside a narrow container and the auto height keeps it from squashing. When no intrinsic size and no ratio is available and CSS says nothing either, the box falls back to a **default object size** of 300px by 150px. That is where the mysterious 300x150 `iframe` or `canvas` comes from. ## Distortion when you fix both axes Setting both dimensions to values that contradict the ratio does not letterbox anything. The initial value of `object-fit` is `fill`, meaning the resource is stretched to exactly fill the content box: ```css .avatar { width: 100px; height: 100px; object-fit: cover; object-position: center; } ``` `cover` scales the image until it covers the box and crops the overflow; `contain` scales it until it fits entirely, leaving empty space. `object-position` chooses which part survives a `cover` crop. These properties act on the *replaced content inside* the box — they never change the box's own size, which CSS has already decided. ## The width and height attributes Before the file arrives, the browser has no intrinsic size to work with, so a bare `img` occupies no space and then jolts the page when the bytes land. Current engines (Chrome 79+, Firefox 71+, Safari 14+) solve this by deriving a default aspect ratio from the HTML `width` and `height` attributes, roughly as if the UA stylesheet said `aspect-ratio: attr(width) / attr(height)`. Combined with `height: auto` in your CSS, the browser can compute the correct display height from the CSS width immediately and hold the space open. That is why the modern advice is to keep both attributes on the markup *and* still write `height: auto` in CSS — the attributes supply the ratio, the CSS supplies the responsive width, and the auto height ties them together. ## What interviewers are checking The question separates people who think `width` and `height` are two independent knobs from people who know a replaced box has a size of its own that CSS merely constrains. The follow-through — distortion when both are pinned, the 300x150 fallback, the pre-load ratio — is all one idea: the resource has an opinion about its size, and `auto` is how you defer to it.
- What happens if you set both width and height to values that contradict the image's intrinsic ratio?The box takes exactly those dimensions and the picture is distorted, because `object-fit` defaults to `fill` — the resource is stretched to fill the content box. To keep it undistorted you add `object-fit: contain` (fits entirely, leaving gaps) or `object-fit: cover` (fills and crops), optionally steering the crop with `object-position`. Neither changes the box size CSS already resolved.
- An iframe with no width or height in CSS renders at 300 by 150 pixels. Where does that come from?An `iframe` is a replaced element with no intrinsic dimensions from its resource, so nothing supplies a natural size. CSS defines a default object size of 300px by 150px for exactly this case, and the box falls back to it. The same default explains a bare `canvas` or a `video` before metadata loads.
- How does an SVG differ from a JPEG in terms of intrinsic sizing?A JPEG always has an intrinsic width and height in pixels. An SVG may have none: if it declares only a `viewBox` and no `width`/`height` attributes, it contributes an intrinsic *ratio* but no intrinsic size. Given a CSS width it scales through that ratio; given nothing at all it falls back to the 300x150 default object size.
A replaced element is like a framed photograph handed to you: it already has a shape, and giving it only a width is telling the framer one dimension and trusting them to keep the proportions.
saying these in an interview costs you the question
- Thinks the browser preserves the ratio even when both dimensions are set
- Says an image has no size until CSS gives it one
- Believes object-fit changes the size of the box itself
- Claims height: auto on an image resolves to zero
- Thinks the HTML width attribute is only a fallback for CSS