skip to content

Intrinsic Sizing and Aspect Ratio

Sizing an element from its content rather than a hardcoded number, and constraining the result with the min/max pair. Interviewers use these to check whether you can avoid magic pixel values and reserve space to prevent layout shift.

part ofCSSoverview, primer and where to startread it →
on this pageshow

questions

5

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?

level: juniorimportance: must knowfreq 58%

answer

  1. the element's content comes from elsewhere
  2. natural width, height and ratio
  3. auto defers to the file
  4. both pinned means stretched

basics

~10 s

An 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 s

An `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
html
<img src="photo.jpg" width="800" height="600" alt="Harbour at dusk">

go deeper

for a junior

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.

for a middle

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.

for a senior

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.

for a principal

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

context

open as a page

In CSS, why does height: 100% on a child element so often do nothing, while width: 100% works as expected?

level: middleimportance: must knowfreq 70%

basics

~20 s

A percentage height resolves against the containing block's height, and a normal-flow parent with height: auto has no definite height to resolve against, so the percentage behaves as auto. Widths work because a block's available inline size is always known.

open as a page

In CSS, what do the sizing keywords min-content, max-content and fit-content mean when used as a value for width?

level: middleimportance: should knowfreq 42%

basics

~20 s

max-content is the width the content would take on a single line with no wrapping; min-content is the narrowest width without overflowing, roughly the longest unbreakable word; fit-content sits between them, using max-content when it fits the available space and shrinking no further than min-content.

open as a page

In CSS, an element has width: 400px, max-width: 300px and min-width: 500px. What is its used width, and what is the resolution order between the three properties?

level: middleimportance: should knowfreq 46%

basics

~20 s

The used width is 500px. CSS computes a tentative width from width, clamps it down to max-width if it exceeds it, then clamps it up to min-width if it falls below — so min-width always wins over max-width, which always wins over width.

open as a page

How would you use the CSS aspect-ratio property to reserve space for a media embed, and in which cases is the declared ratio ignored?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Declare aspect-ratio on the box and leave one axis auto: aspect-ratio: 16 / 9 with a fluid width computes the height before any content loads, holding the space open. The ratio is ignored when both width and height are definite, and the box can still grow past it when content does not fit.

open as a page