CSS Aspect Ratio Property: How to Use It
In this article
The CSS aspect-ratio property lets you set a fixed width-to-height ratio for an element, so it automatically adjusts its size to maintain that proportion. This is incredibly useful for responsive images, video embeds, cards, and placeholders—especially when you don't know the exact dimensions in advance. With aspect-ratio, you can avoid the classic "layout shift" problem and create consistent, flexible designs without JavaScript or padding hacks. All you need is a modern browser (Chrome 88+, Firefox 87+, Safari 15+), and you're ready to go.
What You'll Need
- A basic understanding of HTML and CSS (selectors, properties, values).
- A code editor or any text editor (like VS Code, Sublime Text, or even Notepad).
- A modern web browser (Chrome, Firefox, Safari, or Edge) to test your code.
- An HTML file and a CSS file (or a
<style>block in your HTML).
Step-by-Step
- Create an HTML element. Start with a simple element like a
<div>,<img>, or<video>. For example:<div class="box"></div>. - Give it a width (or height). The
aspect-ratioproperty works best when one dimension is set. If you set a width, the height will be calculated automatically. If you set a height, the width will adjust. If you set neither, the element will use its content's natural size, and the ratio may not apply as expected. - Apply the
aspect-ratioproperty. In your CSS, write:.box { aspect-ratio: 16 / 9; width: 100%; }. This sets a 16:9 ratio. The slash and spaces are optional;aspect-ratio: 16/9works too. - Understand the syntax. The value can be a ratio (like
1 / 1for a square) or a decimal number (like1.5for 3:2). You can also use the keywordautoto fall back to the element's natural size. - Test with different sizes. Try setting
width: 50%ormax-width: 400pxand see how the height changes. The element will always maintain the ratio you specified. - Use with images and videos. For a responsive video embed, wrap the iframe in a container with
aspect-ratio: 16/9and set the iframe towidth: 100%; height: 100%. This keeps the video's ratio intact on any screen size. - Combine with other properties. You can use
object-fit: coveron an<img>to crop it nicely within the ratio, or useoverflow: hiddenon a container to clip content that doesn't fit.
Common Mistakes
- Not setting a width or height. If the element has no explicit size and no content, it may collapse to zero height. Always set at least one dimension.
- Using
aspect-ratioon inline elements. The property only works on block-level or inline-block elements. If you apply it to a plain<span>, it won't work unless you setdisplay: blockordisplay: inline-block. - Expecting it to force a ratio on replaced elements. For
<img>and<video>, the browser uses the intrinsic dimensions if no width or height is set. You need to explicitly set one dimension for the ratio to take effect. - Forgetting about content overflow. If the content inside the element is larger than the computed height, it will overflow unless you set
overflow: hiddenor useobject-fitfor media.
Tips
- Use the
autokeyword wisely. You can writeaspect-ratio: auto 16/9to let the element use its natural ratio if it has one (like an image), and fall back to 16:9 otherwise. - Combine with
min-heightormax-height. If you need the element to not get too small or too large, set min/max constraints along with the ratio. - Use it for placeholder boxes. When lazy-loading images, a placeholder with the same aspect ratio prevents layout shift, improving Core Web Vitals.
- Remember that
aspect-ratiois not a replacement for responsive images. It only controls the box size, not the actual image resolution. Usesrcsetfor that.
Troubleshooting
- The element has no height. Check that you've set a width (or height). If you want the element to size based on content, you may need to use a different approach (like padding-top hack) for older browsers.
- The ratio isn't being applied. Make sure the element is not inline. Add
display: blockordisplay: inline-blockto the element. - The element is too tall or too short. This can happen if you have both width and height set, or if the content has a min-height. Remove any conflicting height declarations, or use
min-height: 0to allow the ratio to work. - It works in some browsers but not others. Check that you're using a browser that supports
aspect-ratio. For older browsers, you can use the padding-top hack as a fallback: setheight: 0; padding-top: 56.25%for 16:9. - Images are distorted. If you're using an
<img>with a set ratio but the image doesn't fit, addobject-fit: coverto crop it properly, orobject-fit: containto show the whole image with letterboxing.
Frequently Asked Questions
Can I use aspect-ratio with CSS Grid or Flexbox?
Yes, it works fine. The ratio applies to the element's box, and grid/flex will respect that size as long as you don't override it with other sizing properties.
Does aspect-ratio work on replaced elements like images?
Yes, but you need to set one dimension (width or height) explicitly. If you don't, the browser uses the image's intrinsic dimensions and the ratio may be ignored.
What's the difference between aspect-ratio and the padding-top hack?
The padding-top hack uses a percentage padding to create a ratio, but it requires an extra wrapper and can be tricky. aspect-ratio is cleaner and more direct, but the hack is a good fallback for older browsers.
Can I animate aspect-ratio?
Yes, it's animatable in most modern browsers. You can transition from one ratio to another, though the effect may be jarring if the ratio changes drastically.
Does aspect-ratio affect the content inside?
Not directly. It only sets the box size. Content inside will flow normally, and if it overflows, you'll need to handle it with overflow or object-fit.
Can I use a decimal like 1.5 instead of a ratio?
Yes, aspect-ratio: 1.5 is equivalent to 3/2. It's just a different way to express the same ratio.