CSS Aspect Ratio Calculator
Test cases replayed : 2 of 2 match · Methodology · No outside source listed
Enter the original width and height in pixels, and the width the element will be shown at.
Result
padding-top%
aspect-ratio: width part
- aspect-ratio: height part
- Height at the displayed width (px)
Estimate for planning only, not tax, legal, financial or medical advice. This calculator uses the method explained on this page.
This browser did not allow copying. The sentence is in the box below, already selected: copy it with your keyboard or the menu.
How to use this calculator
Fill in the fields in this order; each step takes you to its field.
- Original width, in px: enter 1 or more.
- Original height, in px: enter 1 or more.
- Displayed width, in px: enter 0 or more.
- Read the result: padding-top; the panel also shows aspect-ratio: width part, aspect-ratio: height part and Height at the displayed width (px).
How this calculator works
The CSS aspect-ratio property (W3C CSS Box Sizing Module Level 4) takes width / height, which this calculator reduces by the greatest common divisor, e.g. 1920 × 1080 → 16 / 9.
For the older padding technique, vertical padding in percent is resolved against the element's width (CSS 2.1, box model), so padding-top = height ÷ width × 100% keeps the ratio. Height at a given width = width × h ÷ w.
The formula
| Symbol | Meaning | Unit |
|---|---|---|
| Original width | px | |
| Original height | px | |
| padding-top | % |
Worked example
| Original width | 1,920 px |
|---|---|
| Original height | 1,080 px |
| Displayed width | 640 px |
| aspect-ratio: width part | 16 |
|---|---|
| aspect-ratio: height part | 9 |
| Height at the displayed width (px) | 360 |
padding-top 56.25 %
Each step uses the unrounded value of the step before it; figures on this page are rounded for display.
Frequently asked questions
What padding-top keeps a 16:9 ratio?
56.25%, because 9 ÷ 16 = 0.5625.
Is the padding hack still needed?
Current browsers support the aspect-ratio property, so aspect-ratio: 16 / 9 does the same job. The padding method still works for very old browsers.
What height does a 16:9 video need at 640 px wide?
360 px.