<script lang="ts">
import { AspectRatio } from "$lib/components/ui/aspect-ratio/index.js";
</script>
<AspectRatio ratio={16 / 9} class="bg-muted rounded-lg">
<img
src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80"
alt="Gray by Drew Beamer"
class="h-full w-full rounded-lg object-cover dark:brightness-[0.2] dark:grayscale"
/>
</AspectRatio> Installation
pnpm dlx jsrepo@latest add aspect-rationpx jsrepo@latest add aspect-ratioyarn dlx jsrepo@latest add aspect-ratiobunx jsrepo@latest add aspect-ratioInstall bits-ui:
pnpm add bits-ui -Dnpm install bits-ui -Dyarn add bits-ui -Dbun add bits-ui -DCopy and paste the following code into your project.
<script lang="ts">
import { AspectRatio as AspectRatioPrimitive } from 'bits-ui';
import { cn } from '$lib/utils.js';
let {
ref = $bindable(null),
class: className,
...restProps
}: AspectRatioPrimitive.RootProps = $props();
</script>
<AspectRatioPrimitive.Root
bind:ref
data-slot="aspect-ratio"
class={cn('overflow-hidden border border-border bg-background text-foreground', className)}
{...restProps}
/>
Usage
<script lang="ts">
import { AspectRatio } from '$lib/components/ui/aspect-ratio/index.js';
</script> <div class="w-[450px]">
<AspectRatio ratio={16 / 9} class="bg-muted">
<img src="..." alt="..." class="rounded-md object-cover" />
</AspectRatio>
</div>