Home/Tools/Layout/Aspect Ratio Generator
Layout · Generator

Aspect Ratio Generator

Generate aspect ratio CSS for responsive media containers.

Aspect Ratio

Container Width

100

Calculated Values

Ratio: 16:9

Padding hack: 56.25%

Decimal: 1.7778

Preview

16:9

Common Use Cases

16:9Videos, YouTube embeds
4:3Standard photos, older TVs
1:1Profile images, icons
9:16Stories, mobile video
Generated CSS
/* Modern approach (aspect-ratio property) */
.aspect-container {
  width: 100%;
  aspect-ratio: 16 / 9;
}

/* Fallback for older browsers (padding hack) */
.aspect-container-legacy {
  width: 100%;
  position: relative;
  padding-bottom: 56.25%;
}

.aspect-container-legacy > * {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
100%
Free, no
sign-up needed
0
Code written
by hand
85+
CSS tools
in one place
<1s
Copy-ready
CSS instantly
What is this

What is the CSS Aspect Ratio Generator?

Generate aspect-ratio CSS for responsive media containers. Supports standard ratios and custom values, with fallback padding-top code for older browsers.

Everything runs locally in your browser. Adjust the controls above, preview instantly, and copy clean, production-ready CSS — no account, no upload.

How to use

Using the CSS Aspect Ratio Generator

Everything lives in the panel at the top of this page. There is nothing to install — it all runs right here in your browser.

1

Select or enter your ratio

Pick from common ratios (16:9, 4:3, 1:1, 3:2) or enter custom width and height values.

2

Choose your output

Toggle between the modern aspect-ratio property and the legacy padding-top percentage fallback. The calculated percentage is shown automatically.

3

Copy the CSS

Click Copy to grab the output. The modern property is a one-liner. The legacy fallback includes the container and child position rules.

The output

What the generated code looks like

Clean, production-ready CSS — no vendor bloat, no unnecessary declarations.

styles.css
/* Modern browsers - Chrome 88+, Firefox 89+, Safari 15+ */
.video-container {
  aspect-ratio: 16 / 9;
  width: 100%;
}

/* Fallback for older browsers using padding hack */
.video-container-legacy {
  position: relative;
  padding-top: 56.25%; /* 9/16 = 0.5625 */
  width: 100%;
}
.video-container-legacy > * {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

The modern aspect-ratio property is simpler. Include the padding fallback only if you support Safari 14 or earlier.

Pro tips

Pro tips for better results

The modern aspect-ratio property is safe to use today

Chrome 88+, Firefox 89+, Safari 15+, and Edge 88+ support aspect-ratio. That covers more than 93% of current traffic. The padding-top fallback is only needed for very old Safari.

aspect-ratio on images prevents layout shift

Setting width: 100%; aspect-ratio: 16/9 on an img element reserves the correct space before the image loads, eliminating cumulative layout shift (CLS) - a Core Web Vitals metric.

The padding-top percentage formula

padding-top % = (height / width) x 100. For 16:9: (9/16) x 100 = 56.25%. For 4:3: (3/4) x 100 = 75%. For 1:1: 100%.

FAQ

Frequently asked questions

Before aspect-ratio was supported, developers used padding-top as a percentage to create responsive containers. A 56.25% top padding on a block element creates a 16:9 container because padding percentages are calculated relative to the element width.
Yes - it works on images, videos, iframes, divs, and any block or inline-block element. For replaced elements like img and video, the intrinsic ratio is used if no aspect-ratio is set.
No. Everything runs in your browser. Nothing is sent to a server.

From the blog

Read more