CSS aspect-ratio Property Guide
The CSS aspect-ratio property defines the preferred width-to-height ratio of an element. It replaced the padding-top hack for responsive containers and is now supported in all modern browsers.
Aspect Ratio Calculator
Calculate, convert, and resize aspect ratios for images, video, and responsive design.
Enter Dimensions
Result
aspect-ratio: 16 / 9;Common Aspect Ratios
Equivalent Sizes at 16:9
| Width | Height | Pixels | Label |
|---|---|---|---|
| 320 | 180 | 0.1 MP | |
| 480 | 270 | 0.1 MP | |
| 640 | 360 | 0.2 MP | |
| 768 | 432 | 0.3 MP | |
| 960 | 540 | 0.5 MP | |
| 1024 | 576 | 0.6 MP | |
| 1280 | 720 | 0.9 MP | 720p / HD |
| 1440 | 810 | 1.2 MP | |
| 1600 | 900 | 1.4 MP | |
| 1920 | 1080 | 2.1 MP | 1080p / Full HD |
| 2560 | 1440 | 3.7 MP | 1440p / 2K |
| 3840 | 2160 | 8.3 MP | 2160p / 4K UHD |
Syntax and basic usage
The property accepts a ratio: aspect-ratio: 16 / 9. The element will try to maintain this ratio as its size changes. Set width: 100% and the height adjusts automatically. For square elements: aspect-ratio: 1 / 1 (or just aspect-ratio: 1). You can also use decimal values: aspect-ratio: 1.7778. The auto keyword (aspect-ratio: auto) uses the element's intrinsic ratio — useful for images that should keep their natural proportions with auto as a fallback: aspect-ratio: auto 16 / 9.
/* CSS aspect-ratio property */
/* Basic usage */
.video { aspect-ratio: 16 / 9; width: 100%; }
.square { aspect-ratio: 1; width: 200px; }
.photo { aspect-ratio: 4 / 3; }
/* Responsive video embed */
.video-wrapper {
aspect-ratio: 16 / 9;
width: 100%;
max-width: 800px;
}
.video-wrapper iframe {
width: 100%;
height: 100%;
}
/* Card with fixed ratio */
.card-image {
aspect-ratio: 3 / 2;
object-fit: cover;
width: 100%;
}
/* Browser support: 95%+ (Chrome 88+, Firefox 89+, Safari 15+) */
/* Fallback: padding-top hack */
.fallback { padding-top: 56.25%; /* 9/16 = 0.5625 */ }Common patterns
Responsive video container: .video-container { aspect-ratio: 16 / 9; width: 100%; } with the iframe or video filling it via position: absolute and inset: 0. Image card grid: set aspect-ratio: 3 / 2 on the img element with object-fit: cover to create uniform image grids. Hero section: aspect-ratio: 21 / 9 for a cinematic banner that scales with viewport width. Avatar circle: aspect-ratio: 1 with border-radius: 50%.
Browser support and fallbacks
aspect-ratio is supported in Chrome 88+, Firefox 89+, Safari 15+, and Edge 88+ (since early 2021). For older browsers, use the padding-top fallback: .container { position: relative; padding-top: 56.25%; } .container > * { position: absolute; inset: 0; }. The 56.25% comes from 9/16 × 100. Use @supports (aspect-ratio: 1) { ... } to progressively enhance. For images, setting width and height HTML attributes gives the browser the intrinsic ratio for layout calculation even before CSS loads.
Frequently Asked Questions
Can I use aspect-ratio on images?
Yes. Setting aspect-ratio on an img element reserves the correct space in the layout before the image loads, preventing Cumulative Layout Shift (CLS). Combine with object-fit: cover or contain to control how the image fills the box. Setting width and height HTML attributes achieves the same layout reservation.
What is the padding-top aspect ratio hack?
Before the aspect-ratio CSS property existed, developers used padding-top as a percentage of the width to create ratio-locked containers. For 16:9: padding-top: 56.25% (9 ÷ 16 × 100). Content inside uses position: absolute. This hack is no longer needed in modern browsers but still works as a fallback.
Does aspect-ratio work with flexbox and grid?
Yes. The aspect-ratio property works in flex items and grid items. In flex layouts, set flex-shrink: 0 if you want the item to maintain its ratio without being squished. In grid, use min-height: 0 on the item to allow the ratio to take effect when the grid cell has an implicit size.
Related Inspect Tools
Code Complexity Analyzer
Analyze JavaScript and TypeScript code for cyclomatic complexity, cognitive complexity, nesting depth, and maintainability index with per-function metrics
GitHub Actions Validator
Validate GitHub Actions workflow YAML files for syntax, triggers, job structure, step config, needs dependencies, and deprecated actions
XPath Tester
Test XPath expressions against XML data with real-time evaluation — select elements, filter by attributes, navigate axes, and use XPath functions
SQL Playground
Run SQL queries in your browser with a full SQLite database powered by WebAssembly — practice JOINs, CTEs, window functions, aggregations, and more