Veil UI
Components

Aspect Ratio

Locks a box to a width-to-height ratio while it resizes.

Installation

$ npx veil-ui@latest add aspect-ratio

Usage

import { AspectRatio } from "@/components/ui/aspect-ratio";

<AspectRatio ratio={16 / 9}>
  <img src="/cover.jpg" alt="" className="size-full object-cover" />
</AspectRatio>;

Examples

Basic

16 / 9

Square

1 / 1

Portrait

3 / 4

API reference

PropTypeDefaultDescription
rationumber16 / 9Width divided by height.
styleReact.CSSProperties—Merged with the aspect-ratio style; your values win.
...propsReact.ComponentProps<"div">—All native div attributes.

Accessibility

  • Presentational wrapper; it adds no roles or landmarks.
  • Children should fill the box (for example className="size-full") or they will only occupy their intrinsic size. Use object-cover on images to avoid distortion.

Vue and plain HTML

This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add aspect-ratio --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { computed, type StyleValue } from "vue";

import { cn } from "@/lib/utils";

const props = withDefaults(
  defineProps<{
    ratio?: number;
    style?: StyleValue;
    class?: string;
  }>(),
  { ratio: 16 / 9 },
);

const classes = computed(() =>
  cn("relative w-full overflow-hidden", props.class),
);
</script>

<template>
  <div
    data-slot="aspect-ratio"
    :style="[{ aspectRatio: String(ratio) }, props.style]"
    :class="classes"
  >
    <slot />
  </div>
</template>

On this page