Sub topic 6
Aspect ratios for web layouts
This chapter assumes you know basic HTML (the img element and its attributes), basic CSS (selectors, width and height), and how breakpoints work in a responsive layout.
What an aspect ratio is
An aspect ratio is the proportion of an image's width to its height, written as width:height. A photo that is 1600 pixels wide and 900 pixels tall has a ratio of 16:9. The ratio stays the same when you scale the image up or down. Only the proportion matters, not the pixel count.
Most layout problems with photos come from a mismatch. The photo has one ratio and the space in your layout has another. Something has to give: the photo is stretched, cropped, or surrounded by empty space.
Common ratios and where they fit
| Ratio | Shape | Typical use |
|---|---|---|
| 1:1 | Square | Avatars, product grids, social cards |
| 4:3 | Slightly wide | Article thumbnails, classic camera photos |
| 3:2 | Wide | Content photos from most cameras |
| 16:9 | Wide | Hero banners, video posters |
| 21:9 | Very wide | Cinematic banners on large screens |
| 4:5 | Slightly tall | Portrait cards, mobile feature images |
Pick a small set of ratios for your site and reuse them. A card grid in which every image is 4:3 looks orderly. A grid of mixed ratios looks ragged, and the rows no longer line up.
Matching the photo to the slot
Start from the layout, not from the photo. Ask what shape the slot has at each breakpoint, then choose or crop photos to fit. A wide hero slot needs a photo with horizontal room around the subject. A tall portrait photo forced into a 21:9 banner would lose most of its height.
This is also a selection decision. When you choose a photo for a wide slot, look for one where the subject can sit inside a wide frame without being cut off. The next chapter covers how to crop when the photo and the slot do not match.
Declaring the ratio in CSS
The aspect-ratio property gives an element a fixed shape based on its width. You write the ratio with a slash.
.card img {
width: 100%;
aspect-ratio: 4 / 3;
object-fit: cover;
}The box is always 4:3, however wide the card becomes. The object-fit: cover line makes the photo fill that box and crops any overflow, so the photo is not distorted. Without object-fit, a photo with a different ratio would be stretched to fit.
You can change the ratio at a breakpoint:
.hero img {
width: 100%;
aspect-ratio: 1 / 1;
object-fit: cover;
}
@media (min-width: 800px) {
.hero img {
aspect-ratio: 16 / 9;
}
}On narrow screens the hero is square. From 800 pixels up it becomes 16:9.
Common errors
- Stretched photos. Setting both
widthandheightin CSS withoutobject-fitdistorts the image. Useobject-fit: coverorcontain. - Ragged grids. Mixing ratios in one row of cards. Choose one ratio per grid and crop to it.
- Very tall banners on phones. A 21:9 banner is fine on a desktop, but on a narrow phone it becomes a thin strip. Change the ratio at a breakpoint, or serve a different crop.
- Ratios written with a colon in CSS. CSS requires a slash:
16 / 9, not16:9.
Exercise
A product grid has cards with aspect-ratio: 1 / 1 and object-fit: cover. One supplier photo is a 3:2 landscape image. What happens to it, and why is that acceptable or not?
Show answer
The photo fills the square, and the browser crops the left and right sides because 3:2 is wider than 1:1. It is not distorted. It is acceptable if the product sits near the center. If the product is off to one side, it may be cut off, so you would adjust the crop or choose another photo.