What is the CSS aspect-ratio Property?
The aspect-ratio CSS property sets a preferred aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions. It defines the desired width-to-height ratio of an element's box, allowing the browser to automatically adjust dimensions while maintaining consistent proportions.
At least one of the box's sizes needs to be automatic for aspect-ratio to take effect. If neither the width nor height is set to auto, the provided aspect ratio has no effect on the box's preferred sizes.
Why aspect-ratio Matters for Performance
One of the most significant performance benefits of using aspect-ratio is its role in preventing Cumulative Layout Shift (CLS), a Core Web Vital metric. By declaring aspect-ratio upfront, browsers can reserve the correct amount of space before content loads, eliminating layout shifts entirely.
For responsive web design projects, this means smoother user experiences and better SEO performance, as search engines increasingly prioritize page stability in their ranking algorithms. Combined with CSS Grid layouts, aspect-ratio creates powerful responsive systems that scale gracefully across all devices.
Browser Support Since 2021
97%
Global Browser Support
3
Major Browsers Supported
0
JavaScript Dependencies
Syntax and Values
Basic Syntax
The aspect-ratio property accepts several value types:
/* Single ratio value */
aspect-ratio: 16 / 9;
/* Single number (equivalent to width / 1) */
aspect-ratio: 1.5;
/* Auto value for replaced elements */
aspect-ratio: auto;
/* Combined auto and ratio */
aspect-ratio: auto 3 / 4;
Understanding Ratio Values
The <ratio> value represents the width-to-height ratio, expressed as two positive numbers separated by a forward slash or a single number.
The following declarations are all equivalent:
aspect-ratio: 3 / 6;
aspect-ratio: 1 / 2;
aspect-ratio: 0.5 / 1;
aspect-ratio: 0.5;
The auto Keyword
For replaced elements with an intrinsic aspect ratio (like images), auto uses the natural aspect ratio of the content. For non-replaced elements without intrinsic proportions, auto means no preferred aspect ratio is applied.
When working with modern CSS techniques, understanding how auto combines with ratio values gives you precise control over both replaced and non-replaced elements in your layouts.
1/* Single ratio value */2aspect-ratio: 16 / 9;3 4/* Single number (equivalent to width / 1) */5aspect-ratio: 1.5;6 7/* Auto value for replaced elements */8aspect-ratio: auto;9 10/* Combined auto and ratio */11aspect-ratio: auto 3 / 4;12 13/* Common aspect ratio shortcuts */14aspect-ratio: 1 / 1; /* Square */15aspect-ratio: 4 / 3; /* Standard photo */16aspect-ratio: 16 / 9; /* Widescreen video */17aspect-ratio: 21 / 9; /* Ultrawide */aspect-ratio for Replaced Elements
Images and Intrinsic Ratios
Replaced elements like <img> and <video> have intrinsic aspect ratios determined by their actual content. When you want to override or constrain these natural proportions while maintaining consistency, aspect-ratio becomes particularly useful.
For images, setting only one dimension (width or height) causes the browser to automatically calculate the other while preserving the intrinsic ratio:
img {
width: 100%;
height: auto; /* Maintains aspect ratio */
}
Preventing Image Distortion
Setting both width and height explicitly on replaced elements risks distortion. Using aspect-ratio with object-fit provides a better approach:
img {
aspect-ratio: 16 / 9;
width: 100%;
object-fit: cover;
}
Videos and Embeds
Video elements and iframe embeds benefit significantly from aspect-ratio declarations:
.video-container {
aspect-ratio: 16 / 9;
width: 100%;
}
.video-container iframe {
width: 100%;
height: 100%;
}
This technique eliminates the need for the traditional padding-bottom hack while providing the same responsive behavior with cleaner code. When building Next.js applications, this approach integrates seamlessly with the Image component for optimized media loading. For more on responsive media techniques, explore our guide on styling and animating SVGs with CSS.
aspect-ratio for Non-Replaced Elements
Creating Consistent Card Layouts
Non-replaced elements like <div> and <section> don't have intrinsic aspect ratios, making aspect-ratio particularly powerful for creating uniform card layouts, pricing tables, and feature grids:
.card {
aspect-ratio: 3 / 4;
display: flex;
flex-direction: column;
}
.card-content {
flex: 1;
}
Grid-Based Responsive Layouts
In CSS Grid layouts, aspect-ratio works seamlessly to create responsive galleries:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
.gallery-item {
aspect-ratio: 1;
background: #f0f0f0;
}
Modern UI Component Uses
- Avatar images: 1:1 ratio
- Thumbnail previews: 16:9 or 4:3 ratios
- Feature icons within cards
- Button icon containers
- Hero section backgrounds
When combined with modern CSS techniques, aspect-ratio ensures all cards maintain consistent proportions regardless of their content, creating visually harmonious layouts without fixed heights. For advanced CSS optimization strategies, see our guide on refactoring CSS and optimizing size performance.
Performance Benefits
Cumulative Layout Shift Prevention
Cumulative Layout Shift (CLS) measures visual stability during page loading. The aspect-ratio property directly addresses this by reserving space before content loads.
For Next.js applications, this is especially important:
import Image from 'next/image';
function ProductCard({ product }) {
return (
<div className="product-card">
<div className="image-container" style={{ aspectRatio: '4/3' }}>
<Image
src={product.image}
alt={product.name}
fill
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
/>
</div>
<h3>{product.name}</h3>
</div>
);
}
Reduced JavaScript Dependencies
Before aspect-ratio, developers relied on JavaScript solutions or the padding-bottom hack. The aspect-ratio property eliminates these workarounds, reducing JavaScript execution time and improving page load performance. This aligns with modern web performance optimization best practices that prioritize Core Web Vitals. For comprehensive performance strategies, explore our guide on CSS browser support to ensure your optimized layouts work across all browsers.
Common Use Cases and Examples
Responsive Image Cards
.card {
border: 1px solid #e0e0e0;
border-radius: 8px;
overflow: hidden;
transition: transform 0.2s ease;
}
.card:hover {
transform: translateY(-4px);
}
.card-image-container {
width: 100%;
aspect-ratio: 4 / 3;
position: relative;
}
.card-image-container img {
width: 100%;
height: 100%;
object-fit: cover;
}
Responsive Video Embeds
.video-wrapper {
width: 100%;
aspect-ratio: 16 / 9;
background: #000;
border-radius: 8px;
overflow: hidden;
}
.video-wrapper iframe {
width: 100%;
height: 100%;
border: none;
}
Icon Grids
.icon-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
gap: 1.5rem;
}
.icon-item {
display: flex;
flex-direction: column;
align-items: center;
aspect-ratio: 1;
padding: 1rem;
background: #f8f9fa;
border-radius: 12px;
}
Best Practices
Choose the Right Ratio for Your Use Case
Different content types benefit from different aspect ratios:
| Ratio | Use Case |
|---|---|
| 16:9 | Standard video, widescreen images |
| 4:3 | Traditional photography, documents |
| 3:2 | Standard photography, portrait images |
| 1:1 | Avatars, profile pictures, thumbnails |
| 3:4 | Portrait photography, vertical cards |
Combine with object-fit for Media
For replaced elements, always pair aspect-ratio with object-fit to control how content fills the designated space.
Use for Container Reserve Space
Apply aspect-ratio to containers holding media that loads dynamically to reserve space and prevent layout shift.
Mobile-First Responsive Ratios
Consider using different aspect ratios for different viewport sizes:
.card {
aspect-ratio: 16 / 9;
}
@media (max-width: 600px) {
.card {
aspect-ratio: 4 / 3;
}
}
Performance First
Leverage aspect-ratio as a performance optimization tool. Reserve space for all media elements, especially above-the-fold content, to improve Core Web Vitals scores. This approach is essential for professional web development that delivers exceptional user experiences. For more on creating visually appealing interfaces, see our guide on CSS gradient background animations.
Browser Compatibility and Fallbacks
Current Support
The aspect-ratio property has excellent browser support:
- Chrome 88+ (January 2021)
- Firefox 89+ (June 2021)
- Safari 15+ (September 2021)
- Edge 88+ (January 2021)
This represents essentially 97% of global browser usage, making aspect-ratio safe for production use without fallback requirements.
Feature Detection
For environments requiring legacy browser support, use @supports to provide fallback styles:
.card-image {
width: 100%;
height: 0;
padding-bottom: 75%; /* Fallback for 4:3 ratio */
}
@supports (aspect-ratio: 4 / 3) {
.card-image {
aspect-ratio: 4 / 3;
height: auto;
padding-bottom: 0;
}
}
However, given the widespread support, most projects can safely use aspect-ratio without fallbacks.
Why you should use aspect-ratio in your projects
CLS Prevention
Reserve space for media before it loads to prevent layout shifts and improve Core Web Vitals scores.
Clean Code
Eliminate the padding-bottom hack and JavaScript workarounds with native CSS solution.
Responsive Design
Create consistent proportions across all viewport sizes without complex calculations.
Excellent Support
97% browser support since 2021 makes it safe for production use without fallbacks.
Frequently Asked Questions
What is the default value of aspect-ratio?
The default value is `auto`. For replaced elements with an intrinsic aspect ratio, this uses the natural aspect ratio. For non-replaced elements, no preferred aspect ratio is applied.
Does aspect-ratio work on all elements?
No, aspect-ratio does not apply to inline boxes and internal ruby or table boxes. It works on most block-level and replaced elements like div, section, img, video, and iframe.
How does aspect-ratio interact with min-height and max-height?
aspect-ratio sets a preferred size that can be constrained by min-height, max-height, min-width, and max-width. The final size is determined by considering all these constraints together.
Can I use aspect-ratio with flexbox and grid?
Yes, aspect-ratio works seamlessly with both flexbox and grid layouts. It's particularly useful for creating consistent card sizes in grid layouts.