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. It does not cover cropping decisions or focal points. Those come in the next chapters. Here the goal is to understand aspect ratios well enough to choose them on purpose.
What an aspect ratio is
An aspect ratio is the relationship between an image's width and 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 does not depend on size. A 800 by 450 image has the same 16:9 ratio.
You can reduce any size to a ratio by dividing both numbers by their greatest common divisor. You can also work with the decimal form, which is width divided by height. 16:9 is about 1.78, 4:3 is about 1.33, and 1:1 is exactly 1. A value above 1 is landscape, a value below 1 is portrait, and 1 is square.
Aspect ratio matters on the web because a layout is a set of boxes. Every photo ends up in a box, and the box has its own ratio. If the photo's ratio and the box's ratio differ, something has to give: the image is cropped, stretched, or surrounded by empty space. Choosing ratios deliberately lets you decide which of those happens, instead of leaving it to chance.
Common ratios and where they fit
Most web layouts use a small set of ratios. Using a few consistently makes a page look ordered.
| Ratio | Decimal | Typical use |
|---|---|---|
| 1:1 | 1.00 | Avatars, product grids, social previews, mobile hero crops |
| 4:5 | 0.80 | Portrait cards, mobile feeds |
| 3:2 | 1.50 | Native ratio of many cameras, article images, galleries |
| 4:3 | 1.33 | Card thumbnails, older camera and tablet screens |
| 16:9 | 1.78 | Hero banners, video posters, embedded media |
| 21:9 | 2.33 | Wide cinematic banners on large screens |
| 3:1 or wider | 3.00+ | Thin header strips, section dividers |
Cameras often produce 3:2 (many dedicated cameras) or 4:3 (many phones). A page rarely wants exactly that ratio, so you will usually crop. A wide banner needs a much shorter shape than the camera gave you. A card grid wants every image to match so the rows line up.
Source ratio and container ratio
Two ratios are always in play.
- The source ratio belongs to the file you were given.
- The container ratio belongs to the slot in your layout.
When they match, the image fills the slot with no loss. When they differ, you choose a fitting strategy. The sections below compare them.
Crop (fill)
Cropping fills the box completely and discards the parts of the image that fall outside it. The image keeps its true proportions. This is the usual choice for banners, cards, and galleries, because the layout stays tidy. The cost is that you lose some of the photo, so you must be sure the important part survives.
Contain (fit)
Containing shows the entire image inside the box. Unused space remains on two sides. This suits diagrams, logos, product shots on plain backgrounds, and any photo where an edge carries meaning, such as a screenshot. The cost is empty bands, which you can fill with a neutral background color.
Stretch (distort)
Stretching forces the image into the box by scaling width and height independently. Circles become ovals and faces look wrong. It is almost never acceptable for photographs. It happens by accident when you set both width and height in CSS on an img without telling the browser how to fit it.
Setting a ratio in CSS
The aspect-ratio property gives an element a ratio, so its height follows its width.
.card-media {
width: 100%;
aspect-ratio: 4 / 3;
background: #e8e8e8;
}You write the ratio as two numbers separated by a slash. With only the width set, the browser works out the height. A card that is 320 pixels wide becomes 240 pixels tall. If the card is resized, the height tracks it.
Apply the ratio to the container or to the image itself. Putting it on the image also pairs well with object-fit, which controls how the file fills that box. The object-fit and object-position properties get a full treatment in the chapter on CSS cropping. For now, note the two values you will use most:
.card-media img {
width: 100%;
height: 100%;
aspect-ratio: 4 / 3;
object-fit: cover; /* fill the box, crop the overflow */
}
.logo-tile img {
width: 100%;
aspect-ratio: 4 / 3;
object-fit: contain; /* show everything, allow empty space */
}How aspect-ratio interacts with other sizes
aspect-ratio only applies when at least one dimension is automatic. If you set both width and height to fixed values, the ratio is ignored, because the box is already fully determined. This is a common cause of "my aspect-ratio does nothing" reports.
Content can also override the ratio. By default, aspect-ratio acts as a preferred ratio, and a box can grow taller if its content does not fit. Setting min-height: 0 or overflow: hidden on the box prevents that growth when you want the ratio held firm.
For a replaced element such as img, the browser already knows a natural ratio once the file loads. A CSS aspect-ratio on the element overrides that natural ratio for layout.
Choosing one ratio for a set of images
A grid of cards looks calm when every image uses the same ratio. If the source photos have different shapes, do not let each one set its own height. Pick one container ratio, apply it to all of them, and let object-fit: cover crop each photo to fit.
A practical way to choose the ratio:
- List where images appear: hero, article body, cards, thumbnails.
- Give each slot one ratio and name it, for example
--ratio-heroand--ratio-card. - Check that your photos can survive being cropped to that shape. A tall portrait forced into 21:9 may lose the face.
- Keep the number of ratios small. Three or four across a whole site is plenty.
Storing the ratios as custom properties makes them easy to change in one place:
:root {
--ratio-hero: 16 / 9;
--ratio-card: 4 / 3;
--ratio-thumb: 1 / 1;
}
.hero img { aspect-ratio: var(--ratio-hero); width: 100%; object-fit: cover; }
.card img { aspect-ratio: var(--ratio-card); width: 100%; object-fit: cover; }
.thumb img { aspect-ratio: var(--ratio-thumb); width: 100%; object-fit: cover; }Ratios change with the viewport
A ratio that works on a desktop can fail on a phone. A 21:9 banner on a 390-pixel-wide screen is only about 167 pixels tall. A face in it becomes a few dozen pixels across. Common responses:
- Change the ratio at a breakpoint, for example 21:9 on wide screens and 4:3 or 1:1 on narrow ones.
- Let the hero take a taller ratio on mobile so it uses the vertical space that phones have.
- Serve a differently composed file for narrow screens, which is the subject of the chapter on art direction.
A media query handles the first two:
.hero img {
width: 100%;
aspect-ratio: 1 / 1;
object-fit: cover;
}
@media (min-width: 800px) {
.hero img {
aspect-ratio: 21 / 9;
}
}Ratio and the file you export
The ratio of the CSS box and the ratio of the exported file should be close. If you crop an image in CSS from a 3:2 file to 21:9, the browser downloads the whole 3:2 file and throws much of it away. That wastes bandwidth. When a ratio is fixed and the image is important, consider exporting the file at or near that ratio.
Also note that the ratio of the file is part of how much the browser needs to reserve space before the image loads. Declaring width and height attributes on the img gives the browser that ratio early. The chapter on layout shift covers this in detail.
Common errors
- Stretched photos. Setting both
widthandheightin CSS withoutobject-fit. Fix it by addingobject-fit: coverorcontain. - Ignored
aspect-ratio. A fixed height on the same element cancels it. Remove the fixed dimension. - Mixed ratios in a grid. Rows with uneven heights. Apply one shared ratio and crop.
- Guessing the decimal. Writing
aspect-ratio: 16 9without the slash is invalid in many contexts. Write16 / 9. - Cropping before checking the subject. A ratio chosen from the layout alone can cut off a face. Always preview real photos.
- Over-wide mobile banners. A very wide ratio on a narrow screen leaves a sliver. Change the ratio at a breakpoint.
Summary
An aspect ratio is a shape, independent of size. Every image has a source ratio, every slot has a container ratio, and your job is to decide how the two meet: crop, contain, or, almost never, stretch. Use a small set of named ratios, apply them with aspect-ratio, and adjust them at breakpoints so the shape suits each screen.
Exercise
A card grid shows photos in a box whose CSS reads width: 100%; height: 200px; aspect-ratio: 4 / 3;. The box is 400 pixels wide. What height does it get, and how would you make the 4:3 ratio take effect? Also name the object-fit value you would use so photos of any shape fill the box without distortion.
Show answer
The box is 200 pixels tall, because a fixed height means the box is fully determined and aspect-ratio is ignored. Remove the fixed height (or set it to auto) so the height is 300 pixels, which is 400 divided by 4 and times 3. Use object-fit: cover on the image so it fills the box and crops the overflow instead of stretching.
Exercise
A hero image uses aspect-ratio: 21 / 9 on all screens. On a phone 390 pixels wide, how tall is it, and what change would you make for narrow screens?
Show answer
Height is 390 divided by 21, times 9, which is about 167 pixels. That is too short for most subjects. Use a media query so narrow screens use a taller ratio such as 4 / 3 or 1 / 1, and apply 21 / 9 only from a wider breakpoint such as min-width: 800px.