Boneyard Tools

CSS Aspect Ratio Box Generator

Generate the CSS that keeps a container locked to a fixed ratio as its width changes, ideal for video embeds, thumbnails and hero images. Pick a common ratio or type your own, and the generator reduces it with the greatest common divisor before writing the rule. Switch between the modern aspect-ratio property and the padding-top fallback, then copy a ready to paste .aspect-box class.

How to generate an aspect ratio box

  1. Click a common ratio like 16:9, or set Width and Height with the number fields.
  2. Choose the aspect-ratio method for modern browsers or padding-top for older support.
  3. Check the live preview box updates to match the ratio you entered.
  4. Press the copy button to grab the generated .aspect-box CSS.
  5. Paste the rule and wrap your image, video or iframe in a .aspect-box element.

Examples

16:9 with the modern property

width 16, height 9, method aspect-ratio
.aspect-box { aspect-ratio: 16 / 9; width: 100%; }

16:9 padding-top fallback

width 16, height 9, method padding
padding-top: 56.25%; on a relative wrapper with an absolute child

21:9 reduces to 7:3

width 21, height 9, method aspect-ratio
.aspect-box { aspect-ratio: 7 / 3; width: 100%; }

Frequently asked questions

What is the difference between the two methods?

The aspect-ratio property is the modern one-line approach and is widely supported in current browsers. The padding-top trick is the classic fallback that reserves height as a percentage of width, which still works in very old browsers that never shipped aspect-ratio.

How is the padding-top percentage calculated?

It is the height divided by the width, times 100. For 16 by 9 that is 9 / 16 = 56.25%, which makes a wrapper that keeps the ratio as it grows. The comment in the generated code shows the reduced ratio next to the percentage.

Why is the ratio reduced, like 21:9 to 7 / 3?

The tool divides both numbers by their greatest common divisor so the printed ratio is the simplest equivalent. For 21 by 9 the divisor is 3, giving 7 / 3, which behaves identically but reads more cleanly in your stylesheet.

Why does the padding method need an absolute child?

Padding reserves the space but leaves no box for content to flow into, so a child is positioned absolutely with top, left, width and height set to fill it. Put your image, video or iframe inside that child element.

What ratio values can I enter?

Width and height each accept whole numbers from 1 to 100. Both must be positive integers, so decimals are rejected. If you need a ratio from pixel dimensions like 1920 by 1080, divide them down to small integers such as 16 by 9 first.

Does the generated class name matter?

The rule targets a class called .aspect-box, and the padding version also styles .aspect-box > * for the child. Rename both selectors after pasting if that name clashes with your existing CSS, keeping the pair in sync.

Which method should I use today?

For most projects the aspect-ratio property is cleaner and needs no wrapper child, so use it unless you must support browsers from before 2021. The padding-top fallback remains handy for email or legacy embed contexts where support is uncertain.

Is anything sent to a server?

No. The CSS is generated in your browser and your width and height values are never uploaded, so you can use the tool offline once the page has loaded.

Learn more

Related tools