CSS Aspect Ratio
In this page:
selector {
aspect-ratio: width / height;
}
Basic Syntax of aspect-ratio
The aspect-ratio property accepts a width and height separated by a forward slash, and the browser computes the missing dimension from whichever one is set. This means you only ever need to set a width or a height, not both, and the browser handles the rest.
- You can write aspect-ratio: 1 as shorthand for a perfect square, equivalent to aspect-ratio: 1 / 1.
- Accepted values:
- ratio — width / height, e.g. 16 / 9
- number — single number such as 1 for a square
- auto — uses the element's natural ratio (default)
- auto ratio — natural ratio if available, else the given ratio
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Basic Syntax of aspect-ratio
<style>
.box {
width: 300px;
aspect-ratio: 16 / 9;
background: steelblue;
color: white;
}
</style>
<div class="box">Height computed automatically from the width</div>
16 by 9 Video Containers
16 by 9 is the standard widescreen video ratio, and aspect-ratio makes embedding responsive video players simple without wrapper divs or padding tricks. Before this property existed, developers relied on a padding-top percentage hack to achieve the same responsive effect.
- Set width: 100% alongside aspect-ratio: 16 / 9 on an iframe so the video fills its parent's width and derives height automatically.
- Accepted values:
- aspect-ratio: 16 / 9 — widescreen video
- aspect-ratio: 4 / 3 — classic monitor
- width: 100% — the height follows automatically
- height: auto — needed so the ratio can apply
Example: 16 by 9 Video Containers
<style>
iframe {
width: 100%;
aspect-ratio: 16 / 9;
}
</style>
<iframe src="about:blank"></iframe>
Square Images in a Gallery
A 1 by 1 ratio keeps thumbnail grids visually consistent even when source photos come in different original sizes. This avoids the layout jump that happens when images of varying dimensions load into fixed-size grid cells.
- Combine aspect-ratio: 1 with object-fit: cover on the inner img so every thumbnail is both square and uncropped-looking.
- Accepted values:
- aspect-ratio: 1 / 1 — square
- aspect-ratio: 1 — same as 1 / 1
- object-fit: cover — fills the square without distortion
- width: 100% — the height follows
Example: Square Images in a Gallery
<style>
img {
aspect-ratio: 1;
object-fit: cover;
width: 150px;
}
</style>
<img src="https://placehold.co/300x200" alt="Cropped to a perfect square">
Responsive Boxes That Hold Their Shape
aspect-ratio is not limited to media, any box such as a card, banner, or map placeholder can hold a consistent shape as its width changes on different screens. This makes it a general-purpose layout tool, not just an image-specific property.
- Use aspect-ratio on skeleton loading placeholders so the page layout does not jump once real content replaces them.
- Accepted values:
- aspect-ratio: <ratio> — height derived from the width
- max-width: <length> — caps the box
- width: <percentage> — flexible width
- min-height — overrides the ratio if content is taller
Example: Responsive Boxes That Hold Their Shape
<style>
.card {
width: 100%;
aspect-ratio: 4 / 3;
background: steelblue;
color: white;
}
</style>
<div class="card">Holds its shape as width changes</div>
Falling Back Before aspect-ratio Existed
Older codebases used the padding-bottom percentage trick, applying padding-bottom as a percentage of width on an empty element to force a ratio, since percentage padding is always calculated from width.
- When supporting very old browsers, keep the padding-bottom hack as a fallback alongside a modern aspect-ratio declaration.
- Accepted values:
- aspect-ratio: 16 / 9 — modern approach
- padding-top: 56.25% — old padding hack for 16 / 9
- position: relative — on the wrapper for the hack
- position: absolute — for the inner content in the hack
Example: Falling Back Before aspect-ratio Existed
<style>
.ratio-box {
position: relative;
padding-bottom: 56.25%;
background: #ddd;
}
.ratio-box > * {
position: absolute;
inset: 0;
background: steelblue;
color: white;
}
</style>
<div class="ratio-box">
<div>The old padding-bottom percentage trick</div>
</div>
- Setting both aspect-ratio and an explicit height, an explicit height always overrides the ratio and the property has no visible effect.
- Writing the ratio backwards, aspect-ratio: 9 / 16 produces a tall portrait box instead of a wide landscape one.
- Forgetting that aspect-ratio only reserves shape, it does not automatically crop or fit media, you still need object-fit for images inside it.
- aspect-ratio: width / height locks a box's proportions so height is derived automatically from its width.
- It replaces the old padding-bottom percentage hack for responsive video and image containers.
- Combine aspect-ratio with object-fit: cover on inner media to keep both the box shape and the image proportions correct.
aspect-ratio is supported by all modern browsers since 2021, older browsers ignore it silently and fall back to the natural height of the content.
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep