Aspect Ratio
The React Aspect Ratio component keeps an element’s width and height in a fixed ratio. It is useful for images, videos, and embeds that need to keep the same shape across different screen sizes.
It adjusts the height based on the container width. So, the content scales correctly, and the layout does not shift during loading or resizing.
Built with React and styled using Tailwind CSS. It supports predefined aspect ratios and custom values for flexible layouts.
Installation
Install the component using the Tailgrids CLI.
npx @tailgrids/cli@latest add aspect-ratioAnatomy
Import the component and pass the desired aspect ratio as a number (width ÷ height).
import { AspectRatio } from "@/components/tailgrids/aspect-ratio";
export const AspectRatioExample = () => (
<AspectRatio ratio={16 / 9} className="rounded-lg overflow-hidden">
<img
src="path/to/image.jpg"
alt="Description"
className="size-full object-cover"
/>
</AspectRatio>
);Examples
Common Ratios
ratio is a number (width ÷ height) and defaults to 16 / 9. Use 1 for square, 4 / 3, 3 / 4, 21 / 9, 9 / 16, 3 / 2 or 2 / 3 for the others.
Custom Ratio
Pass any numeric value to ratio (e.g., 2.35 for cinematic widescreen).
Images
The AspectRatio component works seamlessly with responsive images. Use object-cover or object-contain classes to control how images fill the container.

16:9 Landscape

1:1 Square
API Reference
AspectRatio
| Prop | Type | Default | Description |
|---|---|---|---|
ratio | number | 16 / 9 | Aspect ratio (width ÷ height), exposed as the --ratio CSS variable |
className | string | - | Additional CSS classes to apply to the container |
children | React.ReactNode | - | Content to display within the aspect ratio container |
Accessibility
- Semantic structure: The component uses a standard
<div>. Add semantic elements inside it when needed, such as<img>,<video>, or<iframe>. - Alt text for images: Always provide a clear
altattribute when using images so screen readers can understand the content. - Focus handling: The component forwards refs, so you can manage focus if the content inside is interactive.
- Responsive behavior: The aspect ratio stays consistent across screen sizes, so content does not shift or break layout.
