- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Aspect Ratio & Object Fit
CSS Layout
Aspect Ratio & Object Fit
Reserve stable media space with aspect-ratio and control how images and video fit and align inside their boxes. You will reserve stable media space and compare cover, contain, fill, scale-down, and object-position for images and video.
Two properties for images and videos that do not fit their box. aspect-ratio gives the box a fixed shape, like 16:9. object-fit decides what the picture does inside that shape: crop to fill it, or shrink to show everything.
Together they keep a grid of thumbnails neat even when the photos have different shapes, and they stop the page from jumping while images load. Sizing basics are in CSS Width and Height, and fitting media into layouts in Responsive Layout.
A tidy thumbnail
Example
css
.thumbnail {
aspect-ratio: 16 / 9;
width: 100%;
overflow: hidden;
border-radius: 0.75rem;
}
.thumbnail img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 35%;
}The box is always 16:9, whatever the photo's own shape. The image fills it with object-fit: cover, so it is cropped instead of squashed, and object-position: 50% 35% keeps the crop a little above the middle, where faces usually are.
aspect-ratio only applies when at least one side is automatic. If you set both width and height, they win.
object-fit values
- cover fills the whole box and crops what does not fit. Best for thumbnails and banners.
- contain shows the whole image and leaves empty space around it. Best for logos and product photos.
- fill stretches the image to the box, which distorts it. This is the default for
<img>, and the reason photos look squashed. - none keeps the original size and crops.
- scale-down picks whichever of
noneorcontainis smaller.
Pixel art and diagrams
Example
css
.pixel-preview {
image-rendering: crisp-edges;
}When a small pixel image is enlarged, browsers blur it. image-rendering: crisp-edges (or pixelated) keeps the hard edges. Do not use it on photos; they look worse.
Replaced Element Properties
| Property | Purpose | Example |
|---|---|---|
| aspect-ratio | Supplies a preferred width-to-height ratio. | aspect-ratio: 16 / 9 |
| object-fit | Controls how replaced content fits its box. | object-fit: cover |
| object-position | Positions replaced content inside its box. | object-position: center top |
| image-rendering | Controls the scaling algorithm used for an image. | image-rendering: crisp-edges |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Aspect Ratio & Object Fit example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.thumbnail {
aspect-ratio: 16 / 9;
width: 100%;
overflow: hidden;
border-radius: 0.75rem;
}
.thumbnail img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 35%;
}
.pixel-preview {
image-rendering: crisp-edges;
}
</style>
</head>
<body>
<main class="thumbnail"><img alt="Decorative landscape" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 800 450'%3E%3Crect width='800' height='450' fill='%230ea5e9'/%3E%3Ccircle cx='620' cy='100' r='60' fill='%23fde68a'/%3E%3Cpath d='M0 360L220 180L390 340L560 220L800 390V450H0Z' fill='%230f766e'/%3E%3C/svg%3E"></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| aspect-ratio | 88 | 88 | 89 | 15 |
| object-fit | 32 | 79 | 36 | 10 |
| image-rendering: crisp-edges | 148 | 148 | 65 | 7 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Good to know
These properties control how an image looks, not how big the file is. A 4000-pixel photo shown in a 300-pixel thumbnail still downloads 4000 pixels. Use srcset or <picture> to serve smaller files.
Aspect Ratio & Object Fit exercises
Widescreen
Give the video box the ratio 16 / 9.
The HTML your code works on
<div class="video">Video</div>
<img class="pic" src="https://picsum.photos/id/237/600/300.jpg" alt="Dog">Result
