Display

Aspect Ratio.

Keeps media and placeholders at a fixed width to height ratio as the container resizes.

Screenshot16 : 9

Installation#

With the CLI

$npx lanterncn add aspect-ratio

This also installs the Lantern theme and any Lantern UI components it depends on. The shadcn CLI works directly too: npx shadcn@latest add httptim/lantern-ui/aspect-ratio. See Installation if your project is not set up yet.

Manually

  1. Install the dependencies

    npm install radix-ui
  2. Copy the source into your project

    components/ui/aspect-ratio.tsx
    "use client";
    
    import * as React from "react";
    import { AspectRatio as AspectRatioPrimitive } from "radix-ui";
    
    function AspectRatio({ ...props }: React.ComponentProps<typeof AspectRatioPrimitive.Root>) {
      return <AspectRatioPrimitive.Root data-slot="aspect-ratio" {...props} />;
    }
    
    export { AspectRatio };
  3. Update the import paths to match your project

    The source imports cn from @/lib/utils.

Usage#

import { AspectRatio } from "@/components/ui/aspect-ratio";
<AspectRatio ratio={16 / 9} className="rounded-lg border bg-accent bg-grid">
  <img src="/screenshot.png" alt="Turtle Farm" className="size-full object-cover" />
</AspectRatio>

The child fills the box absolutely, so give images size-full and object-cover. Put rounded corners and overflow-hidden on AspectRatio itself.

Examples#

Other ratios

1 : 1
4 : 3
3 : 4