Six common presets
16:9, 4:3, 1:1, 21:9, 9:16 and 3:2 cover video, photo, square and portrait use cases instantly.
Pick a ratio preset or enter a custom width and height, watch the box resize live, and copy both the modern and classic fallback CSS.
Lock any element to a ratio and watch it hold that shape at every screen size.
16:9, 4:3, 1:1, 21:9, 9:16 and 3:2 cover video, photo, square and portrait use cases instantly.
Type any width and height numbers to preview and generate CSS for a ratio that isn't in the preset list.
Copy the one-line aspect-ratio rule, or the classic padding-top hack for older-browser support.
The CSS aspect-ratio property sets a preferred width-to-height ratio for a box, so once one dimension is known — usually the width, from the surrounding layout — the browser calculates the other automatically, with no JavaScript, no wrapper element and no image required. Written as aspect-ratio: 16 / 9, it means the box will try to stay 16 units wide for every 9 units tall as it resizes, which is exactly what you want for responsive video embeds, product photo grids and placeholder boxes that must not jump around as images finish loading. Before this property existed, developers relied on what's now called the padding-top percentage hack: because CSS always resolves percentage values for padding-top and padding-bottom against an element's own width, not its height, setting padding-top: 56.25% on an empty block forces its rendered height to be exactly 9/16 of its width, and the real content is then absolutely positioned inside to fill that reserved space. The hack still works everywhere and remains useful for supporting very old browsers, but it needs extra markup and positioning, whereas aspect-ratio is now supported in every current major browser and should be the default choice for new projects.
aspect-ratio typically computes height from a known width, but it works in reverse too when height is constrained instead.
If content inside the box is taller than the ratio allows, the box will grow unless you also set overflow or a fixed height.
Set aspect-ratio directly on an img with object-fit: cover to prevent layout shift while it loads.
The padding-top hack isn't a hack in the negative sense — it's valid, well-supported CSS worth keeping for older targets.
aspect-ratio sets a preferred width-to-height ratio for an element, so its height is calculated automatically from its width (or vice versa) without JavaScript.
The padding-top hack works in browsers that predate the aspect-ratio property, using the fact that percentage padding is always calculated relative to an element's width, even for padding-top and padding-bottom.
aspect-ratio is supported in all current major browsers, so most new projects can use it alone; keep the fallback only if you must support very old browser versions.
Yes. Enter any width and height numbers in the custom fields and both the modern and fallback CSS update to match that exact ratio.
Yes. It's completely free, requires no signup, and the CSS is generated locally in your browser.
Keep building polished interfaces with these free online CSS generators.