# AspectRatio Reserve a proportion of the available width for media before it loads. [Interactive guide](/docs/components/aspectratio) · Forge 3.2.0 Use the API for your installed version. Run `npx --no-install wss3-forge api --json` inside the project. ## Implementation guidance ### When to use it Use AspectRatio to reserve the dimensions of images or video before they load. ### How to compose it Put the media inside the frame and choose its fit behavior according to whether cropping is acceptable. ### What to verify Check the frame before loading and at narrow widths to verify that the surrounding layout stays stable. ## Usage ```tsx import { AspectRatio } from 'wss3-forge' ``` - Divide width by height. Use 16 / 9 for a landscape frame, 1 for a square or 3 / 4 for a portrait. - Use maxW to cap the frame. Its height follows its available width, including before an image finishes loading. - Set objectFit to cover to crop a photograph, or contain to show an entire image. Supply alternative text on the image itself. - The root accepts native div attributes and a ref. Zero, negative and non-finite ratios fall back to a square. ```tsx Vintage wooden letterpress characters ``` ## Runnable examples ### Widescreen ```tsx import { AspectRatio } from 'wss3-forge' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ### Square ```tsx import { AspectRatio } from 'wss3-forge' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ### Portrait ```tsx import { AspectRatio } from 'wss3-forge' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ### Right to left ```tsx import { AspectRatio } from 'wss3-forge' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ## Primitive libraries ### Base UI This entry point uses a native div with CSS aspect-ratio. Its ref points to the frame and it accepts native div attributes. Base UI and React Aria use the Forge CSS aspect-ratio frame. Radix uses its AspectRatio primitive. ```sh npm install wss3-forge @base-ui/react ``` ```tsx import { AspectRatio } from 'wss3-forge/base-ui' export default function Example() { return ( Profile portrait ) } ``` #### Preview ```tsx import { AspectRatio } from 'wss3-forge/base-ui' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ### Radix This entry point uses Radix AspectRatio.Root and accepts its asChild prop. maxW constrains the outer frame. Base UI and React Aria use the Forge CSS aspect-ratio frame. Radix uses its AspectRatio primitive. ```sh npm install wss3-forge radix-ui ``` ```tsx import { AspectRatio } from 'wss3-forge/radix' export default function Example() { return ( Profile portrait ) } ``` #### Preview ```tsx import { AspectRatio } from 'wss3-forge/radix' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ### React Aria This entry point uses a native div with CSS aspect-ratio. Its ref points to the frame and it accepts native div attributes. Base UI and React Aria use the Forge CSS aspect-ratio frame. Radix uses its AspectRatio primitive. ```sh npm install wss3-forge react-aria-components ``` ```tsx import { AspectRatio } from 'wss3-forge/react-aria' export default function Example() { return ( Profile portrait ) } ``` #### Preview ```tsx import { AspectRatio } from 'wss3-forge/react-aria' function LetterpressPhoto() { return Vintage wooden letterpress characters } export default function Example() { return () } ``` ## Root package API ### AspectRatio Reserve a proportion of the available width for media before it loads. Props type: `AspectRatioProps` Native attributes: AriaAttributes, DOMAttributes, HTMLAttributes - `ratio?` — `number` Default `1`. Frame width divided by height. Non-positive or non-finite values use a square. - `maxW?` — `string | number` Maximum width as a CSS length or pixel count. - `ref?` — `React.LegacyRef | undefined` ## Related types ```ts export interface AspectRatioProps extends React.HTMLAttributes { /** Frame width divided by height. Non-positive or non-finite values use a square. */ ratio?: number maxW?: string | number } ``` ## Optional primitive contracts - [wss3-forge/base-ui · AspectRatio contract](/ai/skill/primitive-api/AspectRatio.json) - [wss3-forge/radix · AspectRatio contract](/ai/skill/primitive-api/AspectRatio.json) - [wss3-forge/react-aria · AspectRatio contract](/ai/skill/primitive-api/AspectRatio.json)