Sizing Images and Replaced Elements
Why images behave differently from other boxes, how intrinsic size and aspect ratio work, and the small reset that makes images safe in any layout.
Sizing Images and Replaced Elements
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A replaced element (`img`, `video`, `canvas`, `iframe`) gets its content from outside CSS, so it arrives with an intrinsic width, height and aspect ratio that CSS does not define.
- The reset `img { max-width: 100%; height: auto; display: block; }` stops images overflowing their containers, keeps their proportions, and removes the stray gap beneath inline images.
- Setting `width` and `height` attributes on an `<img>` makes the browser reserve the right space before the file loads, via a default `aspect-ratio` of `auto <width> / <height>`, which prevents layout shift.
- `object-fit` controls how an image fills a box whose shape differs from its own: `cover` crops, `contain` letterboxes, `fill` stretches, `none` keeps its size.
- Images are inline by default, so they sit on the text baseline and leave a few pixels of space for descenders under them.
Boxes whose contents CSS does not control
Most boxes size themselves from their content: text flows, children stack, the box wraps around the result. An <img> is different. Its content is a file, and the CSS engine cannot look inside it. A box whose content comes from outside the CSS formatting model is a replaced element. img, video, canvas, iframe and embed are the usual ones, and input elements behave similarly.
A replaced element comes with three facts the file supplies: an intrinsic width, intrinsic height and intrinsic aspect ratio. With nothing else set, an image paints at its natural pixel size. A 4000px-wide photo takes 4000 CSS pixels and blows through any container. Most of the work in this lesson is deciding how to override those facts safely.
The reset almost everyone uses
img {
max-width: 100%;
height: auto;
display: block;
}
Each line has a job.
max-width: 100% caps the image at its container’s width. On a small screen it shrinks; in a wide container it still stops at its natural size and never stretches past it, which is why max-width is used instead of width: 100%.
height: auto lets height follow the width, so shrinking keeps the proportions. This matters most when the HTML carries a height attribute, as the next section shows.
display: block removes a bug that catches most people once. Images are inline, so they sit on the text baseline, and the line box reserves space beneath the baseline for the tails of g, p and y. The result is a gap of a few pixels under every image. I measured a 100px image inside a div at 104px tall as inline and 100px once it was display: block. Kevin’s video also covers vertical-align: middle as a different fix for the same gap.
Reserving space with width and height attributes
Because CSS often uses height: auto, it is easy to forget that the HTML width and height attributes still matter. They tell the browser the image’s shape before the file has downloaded. The browser maps them onto a default aspect-ratio:
<img src="hero.jpg" width="800" height="400" alt="…">
I read the computed style of such an image: aspect-ratio: auto 400 / 200, for attributes of 400 by 200. The auto part means “use the file’s real ratio once it loads, and this guess until then”. So with img { max-width: 100%; height: auto }, the space for a 2:1 image is reserved at the right height as soon as its width is known. Without the attributes, the image takes zero height until it loads and then pushes everything down, a layout shift that counts against performance scores. The deeper treatment is in aspect ratio and responsive media.
A broken image with no dimensions measures 0 by 0 in the layout, which is another reason to give every image its attributes.
When the box and the image disagree: object-fit
If you force both width and height to a shape different from the image, the picture stretches. object-fit decides how the content fits the box:
.thumb {
width: 200px;
height: 100px;
object-fit: cover; /* fill, cropping the overflow */
object-position: 50% 20%; /* which part to keep */
}
fill(default): stretch to the box, distorting.contain: scale to fit entirely, leaving empty bars.cover: scale to fill and crop what does not fit.none: natural size, cropped by the box.scale-down: whichever ofnoneandcontainis smaller.
object-fit works on any replaced element, so it also fits video. The box itself keeps its declared 200 by 100; only the content inside moves. I confirmed the box stays at 200 by 100 with object-fit: cover.
Replaced elements and layout systems
Replaced elements in Flexbox and Grid are stretched along the cross axis by default, which can distort an image. Setting align-self or object-fit stops it, a point the Flexbox group comes back to. Responsive image selection, srcset and sizes, which choose which file to download, is a job for HTML, and is not covered here.
Inline SVG and <img src="logo.svg"> are different things. The img is a replaced element sized by the rules above. Inline <svg> has its own sizing behavior based on its width, height and viewBox.
What the video covers
Kevin Powell’s “A better image reset for your CSS” is about 11 minutes and is from November 2023. From its description and chapters it starts with max-width, then vertical-align: middle, height: auto, font-style: italic (for how alt text looks when an image fails), background-size and background-repeat, and finally shape-margin. That is a reset-focused video rather than one about object-fit, so the object-fit and aspect-ratio sections here are my own, tested in Chromium; the runner-up is a short 2018 clip dedicated to object-fit.
Where this fits
This closes the layout-fundamentals group: you now have the box, normal flow, margins, positioning, stacking and overflow, and images. Typography comes next. The frontend roadmap’s responsive design lesson uses the same image reset, and aspect ratio and responsive media picks up from the shape of media in layouts.
Check yourself
A 1200px-wide photo in a 400px-wide column overflows it. What is the minimal fix?
`img { max-width: 100%; height: auto; }`. `max-width: 100%` caps the image at the container width, and `height: auto` keeps the aspect ratio by deriving height from the new width instead of keeping the attribute or intrinsic height.
Why is there a gap of a few pixels under an `<img>` inside a `<div>` with a background color?
The image is an inline box sitting on the text baseline, and the line box reserves room below the baseline for letters like `g` and `p`. Setting `display: block` on the image (or `vertical-align: middle`) removes the gap. I measured 104px for a 100px image inline versus 100px as a block.
Why put `width="800" height="400"` attributes on an image that CSS then resizes to `width: 100%; height: auto`?
Before the file arrives, the browser has no idea of the image's shape. The attributes give it a ratio, exposed as `aspect-ratio: auto 800 / 400`, so it can reserve the right height and the page does not jump when the image loads.
You need a 200×100 thumbnail from photos of different shapes with no distortion. Which properties?
`width: 200px; height: 100px; object-fit: cover;`, optionally with `object-position` to choose which part is kept. `cover` scales the image to fill the box and crops the overflow, so proportions are never stretched.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.