Aspect Ratio
Displays content within a desired ratio.
AspectRatio is a wrapper that forces its child to a given width/height ratio. Useful for embedding responsive images, videos or iframes without layout shift on load.
Implementation: native CSS aspect-ratio: W/H (Chrome 88+, Firefox 89+, Safari 15+). No JS, no padding hack.
Installation
$ bext ui add aspect-ratio
Copy and paste the following into src/components/ui/aspect-ratio.tsx.
Usage
Anatomy
<AspectRatio ratio={16 / 9}>{child}</AspectRatio>API Reference
AspectRatio
| Prop | Type | Default | Description |
|---|---|---|---|
| ratio | number | string | "1/1" | Width/height ratio (`16 / 9`, `1 / 1`, `9 / 16`…). A string such as `"4/3"` is also accepted. |
| children | ReactNode | — | The content, typically an image (<img className="h-full w-full object-cover" />) or an iframe (<iframe className="h-full w-full" />). |
| className | string | — | Extra classes on the wrapper (relative w-full overflow-hidden), for example a rounded corner or a background. |
Data Attributes
Attributes you can target in custom CSS to override the style for each state.
| Attribute | On | Values | Description |
|---|---|---|---|
| data-slot="aspect-ratio" | AspectRatio root | — | Stable selector for custom styling. The ratio itself is an inline aspect-ratio style. |
Accessibility
Purely decorative, no semantics added. If the content is an image, keep its alt. If it is an iframe, keep its title. For RTL layout, set dir="rtl" on an ancestor.
Examples
Square
Portrait
RTL

YouTube embed (16 / 9)
Edit this example live in the bext playground. It compiles and runs entirely in your browser.
The React idiom: same UI, for comparison.
On This Page