Aspect RatioCalculator
Tool

CSS Aspect Ratio Generator

Enter any aspect ratio and instantly get production-ready CSS code: the modern `aspect-ratio` property, padding-top fallback, Tailwind and Bootstrap snippets, and a reusable React component.

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

Five output formats, live preview, and CLS-safe implementation details in one place.

5 code formats at onceLive previewTailwind and BootstrapReact componentCLS-safe code

Generator

Generate your CSS code

Your aspect ratio

:
Decimal: 1.7778 | Padding-top: 56.25%

Or enter pixel dimensions

×

Detected ratio: 16:9

Quick presets

Code options

Live preview

16:9 Container

800 x 450px

320px480px768px1024px1280px
Ratio: 16:9 | Decimal: 1.7778 | Tailwind: aspect-video
Recommended for modern browsers. Supports clean `aspect-ratio` output with optional fallback.

CSS

/* Aspect Ratio Container - Modern CSS */
/* Works in all modern browsers */
.ratio-container {
  --aspect-ratio: 16 / 9;

  aspect-ratio: var(--aspect-ratio);
  width: 100%;
  max-width: 800px;
}

.ratio-container > * {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

@supports not (aspect-ratio: 1 / 1) {
  .ratio-container {
    position: relative;
    padding-top: 56.25%;
  }

  .ratio-container > * {
    position: absolute;
    inset: 0;
  }
}

HTML

<div class="ratio-container">
  <!-- Your content here -->
</div>

SVG viewBox output

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 9" width="100%" preserveAspectRatio="xMidYMid meet">
  <!-- SVG content here -->
</svg>

Approach Comparison

Comparing the five approaches

Every approach solves the same ratio problem with different tradeoffs. The right answer depends on your browser baseline, framework choices, and how often the code will be reused.

FeatureModern CSSPadding HackTailwindBootstrapReact
Browser support95%+99%+95%+95%+95%+
IE11 supportNoYesNoNoNo
Code simplicityHighMediumHighHighMedium
No extra HTMLYesNoYesNoYes
CLS preventionYesYesYesYesYes
CSS variablesYesYesLimitedYesYes
Dynamic ratio changesYesLimitedLimitedYesYes
Framework dependencyNoNoYesYesYes

Modern CSS

Best for new projects

Recommended

New browser-baseline projects

Low-code, clean CSS output

Dynamic ratio switching via CSS variables

Avoid When

Legacy IE11 support

Projects locked to old browser matrices

Smallest code footprint

Padding Hack

Best for legacy support

Recommended

IE11 and legacy enterprise support

Old CMS themes

Long-tail browser compatibility

Avoid When

New greenfield apps

Simple component systems where extra markup is undesirable

Verboser HTML plus fallback CSS

Tailwind

Best for Tailwind projects

Recommended

Tailwind design systems

Rapid prototypes

Utility-first product codebases

Avoid When

Non-Tailwind stacks

Teams that want framework-agnostic examples only

Shortest usage syntax

Bootstrap

Best for Bootstrap projects

Recommended

Bootstrap 5 apps

Admin tools

Embedded content and iframe-heavy pages

Avoid When

Non-Bootstrap stacks

Heavily custom ratio sets without utility extensions

Low code inside Bootstrap projects

React

Best for component systems

Recommended

React and Next.js apps

Typed component libraries

Reusable media shell components

Avoid When

One-off static HTML pages

Projects without a component layer

Moderate initial setup, easy reuse

Modern CSS Guide

Modern CSS aspect-ratio property complete guide

The `aspect-ratio` property is now the cleanest default for responsive media containers. It reduces markup, works with CSS variables, and lets browsers reserve layout space before content arrives.

Basic syntax

/* Basic syntax */
.element {
  aspect-ratio: width / height;
}

.video-container { aspect-ratio: 16 / 9; }
.square { aspect-ratio: 1 / 1; }
.portrait { aspect-ratio: 4 / 5; }
.ultrawide { aspect-ratio: 21 / 9; }

:root {
  --ratio-widescreen: 16 / 9;
}

.container {
  aspect-ratio: var(--ratio-widescreen);
}

How it works

If width is defined, the browser calculates height from the ratio. If height is defined, the browser calculates width. If both width and height are already fixed, aspect-ratio no longer participates in the layout calculation. That makes it ideal for fluid containers, cards, and image wrappers where only one dimension is constrained.

.box {
  aspect-ratio: 16 / 9;
  width: 320px;
  /* height becomes 180px */
}

.box {
  aspect-ratio: 16 / 9;
  height: 180px;
  /* width becomes 320px */
}

The auto keyword

Replaced elements like images and videos have an intrinsic ratio. The `auto` keyword lets the browser use that natural ratio when it exists, and optionally fall back to a supplied ratio if it does not.

img {
  aspect-ratio: auto 16 / 9;
  width: 100%;
  object-fit: cover;
}

Min and max constraints

.video-wrapper {
  aspect-ratio: 16 / 9;
  width: 100%;
  max-width: 1200px;
  min-width: 280px;
  margin: 0 auto;
}

Browser support

BrowserVersionSupport
Chrome88+Full support
Firefox89+Full support
Safari15+Full support
Edge88+Full support
Opera74+Full support
Samsung Internet15+Full support
iOS Safari15+Full support
Chrome Android88+Full support
IE 11No support

For most modern production stacks the support story is strong enough to make aspect-ratio the default. Legacy enterprise environments are the main reason to keep a padding fallback in play.

Padding Hack Guide

The padding-top hack complete guide

The classic padding-top trick still matters for legacy compatibility. It works because vertical padding percentages are calculated from the element width, which lets you create a predictable height from the width.

Why it works

Padding percentages are always relative to width, even for `padding-top` and `padding-bottom`. That means a 56.25 percent top padding on a full-width box creates the exact height required for a 16:9 rectangle.

padding-top = (height / width) * 100%

16:9  -> 56.25%
4:3   -> 75%
1:1   -> 100%
4:5   -> 125%
9:16  -> 177.78%
21:9  -> 42.86%

Complete implementation

.aspect-ratio-box {
  position: relative;
  width: 100%;
  padding-top: 56.25%;
  overflow: hidden;
}

.aspect-ratio-box-inner {
  position: absolute;
  inset: 0;
}

.aspect-ratio-box-inner > img,
.aspect-ratio-box-inner > video,
.aspect-ratio-box-inner > iframe {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

CSS custom-property version

.aspect-ratio-box {
  --ratio: 56.25%;
  position: relative;
  width: 100%;
  padding-top: var(--ratio);
}

.ratio-4x3  { --ratio: 75%; }
.ratio-1x1  { --ratio: 100%; }
.ratio-4x5  { --ratio: 125%; }
.ratio-9x16 { --ratio: 177.78%; }

Limitations

Requires an extra wrapper element for the actual content.

More verbose than native aspect-ratio and less readable for future maintainers.

Dynamic ratio changes are possible, but less elegant than flipping a single aspect-ratio property.

Use it when compatibility demands it, not because it is the cleanest modern pattern.

Framework Guides

Framework-specific implementations

Utility frameworks, component libraries, and app frameworks all approach ratio shells slightly differently. The examples below focus on practical usage rather than theoretical syntax.

Tailwind CSS

<div class="aspect-video">...</div>
<div class="aspect-square">...</div>
<div class="aspect-[4/3]">...</div>
<div class="aspect-square md:aspect-video lg:aspect-[21/9]">...</div>

Bootstrap 5

<div class="ratio ratio-16x9">...</div>
<div class="ratio ratio-4x3">...</div>
<div class="ratio ratio-1x1">...</div>
<div class="ratio" style="--bs-aspect-ratio: 56.25%;">...</div>

CSS Grid and Flexbox

.grid-item {
  aspect-ratio: 1 / 1;
  overflow: hidden;
}

.flex-item {
  flex: 1;
  aspect-ratio: 16 / 9;
  min-width: 0;
}

Next.js and React

import Image from 'next/image';

export function ResponsiveImage() {
  return (
    <div style={{ aspectRatio: '16 / 9', position: 'relative' }}>
      <Image src="/hero.jpg" alt="" fill sizes="100vw" style={{ objectFit: 'cover' }} />
    </div>
  );
}

In Next.js the safest CLS pattern is width and height on static images, or `fill` inside a container that already has a known aspect ratio and explicit layout space.

CLS Optimization

Preventing layout shift (CLS optimization)

Images, video, embeds, and ad slots without reserved space are one of the fastest ways to damage Core Web Vitals. Aspect ratio CSS exists partly to solve that problem before the content finishes loading.

CLS score targets

0.1 or less: good

0.1 to 0.25: needs improvement

Above 0.25: poor

Why images cause CLS

The browser begins layout before the asset has loaded.

If the container has no known ratio or dimensions, the reserved height is effectively zero.

When the asset arrives, the layout recalculates and content below it shifts downward.

Setting aspect-ratio or explicit width and height gives the browser the geometry up front, which keeps the layout stable.

Modern CSS

.image-shell {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

HTML width and height

<img src="photo.jpg" alt="" width="1920" height="1080" style="width: 100%; height: auto;">

Padding fallback

.image-wrapper {
  position: relative;
  padding-top: 56.25%;
}

CLS prevention checklist

Set aspect-ratio on media containers
Always include width and height attributes on img
Use object-fit: cover or contain intentionally
Use Next.js Image or a similar optimized image component
Reserve space for embeds and ads before they load
Check CLS in Lighthouse and Chrome DevTools

Use Case Library

Common use case code library

These examples cover the most common frontend ratio problems: videos, hero media, product grids, social previews, maps, and ad slots. Use them as production starting points rather than toy snippets.

16:9

Responsive YouTube / video embed

The standard 16:9 embed shell for iframes, hosted video, and product demos.

<div class="video-container">
  <iframe
    src="https://www.youtube.com/embed/VIDEO_ID"
    title="Video title"
    allowfullscreen
  ></iframe>
</div>
.video-container {
  aspect-ratio: 16 / 9;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
}

.video-container iframe {
  width: 100%;
  height: 100%;
  display: block;
  border: 0;
}

16:9

Hero image with CLS prevention

A content hero that reserves space before the image loads and avoids layout shift.

<div class="hero-image">
  <img src="hero.jpg" alt="Hero" width="1920" height="1080">
</div>
.hero-image {
  aspect-ratio: 16 / 9;
  width: 100%;
  overflow: hidden;
}

.hero-image img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

1:1

Product image grid

Square e-commerce cards where image consistency matters more than cinematic framing.

<div class="product-image">
  <img src="product.jpg" alt="Product name" width="800" height="800">
</div>
.product-image {
  aspect-ratio: 1 / 1;
  overflow: hidden;
  background: #f5f5f5;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

1.91:1

Social media card preview

Open Graph or social preview cards with a 1.91:1 media area.

<div class="og-preview">
  <div class="og-image">
    <img src="og-image.jpg" alt="Article title" width="1200" height="630">
  </div>
</div>
.og-image {
  aspect-ratio: 1.91 / 1;
  overflow: hidden;
}

.og-image img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

4:3

Responsive map embed

A 4:3 map shell for location pages and contact sections.

<div class="map-container">
  <iframe src="https://maps.google.com/maps?..." title="Location map" loading="lazy"></iframe>
</div>
.map-container {
  aspect-ratio: 4 / 3;
  width: 100%;
  overflow: hidden;
  border-radius: 12px;
}

.map-container iframe {
  width: 100%;
  height: 100%;
  display: block;
  border: 0;
}

Mixed

Display ad container

A set of ad-slot ratios for IAB standard placements.

.ad-medium-rect { aspect-ratio: 6 / 5; width: 300px; }
.ad-leaderboard { aspect-ratio: 728 / 90; width: 100%; max-width: 728px; }
.ad-skyscraper { aspect-ratio: 4 / 15; width: 160px; }
.ad-half-page { aspect-ratio: 1 / 2; width: 300px; }
.ad-billboard { aspect-ratio: 97 / 25; width: 100%; max-width: 970px; }

Browser Compatibility

Browser compatibility matrix

The support matrix below compares native aspect-ratio, the padding hack, framework wrappers, CSS variables, and related features used by the generated snippets.

Browseraspect-ratioPadding hackTailwind v3Bootstrap 5CSS variablesobject-fitSVG viewBox
Chrome88+Yes88+88+49+32+Yes
Firefox89+Yes89+89+31+36+Yes
Safari15+Yes15+15+9+8+Yes
Edge88+Yes88+88+15+16+Yes
Opera74+Yes74+74+36+19+Yes
IE 11NoYesNoNoNoNoYes

How-To

How to add aspect ratio in CSS

Step 1

Enter the target ratio

Start with either a ratio such as 16:9 or a pixel pair such as 1920 by 1080. The generator will simplify the ratio, calculate the decimal value, and compute the padding-top fallback percentage.

Step 2

Choose the container width

Set the preview width so you can see the real rendered height for the ratio you care about. This is useful when you want a quick pixel-height answer for a fixed-width shell.

Step 3

Pick the output format

Switch between modern CSS, padding fallback, Tailwind, Bootstrap, and React outputs depending on the stack you are shipping. Every tab is built from the same ratio input so the math stays consistent.

Step 4

Enable fallbacks and variables if needed

For legacy support you can include a padding-top fallback. If you want themeable or dynamic values, enable CSS custom-property output and use the generated variable names.

Step 5

Preview the container

Use the live preview to verify the visual shape at different widths before pasting code into production. This is especially helpful for video embeds, hero media, and social cards.

Step 6

Copy the final snippet

Copy the CSS, HTML, combined snippet, or framework example directly from the active tab. The generated code is designed to be production-ready rather than just illustrative.

FAQ

Frequently asked questions

What is the best way to keep aspect ratio in CSS today?

For modern projects the best option is the native aspect-ratio property. It is short, readable, and widely supported in current browsers. Use the padding-top hack only when you must support older environments such as IE11.

How do I calculate the padding-top percentage for a ratio?

Use the formula height divided by width times 100 percent. For 16:9 that is 9 divided by 16 times 100, which equals 56.25 percent. The generator calculates that automatically for any custom ratio.

Does aspect-ratio prevent layout shift?

Yes. When you set aspect-ratio on a media container, the browser can reserve the correct height before the content loads. That prevents the layout from jumping when images, video, or embeds appear.

Should I still add width and height attributes to images?

Yes. Width and height attributes remain a strong CLS signal for images, even if you also use aspect-ratio. In many cases the best result is width and height on the image element plus an aspect-ratio wrapper around the layout shell.

How do I use aspect ratio in Tailwind CSS?

Tailwind v3 supports aspect-video and aspect-square out of the box, and arbitrary values such as aspect-[4/3] or aspect-[21/9] for custom shapes. The generator outputs both built-in and arbitrary-value examples.

How do I use aspect ratio in Bootstrap?

Bootstrap 5 ships with the .ratio utility and standard classes like ratio-16x9, ratio-4x3, ratio-1x1, and ratio-21x9. For custom ratios you can override the --bs-aspect-ratio custom property.

Is the padding-top hack still useful?

It is still useful for legacy browser support or long-lived codebases that cannot rely on aspect-ratio yet. It is not the first choice for greenfield work because it requires extra markup and is harder to read.

Can I use these snippets in React or Next.js?

Yes. The React tab includes a typed component example and a Next.js Image pattern that keeps the ratio stable while still allowing object-fit behavior and responsive sizing.

Keep Exploring

Related tools and ratio guides