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.

Video (16:9)

Installation

Install the component using the Tailgrids CLI.

npx @tailgrids/cli@latest add aspect-ratio

Anatomy

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.

Square (1:1)
Video (16:9)
4:3
3:4
21:9
9:16
3:2
2:3

Custom Ratio

Pass any numeric value to ratio (e.g., 2.35 for cinematic widescreen).

Custom Ratio: 2.35:1 (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.

Landscape image

16:9 Landscape

Square image

1:1 Square

API Reference

AspectRatio

PropTypeDefaultDescription
rationumber16 / 9Aspect ratio (width ÷ height), exposed as the --ratio CSS variable
classNamestring-Additional CSS classes to apply to the container
childrenReact.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 alt attribute 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.

On this page

Tailgrids Pro

Access 600+ blocks, components and templates - built for speed, consistency, and scale.

Get Tailgrids Pro