Aspect Ratio
No more squished cats. Keeps its content at a given width-to-height ratio.
Demo



Installation
Usage
<script setup lang="ts">
import { AspectRatio } from '@/components/ui/aspect-ratio'
</script>
<template>
<AspectRatio :ratio="16 / 9" class="overflow-hidden rounded-lg bg-muted">
<img src="/photo.jpg" alt="" class="size-full object-cover" />
</AspectRatio>
</template>
Set ratio as width divided by height: 16 / 9, 1, 4 / 3.
API Reference
AspectRatio
Props
| Prop | Default | Type and description |
|---|---|---|
| ratio | 1 | numberWidth divided by height, such as |
| asChild | — | booleanRender the single child as the inner box instead of a wrapper. |
| as | — | AsTag | ComponentThe element or component to render as the inner box. |
Slots
| Slot | Scope and description |
|---|---|
| default | { aspect: number; }Content of the inner box. Receives |
Props
- ratio
1 numberWidth divided by height, such as16 / 9.- asChild
booleanRender the single child as the inner box instead of a wrapper.- as
AsTag | ComponentThe element or component to render as the inner box.
Slots
- default
{ aspect: number; }Content of the inner box. Receivesaspect, the height as a percentage of the width.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| aspect-ratio | — | The inner box, where your attributes land. |
Changelog
No changes yet.