Skip to content

Menu

Aspect Ratio Box Generator

Generate CSS aspect ratio containers

Runs entirely in your browserFrontendFree, no sign-up
Loading tool

01What is Aspect Ratio Box Generator?

A box that keeps its shape as the page resizes sounds simple, yet it used to take a hack. Video embeds, image placeholders and square cards all need it. This generator writes the CSS both ways, so you can pick the modern rule or keep a fallback for old browsers. Choose a ratio with one of the eight presets (16:9, 4:3, 1:1, 21:9, 3:2, 9:16, 2:1 and 5:4) or type whole numbers into Width Ratio and Height Ratio. Max Width takes any CSS length, such as 100%, 600px or 80vw. Background Color has a picker and a text field for a hex value. The Live Preview redraws as you go, and a small summary shows the ratio, the padding percentage and the decimal value. You get three CSS blocks. Modern CSS uses the aspect-ratio property from the CSS Box Sizing Module Level 4 spec, along with your width, background and overflow: hidden. Legacy Fallback CSS uses the older padding-bottom trick. Vertical padding in percent is measured against the element's width, so a ::before pseudo-element with padding-bottom set to height divided by width times 100 reserves the right height. Direct children are then positioned absolutely to fill that space. The Combined block keeps the modern rule and wraps the fallback in @supports not (aspect-ratio: 1 / 1). Browsers that understand aspect-ratio skip the padding trick entirely, so you never get double height. The comment in the output lists the versions where support arrived: Chrome and Edge 88, Firefox 89 and Safari 15. A quick worked case: pick 21:9. The summary shows 42.8571% padding and a decimal of 2.3333. The modern block reads aspect-ratio: 21 / 9, and the legacy block uses padding-bottom: 42.8571%. For a portrait 9:16 box, the padding jumps to 177.7778%, because the box is taller than it is wide. A few honest limits. Padding and decimal values are rounded to four places, which is far finer than a pixel at normal sizes. The background colour appears only in the modern rule, so add it to the legacy block yourself if old browsers need it. The ratio fields take whole numbers, so a ratio like 1.85:1 needs to be entered as 37:20. Nothing you type leaves the page, since the CSS is assembled locally in your browser.

Front-end developers, WordPress and Shopify theme builders, UI designers prototyping layouts and anyone embedding video on a site. The tool runs entirely in your browser, so your data never leaves your device.

02How to use Aspect Ratio Box Generator

  1. Click a preset such as 16:9 or 1:1, or type numbers into Width Ratio and Height Ratio.
  2. Set Max Width, for example 100% or 720px.
  3. Choose a Background Color with the picker or type a hex value.
  4. Check the Live Preview and the Ratio, Padding and Decimal summary.
  5. Copy the Modern CSS, Legacy Fallback CSS or Combined (with fallback) block.

03Worked examples

Input: Preset 16:9, Max Width 100%
Result: Padding 56.2500%, Decimal 1.7778 .aspect-ratio-box { aspect-ratio: 16 / 9; width: 100%; background: #1e293b; overflow: hidden; }
Modern CSS block with the default background. 9 divided by 16 gives 56.25%.
Input: Preset 4:3, Max Width 600px
Result: .aspect-ratio-box { position: relative; width: 600px; overflow: hidden; } .aspect-ratio-box::before { content: ""; display: block; padding-bottom: 75.0000%; }
Legacy Fallback CSS, trimmed before the rule that positions children.
Input: Preset 21:9, Background Color #000000
Result: Padding 42.8571%, Decimal 2.3333 .aspect-ratio-box { aspect-ratio: 21 / 9; width: 100%; background: #000000; overflow: hidden; }
Both values are rounded to four decimal places.
Input: Preset 9:16, Max Width 360px (Combined)
Result: /* Legacy fallback (padding-bottom trick) for older browsers only */ @supports not (aspect-ratio: 1 / 1) { ... padding-bottom: 177.7778%; ... }
Trimmed to the fallback part. Portrait boxes need more than 100% padding.
Input: Preset 3:2
Result: Padding 66.6667%, Decimal 1.5000
Summary panel values for a classic photo ratio.

04Common use cases

  • Wrapping a YouTube or Vimeo iframe so it scales with the column width and keeps 16:9
  • Making square product or avatar cards in a CSS grid that stay square at every breakpoint
  • Reserving space for lazy-loaded images so the page doesn't jump when they arrive
  • Building a 9:16 frame for vertical video or story previews in an admin dashboard
  • Keeping a padding-bottom fallback for an older embedded WebView that predates aspect-ratio
  • Getting the decimal ratio a JavaScript video player or chart library expects as a number

05Tips

  • If the element also has a fixed height, the browser ignores aspect-ratio. Leave height on auto.
  • The legacy version only stretches direct children, because the rule targets .aspect-ratio-box > *. Wrap nested content in one child element.
  • Pair the box with object-fit: cover on an img so the picture crops instead of stretching.
  • For a non-integer ratio, scale both sides up. 2.39:1 becomes 239:100.

06Related concepts

CSS aspect-ratio, Padding-bottom hack, Responsive embeds, Cumulative Layout Shift, object-fit.

Frequently Asked Questions

What is Aspect Ratio Box Generator?

Aspect Ratio Box Generator is a free, browser-based developer tool on AIDToolStack. Generate CSS aspect ratio containers It requires no installation or sign-up and works entirely in your browser.

Is Aspect Ratio Box Generator free to use?

Yes. No sign-up, no fees and no usage cap.

Does Aspect Ratio Box Generator store my data?

No. It runs in your browser; nothing is uploaded.

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe