CSS Generators

Aspect Ratio

All tools

Reserve the right space for images and video before they load, using one CSS property.

How it works

Four simple steps

1

Select a ratio

Choose 16:9, 4:3, 1:1 or 9:16 from the menu.

2

Read the rule

The .frame rule sets aspect-ratio, full width and object-fit.

3

Apply to media

Add the class to the img or video element itself.

4

Copy the CSS

Copy the rule or download aspect-ratio.css.

Aspect Ratio

About Aspect Ratio

Blog thumbnails jump the page around as they load, and the layout shift score in your performance report keeps getting worse. Giving each frame a fixed ratio lets the browser reserve the space up front. Pick the ratio and copy the rule.

Select one of four ratios: 16 / 9 for landscape video and hero images, 4 / 3 for classic photos, 1 / 1 for avatars and product tiles, or 9 / 16 for vertical video and stories. The output is a .frame rule with aspect-ratio set to that value, width: 100% so it fills its column, and object-fit: cover so an image or video crops to fill the frame instead of stretching.

Put the class directly on the img or video element. If you place it on a wrapper div instead, object-fit has no effect on the wrapper; move that line to the media element inside. Note that aspect-ratio is ignored when both width and height are set explicitly, so remove any fixed height first. For media where cropping would cut off something important, such as a chart, switch object-fit to contain in the copied rule. The modern property replaces the old padding-top percentage hack, which this tool does not generate. To crop the source files themselves to the same shape, try the image cropper for pictures and the video crop tool for clips.

Keyboard shortcut

Ctrl Enter - Run tool
FAQ

Frequently asked questions

See all FAQs

Answers for this tool. For site-wide help, open the FAQ hub.

They are still recommended. Browsers use the attributes to calculate a ratio before CSS loads, and the CSS rule then keeps the frame consistent across layouts.

If the element has both a fixed width and a fixed height, the property has nothing to calculate. Content taller than the ratio allows can also stretch it unless you set min-height: 0 or overflow: hidden.

Yes. Edit the copied value to aspect-ratio: 21 / 9. Any pair of positive numbers works, including decimals.

View more FAQs

Related tools

View all

Tailwind Class Sorter

Sort Tailwind classes and detect conflicting utilities with a winner explanation.

Open

Design System Diff Lab

Diff design tokens and inspect CSS @layer order and selector specificity locally.

Open

SVG Path Inspector

Parse SVG path commands, preview on canvas, round and minify coordinates.

Open

Flexbox Generator

Build flex layouts visually.

Open

Explore 321 free tools

Everything you need as a developer, marketer or creator - in one beautiful place.

Browse all tools