Components
Aspect Ratio
Locks a box to a width-to-height ratio while it resizes.
Installation
$ npx veil-ui@latest add aspect-ratioUsage
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
| Prop | Type | Default | Description |
|---|---|---|---|
| ratio | number | 16 / 9 | Width divided by height. |
| style | React.CSSProperties | — | Merged with the aspect-ratio style; your values win. |
| ...props | React.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. Useobject-coveron 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>