CSS Aspect Ratio Generator
Generate an aspect-ratio rule for any ratio, with the percentage padding-top fallback worked out for you and wrapped in a feature query.
Loading tool…
The CSS is generated by JavaScript in your browser and never leaves the page. There is nothing to upload and nothing is stored.
What the CSS Aspect Ratio Generator does
Use this CSS aspect ratio generator to generate an aspect-ratio rule for any ratio, with the percentage padding-top fallback worked out for you and wrapped in a feature query. Everything runs inside this page, so the work happens on your own device rather than on a server. It is most often used for a responsive video or iframe embed that keeps its shape at any width. Companion tools to this one live on FreeToolStation.org.
How to use it
- Pick a common ratio, or choose "use the numbers below" and type your own.
- Set a maximum width so the preview matches the size you will use.
- Turn on the fallback if you need to support engines without aspect-ratio.
- Copy the rule.
How it works
aspect-ratio does what the name says: give an element a ratio and one dimension, and the browser works out the other. For a responsive video or image container that is width: 100% and aspect-ratio: 16 / 9, and the height follows whatever width the layout gives it. It is supported in every current browser and has been for several years, so for most projects it is simply the answer and the fallback below is history.
The history is worth knowing because you will meet it in existing code. Before aspect-ratio there was the percentage padding trick, which exploits a genuine oddity of the box model: a percentage padding resolves against the containing block’s width, even for padding-top and padding-bottom. So an element with height: 0 and padding-top: 56.25% is 56.25% of its own width tall, and 9 ÷ 16 is 0.5625. The content then has to be absolutely positioned over that padding, because the box itself has no height to put anything in.
That is why the fallback output here is three rules rather than one: the padding on the container, position: relative so it is a positioning context, and inset: 0 on the child to stretch it over the padded area. It is also wrapped in @supports not (aspect-ratio: 1 / 1), so browsers that understand the modern property never see it. Applying both unconditionally is a common mistake — the padding and the ratio then fight, and the element ends up roughly twice as tall as intended.
The ratio itself can be written two ways and they mean the same thing: aspect-ratio: 16 / 9 and aspect-ratio: 1.7778 both work, and the slash form is clearer in a stylesheet because it says what the ratio is rather than what it evaluates to. Where the two differ is precision: 16 / 9 is exact and 1.7778 is not, which shows up as a sub-pixel difference on a very wide element.
One behaviour catches people with images. If an img has an aspect-ratio and also intrinsic dimensions, the ratio wins for layout but object-fit decides what happens to the picture inside it — the default object-fit: fill stretches it. Add object-fit: cover to crop instead of distort. And on a flex or grid child, a minimum size from the content can override the ratio entirely: min-height: 0 or min-width: 0 is often what is missing when a ratio appears to be ignored.
When to use it
- A responsive video or iframe embed that keeps its shape at any width.
- Image placeholders that reserve the right space before the picture loads, so nothing shifts.
- Square thumbnails or avatars in a grid.
- Matching a design ratio like 21:9 or 2.39:1 exactly.
- Working out the percentage for an existing padding-top layout you are maintaining.
Common mistakes to avoid
- Using both aspect-ratio and padding-top without a feature query, which doubles the height.
- Inverting the fallback percentage. It is height divided by width, so 16:9 is 56.25%, not 177.78%.
- Forgetting position: relative on the padded container, so the child has nothing to position against.
- Leaving object-fit at its default on an image, which stretches it instead of cropping.
- Not clearing min-height or min-width on a flex or grid child, whose content size can override the ratio.
Frequently asked questions
What is the padding-top percentage for 16:9?
56.25% — height divided by width, or 9 ÷ 16. The trick works because a percentage padding resolves against the element’s width even on the top and bottom edges, which is one of the stranger corners of the box model.
Do I still need the padding-top fallback?
For most projects, no. aspect-ratio is supported in every current browser and has been for years. The fallback is here for projects with an old browser-support floor, and for reading code that predates the property — and it is wrapped in @supports so it never applies where it is not needed.
Should I write 16 / 9 or 1.7778?
Both are valid and equivalent. The slash form is clearer, because it states the ratio rather than its decimal value, and it is exact — 1.7778 is a rounded approximation and will differ by a fraction of a pixel on a very wide element.
Why is my aspect-ratio being ignored?
Usually one of two things. On a flex or grid child, the content’s own minimum size can override the ratio: set min-height: 0 or min-width: 0. Or something else is setting an explicit height, which wins over the ratio for the dimension it specifies.
Why is my image stretched inside the ratio?
Because object-fit defaults to fill, which distorts the picture to match the box. Add object-fit: cover to crop to the ratio instead, or object-fit: contain to letterbox it.