Sub topic 10
Making images flexible
This chapter assumes you know basic HTML, CSS selectors, the box model, and units such as px, rem, and percentages. It also assumes you have set the viewport meta tag from earlier in this tutorial.
Why images break layouts
An <img> element has an intrinsic size, which is the pixel width and height of the image file. If a 1200px-wide photo sits in a 360px-wide column, the browser draws it at 1200px. The image overflows its container and forces horizontal scrolling. Fluid text and grids do not help here, because the image ignores them.
The max-width rule
The standard fix is two declarations:
img {
max-width: 100%;
height: auto;
}max-width: 100% caps the image at the width of its container. It can still be smaller than the container, but it never exceeds it. height: auto recalculates the height from the image's aspect ratio, so the picture is scaled, not squashed.
Use max-width, not width: 100%. With width: 100%, a small icon would stretch to fill its container and look blurry.
Reserving space with width and height attributes
Without size information, the browser does not know how tall an image is until it downloads. Text then jumps down when the image appears. This is called layout shift.
Add the intrinsic dimensions as HTML attributes:
<img src="team.jpg" alt="Our team at the 2026 meetup" width="1200" height="800">With height: auto in your CSS, modern browsers use the width and height attributes to compute an aspect ratio. They reserve the right amount of space before the file arrives, and the image still scales fluidly.
Controlling the crop with object-fit
Sometimes a fixed shape matters, such as a square thumbnail in a card. If you force both dimensions, the image distorts. object-fit decides how the image fills its box:
.card img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}| Value | Behavior |
|---|---|
| `fill` | Stretches the image to the box. This is the default and distorts. |
| `contain` | Shows the whole image and may leave empty bars. |
| `cover` | Fills the box and crops the overflow. |
Here width: 100% is deliberate. The card image should fill the card, so you want the stretch behavior that max-width avoids.
Combining images with media queries
A flexible image fits its container, so a media query only needs to change the container. In a mobile-first grid, each card image resizes when the column count changes:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 40em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}You do not write separate image rules per breakpoint. The max-width rule handles every size.
Common errors
- Setting
heightin pixels. A fixedheight: 300pxwith a fluid width distorts the image. Useheight: autooraspect-ratio. - Forgetting
min-width: 0in grid or flex items. A wide image can stop its flex item from shrinking. Addmin-width: 0to the item. - Using
width: 100%on every image. Small images get upscaled and blur.
Exercise
A card image has width: 100%; height: 250px; and looks stretched on narrow screens. Which two changes keep its shape while still filling the card?
Show answer
Replace the fixed height with aspect-ratio (for example aspect-ratio: 16 / 9) and add object-fit: cover. The box keeps a consistent ratio as the width changes, and the image is cropped instead of distorted.