Aspect Ratio
<div class="aspect-ratio" style="--ratio: 16 / 9; max-width: 20rem"> <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 180'%3E%3Cdefs%3E%3ClinearGradient id='a' x1='0' y1='0' x2='1' y2='1'%3E%3Cstop offset='0' stop-color='%236366f1'/%3E%3Cstop offset='1' stop-color='%2306b6d4'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect width='320' height='180' fill='url(%23a)'/%3E%3C/svg%3E" alt="" style="border-radius: 0.5rem" /></div><div class="aspect-ratio" style="--ratio: 16 / 9"> <img src="…" alt="" /></div>The ratio is a custom property rather than a class, so any ratio works without a class for each one. The child fills the box and is cropped to it.
Ratios
Section titled “Ratios”<div class="aspect-ratio" style="--ratio: 1; width: 6rem; background: var(--muted); border-radius: 0.5rem"></div><div class="aspect-ratio" style="--ratio: 4 / 3; width: 8rem; background: var(--muted); border-radius: 0.5rem"></div><div class="aspect-ratio" style="--ratio: 16 / 9; width: 10rem; background: var(--muted); border-radius: 0.5rem"></div>Set dir="rtl" on the page or on any subtree — padding, corners and positions follow the reading
direction on their own. See the button page for a live example.
Accessibility
Section titled “Accessibility”Cropping is a decision about content: a 16 / 9 box cuts the top and bottom from a portrait photo.
Where the subject matters, set object-position rather than accepting the centre.
A decorative image still needs alt="", so it is skipped rather than announced by its file name.