Join our Discord Community

Shadcn Aspect Ratio

React aspect ratio component for consistent image and video proportions. Built with TypeScript and Tailwind CSS for Next.js applications with responsive design.

Aspect ratio not responsive?

Join our Discord community for help from other developers.


Ever uploaded a perfect product photo only to watch it get squashed into a pancake on mobile? Or had a client complain their logo looks "stretched weird"? Yeah, aspect ratios are the unsung heroes that prevent these design disasters in your Next.js applications.

Widescreen ratio

Standard 16:9 ratio for modern videos and images:

Loading component...

This shadcn/ui component leverages Radix UI's AspectRatio primitive with modern CSS and TypeScript types. Styled with Tailwind CSS utilities for easy customization.

npx shadcn@latest add aspect-ratio

Why aspect ratios actually matter

Here's the thing—aspect ratios aren't just about preventing stretched images. They're about creating predictable layouts that don't shift around while content loads. You know that annoying jump when an image finally loads and pushes everything down? Aspect ratios eliminate that.

Think of it like reserving a parking space. Your React component knows exactly how much space that image will take before it even loads. No layout shift, no jumpy scrolling, no frustrated users losing their reading position.

This free shadcn component handles the complexity with a simple ratio prop. Whether you're building image galleries, video players, or card layouts in your JavaScript apps, consistent proportions make everything feel more polished.

Common aspect ratio patterns you'll actually use

Square ratio

Perfect 1:1 ratio for profile pictures and thumbnails:

Loading component...

Portrait ratio

3:4 ratio for vertical content and mobile layouts:

Loading component...

Video player ratio

Multiple video ratios for different platforms:

Loading component...

YouTube embed

Real video embed that maintains 16:9 ratio:

Loading component...

Features

This free open source aspect ratio component includes everything you need:

  • TypeScript-first design - Full type safety with ratio calculations
  • Tailwind CSS integration - Style with utility classes, not inline styles
  • Radix UI foundation - Battle-tested primitive with accessibility built-in
  • CSS aspect-ratio support - Uses native CSS with JavaScript fallback
  • Responsive by default - Maintains proportions across all breakpoints
  • Works everywhere - Next.js, Remix, or vanilla React apps
  • No more jumpy layouts - Reserves space before content loads

API Reference

AspectRatio

The container that maintains a consistent width-to-height ratio.

PropTypeDefaultDescription
rationumber1The desired aspect ratio (width divided by height)
classNamestring-Additional CSS classes for styling
childrenReact.ReactNode-Content to display within the ratio container
asChildbooleanfalseRender as child component using Radix UI Slot

Common Ratios

RatioValueUse Case
Square1 / 1 or 1Profile pictures, app icons, thumbnails
Portrait3 / 4 or 0.75Vertical photos, phone screens
Landscape4 / 3 or 1.333Traditional photos, old TVs
Widescreen16 / 9 or 1.778YouTube videos, modern screens
Cinematic21 / 9 or 2.333Ultra-wide monitors, movie clips
Golden1.618 / 1 or 1.618Aesthetically pleasing layouts

Browser Support

FeatureSupport
CSS aspect-ratioModern browsers (Chrome 88+, Firefox 89+, Safari 15+)
JavaScript fallbackAll browsers via Radix UI
Mobile browsersFull support on iOS and Android
Smart fallbacksWorks even in ancient browsers

Production tips

Prevent layout shift with aspect ratios. This free shadcn/ui component reserves space before images load, eliminating the jarring content jumps users hate. Critical for Core Web Vitals scores.

Combine with Next.js Image for best results. Use fill prop with object-cover to let the Image component handle optimization while aspect ratio maintains proportions. Your TypeScript types will thank you.

Consider skeleton loaders. While aspect ratio prevents layout shift, combine with Skeleton components to show loading state. Users know content is coming instead of staring at empty boxes.

Test with slow connections. Aspect ratios shine when images load slowly. Throttle your network in dev tools to see how your React components behave under real conditions.

Use semantic ratios for content types. Profile pictures? Square. YouTube embeds? 16:9. Product photos? 4:3 or 3:4. Users recognize these patterns instinctively.

Integration with other components

Aspect ratio works brilliantly with Avatar components for consistent profile pictures in your React applications. Combine with Card components to create uniform grid layouts that don't shift as images load.

For loading states, wrap aspect ratio content with Skeleton components. This open source pattern gives users visual feedback while maintaining layout stability. When building galleries, aspect ratio ensures your Dialog previews match their thumbnails perfectly.

Questions you might have