6 Pure CSS1 with JSPublished 2026-10-05Free · no sign-up
Seven demos of the aspect-ratio property: a 16:9 video frame, square tiles sized by their column, object-fit cover and contain compared, card thumbnails kept in line, a frame that switches ratio, a box that grows when content needs it and the old padding hack beside the modern one-liner.
aspect-ratio: 16 / 9 on the frame keeps any embedded video at the right shape at every width, replacing the old padding-top: 56.25% trick. The iframe or video simply fills it with width and height at 100 percent.
View code
<figure class="ar1"><div class="fr"><div class="vid" role="img" aria-label="Video placeholder">▶</div></div><figcaption>16 / 9 at any width</figcaption></figure>
A grid of perfectly square tiles whose size comes from the column width: each tile only sets aspect-ratio: 1. Resize the preview and the tiles stay square without a single height in the CSS.
The same wide image in two square frames: cover fills the frame and crops the sides, contain fits the whole image and leaves bars. Images inside fixed-ratio frames almost always want cover, with object-position to choose which part is kept.
View code
<div class="ar3"><figure><img alt="Wide striped test image cropped to fill the square" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 120'%3E%3Crect width='300' height='120' fill='%230ea5e9'/%3E%3Crect width='100' height='120' fill='%23f59e0b'/%3E%3Crect x='200' width='100' height='120' fill='%23db2777'/%3E%3C/svg%3E" style="object-fit:cover"><figcaption>cover</figcaption></figure><figure><img alt="The same image shown whole with empty bars" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 120'%3E%3Crect width='300' height='120' fill='%230ea5e9'/%3E%3Crect width='100' height='120' fill='%23f59e0b'/%3E%3Crect x='200' width='100' height='120' fill='%23db2777'/%3E%3C/svg%3E" style="object-fit:contain"><figcaption>contain</figcaption></figure></div>
Uploaded images come in all shapes, which makes card grids ragged. Giving every thumbnail the same aspect-ratio with object-fit: cover lines up the titles under them, whatever the original photos look like.
Buttons change one custom property and the frame snaps between 1:1, 4:3 and 16:9, showing how one variable can drive the shape of a media frame. The buttons form a group with the current ratio pressed.
aspect-ratio sets a preferred shape, but a box with text inside grows taller when the text needs room, because its minimum height is auto. The second card has more text and quietly breaks the ratio instead of overflowing, which is usually what you want.
View code
<div class="ar6"><article><b>Short</b>Fits the 4:3 shape.</article><article><b>Long</b>This card has a lot more to say, so it grows past the 4:3 shape rather than letting the text spill out of the box, because aspect-ratio respects content by default.</article></div>
Side by side, the padding-top percentage hack and the modern property produce the same 3:2 box. The hack needed an extra wrapper and absolute positioning; aspect-ratio needs one line. Keep the hack only if you must support very old browsers.
aspect-ratio: 16 / 9 keeps a box's shape as its width changes. It replaces the padding-top percentage trick, which needed an extra wrapper and absolutely positioned content.
Pair it with object-fit
An image in a fixed-ratio frame should use object-fit: cover to fill it and crop the overflow, or contain to show the whole image with bars. object-position picks which part survives the crop.
It prevents layout shift
Images with width and height attributes get an automatic aspect ratio before they load, so the page does not jump when they arrive. Set the attributes on every image.
Content still wins
If text inside a box needs more height than the ratio allows, the box grows, because its minimum height is auto. Add overflow: auto or min-height: 0 only if you really want a fixed shape.
Frequently asked questions
Is aspect-ratio supported everywhere?
Yes, in all current browsers.
What ratio should product images use?
Pick one, often 1:1 or 4:5, and use it everywhere so grids line up.
Can I animate aspect-ratio?
Yes, it is animatable, though changing it reflows the layout, so use it sparingly.