Aspect Ratio
Displays content within a desired ratio.
AspectRatio — wrapper qui force son enfant à respecter un ratio largeur/hauteur donné. Utile pour intégrer des images, vidéos ou iframes responsive sans jump de layout au chargement.
Implémentation : CSS aspect-ratio: W/H natif (Chrome 88+, Firefox 89+, Safari 15+). Aucun JS, aucun 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 | — | Ratio largeur/hauteur (`16 / 9`, `1 / 1`, `9 / 16`…). Un string comme `"4/3"` est aussi accepté. |
| children | ReactNode | — | Le contenu — typiquement une image (<img className="h-full w-full object-cover" />) ou un iframe (<iframe className="h-full w-full" />). |
| className | string | — | undefined |
Data Attributes
Attributs qu'on peut cibler en CSS custom pour override le style par état.
| Attribute | On | Values | Description |
|---|---|---|---|
| data-slot="aspect-ratio" | AspectRatio root | — | undefined |
Accessibility
Purement décoratif — pas de sémantique ajoutée. Si le contenu est une image, garder son alt. Si c'est un iframe, garder son title. Pour la mise en page RTL, wrapper avec dir="rtl" sur un ancêtre.
Examples
Square
Portrait
RTL
YouTube embed (16 / 9)
Edit this example live in the bext playground — it compiles and runs entirely in your browser.
On This Page