b2KIT

CSS Aspect Ratio Box Generator

Generate responsive aspect ratio containers using the aspect-ratio property or padding hack technique.

Tested tool guide Tested browser tools Checked August 16, 2026

What CSS Aspect Ratio Box Generator does, with a checked example

Turn a ratio like 16:9 into ready-to-paste CSS for a responsive box whose height always follows its width, so an embedded video, an image placeholder, or a card stays proportional at every screen size. The tool emits two variants: the modern `aspect-ratio` property and the older padding hack, in which vertical padding creates the height. The number people most often stumble on is the padding percentage, because it is not the ratio itself: 16:9 needs 56.25%, since percentage padding is always calculated from the box's width, never its height.

Worked example

A concrete input and expected output from the current implementation.

Input

16:9 video embed

Expected output

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

/* padding hack variant */
.ratio-box {
  position: relative;
  height: 0;
  padding-bottom: 56.25%;
}
.ratio-box > * {
  position: absolute;
  inset: 0;
}

The modern variant takes the ratio directly. The hack must express the height ratio as a percentage of the width ratio, because vertical percentage padding is measured against the box's width: 9 divided by 16 is 0.5625, so the padding is 56.25%.

How the result is produced

1

The padding hack

Percentage padding - top, bottom, left, or right - always resolves against the containing block's width, never its height. The hack exploits that rule: a box with `height: 0` and `padding-bottom` set to the height ratio as a fraction of the width ratio gets exactly the right height at any width. Children then need `position: absolute` (typically `inset: 0`) so they fill the padded area instead of pushing it taller.

2

The aspect-ratio property

`aspect-ratio: 16 / 9` states the preferred ratio directly: declare one dimension and leave the other auto, and the auto one derives from it. Two caveats make "preferred" accurate rather than decorative: content taller than the ratio stretches the box, and if both width and height are explicitly set, the property has no effect. Every current browser has supported it since late 2021.

Good uses

  • Wrapping a responsive YouTube or Vimeo iframe at 16:9 so the embed scales with its column instead of overflowing it.
  • Reserving a ratio-locked placeholder for a hero image or lazy-loaded photo, which prevents layout shift while the media downloads.
  • Keeping product photos or thumbnail grids visually uniform - forcing every cell to 1:1 or 4:5 regardless of the source images' dimensions, with `object-fit` cropping inside the ratio box.

Limits and checks

  • The padding percentage is computed from the containing block's width, not the element's own rendered width. If you set an explicit `width` or a `max-width` on the padded box that is narrower than its parent, the generated height no longer matches the ratio.
  • `aspect-ratio` is a preference, not a constraint. Setting both width and height explicitly disables it silently, and content taller than the ratio grows the box; the generated CSS only holds its shape while the child is absolutely positioned and one axis stays `auto`.
  • In the padding-hack variant, dropping `height: 0` adds the content's own height on top of the padding, and dropping the absolutely positioned child renders the content below the padded area - both produce a container visibly taller than the ratio.

Common questions

Why is the padding for 16:9 a number like 56.25% instead of something round?

Because vertical padding percentages are always computed from the box's width, the padding must be the height ratio expressed against the width ratio: 9/16 = 0.5625, or 56.25%. Rounding to 56% makes the box 0.25% of its width too short - about 1 pixel at 400px, 3 pixels at 1280px - which can expose background along the bottom edge.

Do I still need the padding hack now that aspect-ratio exists?

Only if you must support browsers released before 2021 - Chrome 87 and earlier, Firefox 88 and earlier, Safari 14 and earlier - none of which know the property. Everything current supports it, so for new projects the property is simpler and the ratio is readable at a glance. The hack remains useful mainly for reading and maintaining legacy code, which is full of it.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools