useAspectRatio

Responsive
stable

Compute a CSS padding-block-end value that enforces a given aspect ratio on a responsive container. Supports explicit aspect-ratio strings (e.g. "16/9") and falls back to the current viewport ratio when none is given. SSR-safe: returns an empty styles array on the server.

API

Signature

Full TypeScript signature sourced from the composable file.

typescript

On the server (SSR), IN_BROWSER is false and window is unavailable. aspectStyles returns [] so the layout collapses to its natural box height. The client rehydrates with the correct padding-block-end on the first reactive access.

API

Parameters

Input parameters accepted by this composable.

props
required
{ aspectRatio?: string | number }
Object carrying an optional aspectRatio value. Accepts a numeric ratio (1.777 for 16:9), a division expression string ("16/9"), or is left undefined to use window.innerWidth / window.innerHeight as the ratio.

API

Return values

Properties and methods exposed by the composable return object.

aspectStyles
Array of inline CSS declarations to spread via :style. Contains a single padding-block-end percentage when a valid ratio is resolved, or an empty array during SSR.

Usage

Examples

Ready-to-paste usage snippets.

16:9 video placeholder

vue

Hardcoded square

vue