Presets

Blocks

Get PRO

Need premium blocks and templates? Upgrade to PRO and get access.

Upgrade
JoinLogin
Presets
Monochrome
Overview
  • Introduction
  • Components
  • Installation
Components
  • Accordion
  • Alert
  • Alert Dialog
  • Aspect Ratio
  • Avatar
  • Badge
  • Breadcrumb
  • Button
  • Button Group
  • CalendarNEW
  • Card
  • CarouselNEW
  • Checkbox
  • Collapsible
  • Dialog
  • Dropdown Menu
  • Empty
  • Field
  • Input
  • Input Group
  • Item
  • Kbd
  • Label
  • Native Select
  • Scroll Area
  • SelectNEW
  • Separator
  • SwitchNEW
  • Table
  • Tabs
  • Textarea
  • TooltipNEW

Aspect Ratio

PreviousNext

Displays content within a desired ratio.

1<script lang="ts">
2import { AspectRatio } from "@/components/ui/aspect-ratio"
3</script>
4

Installation

pnpm dlx sprawlify@latest add aspect-ratio
npx sprawlify@latest add aspect-ratio
yarn sprawlify@latest add aspect-ratio
bunx --bun sprawlify@latest add aspect-ratio

Install the following dependencies:

pnpm add @sprawlify/primitives @sprawlify/svelte
npm install @sprawlify/primitives @sprawlify/svelte
yarn add @sprawlify/primitives @sprawlify/svelte
bun add @sprawlify/primitives @sprawlify/svelte

Add the following files to your project:

aspect-ratio/aspect-ratio.svelte
1<script lang="ts">
2import { AspectRatio as AspectRatioPrimitive } from "@sprawlify/svelte/aspect-ratio"
3import type { ComponentProps } from "svelte";
4
5interface Props extends ComponentProps<typeof AspectRatioPrimitive.Root> {}
6
7let { children, ...rest }: Props = $props()
8</script>
9
10<AspectRatioPrimitive.Root data-slot="aspect-ratio" {...rest}>
11  <AspectRatioPrimitive.Content data-slot="aspect-ratio-content">
12    {@render children?.()}
13  </AspectRatioPrimitive.Content>
14</AspectRatioPrimitive.Root>
aspect-ratio/index.ts
1export { default as AspectRatio } from "./aspect-ratio.svelte";2

Update the import paths to match your project setup.

Usage

1import { AspectRatio } from "@/components/ui/aspect-ratio"
1<AspectRatio ratio={16 / 9} class="bg-muted">
2  <img src="..." class="size-full rounded-md object-cover" />
3</AspectRatio>

Examples

Portrait

A portrait aspect ratio component using the ratio={9 / 16} prop. This is useful for displaying images in a portrait format.

1<script lang="ts">
2import { AspectRatio } from "@/components/ui/aspect-ratio"
3</script>
4

Square

A square aspect ratio component using the ratio={1 / 1} prop. This is useful for displaying images in a square format.

1<script lang="ts">
2import { AspectRatio } from "@/components/ui/aspect-ratio"
3</script>
4

On This Page

InstallationUsageExamplesPortraitSquare

Get PRO

Need premium blocks and templates? Upgrade to PRO and get access.