AspectRatio
A box that keeps a proportion whatever width it is given. It draws nothing of its own: it reserves the space and holds what is inside it to the shape.
import { AspectRatio } from 'neba';
<AspectRatio ratio={16 / 9} rounded>
<img src={src} alt="A still alpine lake at dawn" />
</AspectRatio>;Props
| Prop | Type | Default | Description |
|---|---|---|---|
| ratio | number | string | 1 | The proportion the box holds, written the way CSS writes it: a number (1.5) or a ratio ('16 / 9'), reaching aspect-ratio untouched |
| fit | 'cover' | 'contain' | 'fill' | 'none' | 'cover' | How a single piece of media inside is fitted. Applies to an img, video, canvas, svg or iframe that is a direct child; anything else is laid out normally |
| rounded | boolean | false | Rounds the corners to the size step of the radius ladder. Off by default, because a layout component draws nothing |
| sizeshared | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' | Which step of the radius ladder rounded uses. As on Box, it never touches a height or the type scale |
| render | useRender.RenderProp | — | Renders something other than a div (render={<figure />}). Base UI's own escape hatch |
| children | ReactNode | — | What is held to the proportion |
Native <div> attributes pass through, and render swaps the element. The shared axes are described in prop conventions.
Examples
ratio
ratio is CSS's own aspect-ratio, so a number (1.5) or a ratio ('16 / 9') both reach it untouched and there is nothing to translate.
fit
fit is object-fit applied to a single piece of media that is a direct child: an img, a video, a canvas, an svg or an iframe. The media is stretched to the box first, which is the pair of declarations every use of this component would otherwise start with. cover crops, contain letterboxes, fill squashes.
Reserving the space
The proportion holds whether or not the content has arrived, so a Skeleton inside an AspectRatio occupies exactly the box the image will. Nothing below it moves when the image loads.
rounded
rounded cuts the corners to the size step of the house radius ladder. It is off by default (a layout component draws nothing), and it is the one exception, because a photograph in a card almost always wants it.
<AspectRatio ratio={4 / 3} rounded size="lg">
<img src={src} alt="" />
</AspectRatio>Accessibility
- The box adds no role and no name. It is a shape, and what a reader hears is whatever is inside it.
- An
imginside still needs its ownalt. A decorative image takesalt="".