Aspect Ratio
Control element proportions with CSS aspect-ratio property. Create consistent, responsive layouts with perfect dimensions.
How does aspect-ratio work in CSS?
aspect-ratio keeps an element's width and height in a fixed proportion. Set the width (or let it fill its container) and the browser calculates the height: aspect-ratio: 16 / 9 for video embeds, aspect-ratio: 1 for squares. It replaces the old padding-top percentage hack.
.video { width: 100%; aspect-ratio: 16 / 9; }
.avatar { width: 64px; aspect-ratio: 1; border-radius: 50%; }
.thumb { aspect-ratio: 4 / 3; object-fit: cover; width: 100%; }Interactive Aspect Ratio Demo
Code Editor
<div class="video">16 / 9</div>
<div class="square">1</div>
<img class="thumb" src="https://picsum.photos/id/1060/600/400" alt="Coffee">Preview
Aspect Ratio Fundamentals
Basic Syntax and Usage
/* Basic aspect ratio syntax */ .element {
aspect-ratio: 16 / 9; /* Width to height ratio */ }
/* Alternative syntax */ .element {
aspect-ratio: 1.777; /* Decimal notation (16/9) */ }
/* Square aspect ratio */ .square {
aspect-ratio: 1; /* Same as 1 / 1 */ }
/* Portrait orientation */ .portrait {
aspect-ratio: 3 / 4; /* Narrower than tall */ }
/* Auto keyword (uses intrinsic ratio) */ .auto {
aspect-ratio: auto; /* Default behavior */ }The aspect-ratio property defines the preferred width-to-height ratio for an element.
Common Aspect Ratios
/* Video and media ratios */ .video-16-9 { aspect-ratio: 16 / 9; } /* Widescreen */ .video-4-3 { aspect-ratio: 4 / 3; } /* Traditional TV */ .cinema { aspect-ratio: 21 / 9; } /* Ultra-wide */
/* Image ratios */ .square { aspect-ratio: 1 / 1; } /* Instagram posts */ .photo { aspect-ratio: 4 / 3; } /* Digital cameras */ .landscape { aspect-ratio: 3 / 2; } /* Standard photo */
/* UI component ratios */ .card { aspect-ratio: 5 / 4; } /* Slightly wide */ .portrait-card { aspect-ratio: 3 / 4; } /* Mobile cards */ .banner { aspect-ratio: 8 / 3; } /* Header banners */ .golden { aspect-ratio: 1.618 / 1; } /* Golden ratio */Use standard ratios for consistency and visual appeal
Replacing Padding Hacks
/* Old method: padding hack */ .old-method {
position: relative;
height: 0;
padding-bottom: 56.25%; /* 16:9 = 9/16 = 0.5625 */ }
.old-method > * {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
/* Modern method: aspect-ratio */ .modern-method {
aspect-ratio: 16 / 9;
}
/* Much simpler and more intuitive! */ .container {
width: 100%;
aspect-ratio: 16 / 9;
}Modern approach eliminates complex positioning and calculations
Responsive Applications
Responsive Media Containers
/* Responsive video container */ .video-wrapper {
width: 100%;
max-width: 800px;
aspect-ratio: 16 / 9;
background: #000;
border-radius: 8px;
overflow: hidden;
}
.video-wrapper iframe,
.video-wrapper video {
width: 100%;
height: 100%;
border: none;
}
/* Responsive image gallery */ .image-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
.image-item {
aspect-ratio: 1 / 1;
overflow: hidden;
border-radius: 8px;
}
.image-item img {
width: 100%;
height: 100%;
object-fit: cover;
}Perfect for creating responsive media that maintains proportions across all devices.
Card Layouts
/* Consistent card proportions */ .card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}
.card {
aspect-ratio: 3 / 4; /* Portrait cards */ background: white;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
overflow: hidden;
display: flex;
flex-direction: column;
}
.card-image {
flex: 1;
background-size: cover;
background-position: center;
}
.card-content {
padding: 1rem;
flex-shrink: 0;
}
/* Landscape cards for wider layouts */ @media (min-width: 768px) {
.card.landscape {
aspect-ratio: 4 / 3;
}
}Create consistent card layouts that scale beautifully
Hero Sections
/* Hero banner with consistent proportions */ .hero {
aspect-ratio: 8 / 3; /* Wide banner format */ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
position: relative;
overflow: hidden;
}
.hero-content {
z-index: 2;
max-width: 600px;
padding: 2rem;
}
/* Responsive hero ratios */ @media (max-width: 768px) {
.hero {
aspect-ratio: 4 / 3; /* Taller on mobile */ }
}
@media (min-width: 1200px) {
.hero {
aspect-ratio: 12 / 3; /* Ultra-wide on desktop */ }
}Responsive hero sections that adapt to screen sizes
Advanced Techniques
Dynamic Aspect Ratios
/* CSS custom properties for dynamic ratios */ :root {
--card-ratio: 3 / 4;
--video-ratio: 16 / 9;
--banner-ratio: 8 / 3;
}
.dynamic-container {
aspect-ratio: var(--card-ratio);
}
/* Conditional aspect ratios with CSS */ .adaptive-container {
aspect-ratio: 16 / 9;
}
@media (orientation: portrait) {
.adaptive-container {
aspect-ratio: 3 / 4;
}
}
/* Container query with aspect ratio */ .responsive-layout {
container-type: inline-size;
}
@container (max-width: 400px) {
.responsive-item {
aspect-ratio: 1 / 1;
}
}
@container (min-width: 401px) {
.responsive-item {
aspect-ratio: 16 / 9;
}
}Create dynamic aspect ratios that adapt to different contexts and conditions.
Fallbacks & Browser Support
/* Progressive enhancement with fallbacks */ .aspect-container {
/* Fallback for older browsers */ height: 200px;
}
@supports (aspect-ratio: 1) {
.aspect-container {
height: auto;
aspect-ratio: 16 / 9;
}
}
/* Feature query for aspect-ratio */ .modern-layout {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.grid-item {
background: #f3f4f6;
min-height: 150px; /* Fallback height */ }
@supports (aspect-ratio: 1) {
.grid-item {
min-height: unset;
aspect-ratio: 4 / 3;
}
}Ensure compatibility with graceful fallbacks for older browsers
Performance Considerations
/* Efficient aspect ratio implementations */ .optimized-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
.optimized-item {
aspect-ratio: 1 / 1;
/* Avoid layout shifts */ contain: layout style paint;
}
/* Prevent content overflow */ .content-container {
aspect-ratio: 16 / 9;
overflow: hidden;
display: flex;
flex-direction: column;
}
/* Lazy loading optimization */ .lazy-image {
aspect-ratio: 4 / 3;
background: #f3f4f6;
display: flex;
align-items: center;
justify-content: center;
}
.lazy-image img {
width: 100%;
height: 100%;
object-fit: cover;
}Optimize performance with efficient layouts and contain properties
Best Practices & Browser Support
Best Practices
- • Use standard aspect ratios for familiarity (16:9, 4:3, 1:1)
- • Combine with object-fit for images and videos
- • Test layouts across different content lengths
- • Consider orientation changes on mobile devices
- • Use CSS custom properties for consistency
- • Provide fallbacks for older browser support
Browser Support
- • Chrome 88+ (January 2021)
- • Firefox 89+ (June 2021)
- • Safari 15+ (September 2021)
- • Edge 88+ (January 2021)
- • Use @supports for feature detection
- • Good support in modern browsers (~95%)
CSS aspect-ratio — step by step
aspect-ratio Syntax
aspect-ratio: 16 / 9 makes an element maintain 16:9 proportions — set only the width and the height is calculated automatically.
🎯 Quick Check
Which aspect-ratio value creates a perfect square element?
CSS aspect-ratio: summary
Key points
- If both width and height are set explicitly,
aspect-ratiois ignored. - Content taller than the ratio allows will stretch the box unless you add
overflow: hiddenor amin-height: 0. - On images, combine it with
object-fit: coverto crop instead of distorting. - Setting
widthandheightattributes on<img>gives the browser an automatic aspect ratio, which prevents layout shift while the image loads.
Frequently asked questions
›How do I make a responsive 16:9 video?
Give the iframe or video width: 100%; height: auto; aspect-ratio: 16 / 9;.
›Why is aspect-ratio not working?
Usually both width and height are set, or the content is taller than the ratio allows. Remove the fixed height and check for overflowing content.