‮Skip to content‬
uiify

Aspect Ratio

‮A media frame that preserves its width-to-height ratio.‬

‮Browsers‬
Chrome 123+Firefox 121+Safari 17.5+
‮Tier‬
0
‮Bundle compressed‬
268 B
‮Forced layout budget‬
0 ms
‮Axe budget‬
0
‮SSR compatible‬
‮Yes‬
‮Published contract‬
‮Test harness‬

‮Playground‬

16:9 · Cover

An amber sun above overlapping blue mountain ridges

‮Examples‬

Square

The default ratio is 1. A square frame crops a landscape image at the sides.

An amber sun above overlapping blue mountain ridges
import { AspectRatio } from "@gunkin/uiify";

<AspectRatio>
  <img src="/mountains.svg" width="640" height="400" alt="An amber sun above blue mountain ridges" />
</AspectRatio>

Portrait

Set ratio to 4 / 5 for a portrait frame. The image keeps its proportions while filling the frame.

An amber sun above overlapping blue mountain ridges
import { AspectRatio } from "@gunkin/uiify";

<AspectRatio ratio={4 / 5}>
  <img src="/mountains.svg" width="640" height="400" alt="An amber sun above blue mountain ridges" />
</AspectRatio>

Contain

Apply .uncropped { object-fit: contain; } to keep the whole image visible. The optional skin supplies the frame background.

An amber sun above overlapping blue mountain ridges
import { AspectRatio } from "@gunkin/uiify";

<AspectRatio ratio={16 / 9}>
  <img className="uncropped" src="/mountains.svg" width="640" height="400" alt="An amber sun above blue mountain ridges" />
</AspectRatio>

‮API‬

AspectRatio

‮Prop‬‮Type‬‮Default‬‮Description‬
rationumber1‮Positive finite width divided by height. Invalid numbers fall back to 1. When omitted, the CSS custom property can set the ratio.‬
childrenReactNode—‮Content placed inside the frame. Intrinsic content cannot enlarge the frame.‬
classNamestring—‮Class applied to the frame root.‬
styleCSSProperties—‮Native root styles, including --uiify-aspect-ratio when ratio is omitted.‬
refRef<HTMLElement>—‮Ref forwarded to the rendered root element.‬
asElementType"div"‮Native element or component used for the root.‬
renderReactElement | ((props, state) => ReactElement)—‮Replace the rendered root while preserving its props, ref and identity marker.‬

‮Keyboard‬

‮Key‬‮Action‬

‮Accessibility‬

‮Static layout adds no role, focus target or keyboard behavior. Give meaningful images alt text; keep captions outside the frame. Required behavior CSS preserves the ratio even when children have intrinsic dimensions. Optional styles fill images with object-fit: cover; use contain to show the entire image.‬

‮Styling hooks‬

data-uiify-aspect-ratiodata-part="content"--uiify-aspect-ratio