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
- Click a common ratio like 16:9, or set Width and Height with the number fields.
- Choose the aspect-ratio method for modern browsers or padding-top for older support.
- Check the live preview box updates to match the ratio you entered.
- Press the copy button to grab the generated .aspect-box CSS.
- 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
- CSS aspect-ratio versus the padding-top hack
How the modern aspect-ratio property and the older padding-top technique each hold a box at a fixed shape, and when to reach for one over the other.
Related tools
Aspect Ratio Calculator
Calculate aspect ratios fast. Enter a ratio like 16:9 and one dimension to get the other, or enter width and height to simplify the ratio.
CSS Clamp Generator
Generate a responsive CSS clamp() value for fluid font sizes and spacing. Set min and max sizes plus viewport bounds and copy the result.
CSS Gradient Generator
Build linear and radial CSS gradients with a live preview. Pick colors, set the angle and copy ready-to-paste background code for your site.
.env to JSON
Convert a .env file to JSON, or JSON back to .env. Parses KEY=value lines, comments, quotes and export. Runs entirely in your browser.
ASCII Table
Full ASCII table for all 128 codes with decimal, hex, octal and binary values, character names and descriptions. Search by code, hex or character.
Base32 Encode
Base32 encode and decode text online using the standard RFC 4648 alphabet with proper padding. UTF-8 safe, fast and private in your browser.