StealThis .dev

Aspect Ratio

A container component that maintains a specific aspect ratio regardless of content or viewport size. Uses the modern CSS aspect-ratio property with a padding-bottom fallback.

Open in Lab
css javascript vue svelte
Targets: TS JS HTML React Vue Svelte

Code

Aspect Ratio

A utility container that enforces a specific aspect ratio on its children. Uses the modern CSS aspect-ratio property with a padding-bottom percentage fallback for older browsers.

How it works

  1. The container uses aspect-ratio: <width> / <height> to maintain proportions.
  2. A fallback uses padding-bottom: calc(height / width * 100%) with position: absolute children for browsers without aspect-ratio support.
  3. Content inside the container is stretched to fill via object-fit: cover.

Features

  • Native aspect-ratio CSS property with padding-bottom fallback
  • Supports any ratio (16:9, 4:3, 1:1, 21:9, etc.)
  • Content fills container with object-fit: cover
  • Responsive by default