Aspect Ratio
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={16 / 9}> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Photo by Drew Beamer" class="rounded-md object-cover w-full h-full" /></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <AspectRatio ratio={16 / 9}> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Photo by Drew Beamer" className="rounded-md object-cover w-full h-full" /> </AspectRatio> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="16 / 9"> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Photo by Drew Beamer" class="rounded-md object-cover w-full h-full" /> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<div class="w-full max-w-md p-8"> <AspectRatio ratio={16 / 9}> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Abstract background by Drew Beamer" class="rounded-md object-cover w-full h-full" /> </AspectRatio></div>Installation
pnpx starwind@latest add aspect-ratio --framework astronpx starwind@latest add aspect-ratio --framework astroyarn dlx starwind@latest add aspect-ratio --framework astropnpx starwind@latest add aspect-ratio --framework reactnpx starwind@latest add aspect-ratio --framework reactyarn dlx starwind@latest add aspect-ratio --framework reactpnpx starwind@latest add aspect-ratio --framework vuenpx starwind@latest add aspect-ratio --framework vueyarn dlx starwind@latest add aspect-ratio --framework vuepnpx starwind@latest add aspect-ratio --framework sveltenpx starwind@latest add aspect-ratio --framework svelteyarn dlx starwind@latest add aspect-ratio --framework svelteUsage
AspectRatio renders one element with a preferred CSS aspect ratio. The default ratio is 1. Its class, styles and native attributes apply to that element. Use as to choose its HTML tag.
Images can fill the box with h-full w-full object-cover. For a centered flex or grid parent, add w-full or an explicit width to AspectRatio. Tall in-flow content can grow the box; add overflow-hidden when you want clipping and the preferred height.
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={16 / 9}> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Photo by Drew Beamer" class="rounded-md object-cover w-full h-full" /></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <AspectRatio ratio={16 / 9}> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Photo by Drew Beamer" className="rounded-md object-cover w-full h-full" /> </AspectRatio> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="16 / 9"> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Photo by Drew Beamer" class="rounded-md object-cover w-full h-full" /> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<AspectRatio ratio={16 / 9}> <img src="https://images.unsplash.com/photo-1588345921523-c2dcdb7f1dcd?w=800&dpr=2&q=80" alt="Scene by Drew Beamer" class="rounded-md object-cover w-full h-full" /></AspectRatio>16:9 Ratio (Default Video)
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={16 / 9}> <div class="flex items-center justify-center w-full h-full bg-muted rounded-md"> <span class="text-sm text-muted-foreground">16:9 Aspect Ratio</span> </div></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <> <AspectRatio ratio={16 / 9}> <div className="flex items-center justify-center w-full h-full bg-muted rounded-md"> <span className="text-sm text-muted-foreground">16:9 Aspect Ratio</span> </div> </AspectRatio> </> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="16 / 9"> <div class="flex items-center justify-center w-full h-full bg-muted rounded-md"> <span class="text-sm text-muted-foreground">16:9 Aspect Ratio</span> </div> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<AspectRatio ratio={16 / 9}> <div class="flex items-center justify-center w-full h-full bg-muted rounded-md"> <span class="text-sm text-muted-foreground">16:9 Aspect Ratio</span> </div></AspectRatio>4:3 Ratio (Classic Photo)
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={4 / 3}> <img src="https://images.unsplash.com/photo-1535025183041-0991a977e25b?w=800&dpr=2&q=80" alt="Photo by Kari Shea" class="rounded-md object-cover w-full h-full" /></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <> <AspectRatio ratio={4 / 3}> <img src="https://images.unsplash.com/photo-1535025183041-0991a977e25b?w=800&dpr=2&q=80" alt="Photo by Kari Shea" className="rounded-md object-cover w-full h-full" /> </AspectRatio> </> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="4 / 3"> <img src="https://images.unsplash.com/photo-1535025183041-0991a977e25b?w=800&dpr=2&q=80" alt="Photo by Kari Shea" class="rounded-md object-cover w-full h-full" /> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<AspectRatio ratio={4 / 3}> <img src="https://images.unsplash.com/photo-1535025183041-0991a977e25b?w=800&dpr=2&q=80" alt="Scene by Kari Shea" class="rounded-md object-cover w-full h-full" /></AspectRatio>1:1 Ratio (Square)
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={1}> <img src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=800&dpr=2&q=80" alt="Photo by Ayo Ogunseinde" class="rounded-md object-cover w-full h-full" /></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <> <AspectRatio ratio={1}> <img src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=800&dpr=2&q=80" alt="Photo by Ayo Ogunseinde" className="rounded-md object-cover w-full h-full" /> </AspectRatio> </> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="1"> <img src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=800&dpr=2&q=80" alt="Photo by Ayo Ogunseinde" class="rounded-md object-cover w-full h-full" /> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<AspectRatio ratio={1}> <img src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=800&dpr=2&q=80" alt="Scene by Ayo Ogunseinde" class="rounded-md object-cover w-full h-full" /></AspectRatio>21:9 Ratio (Ultrawide)
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={21 / 9}> <img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&dpr=2&q=80" alt="Photo by Simon Berger" class="rounded-md object-cover w-full h-full" /></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <> <AspectRatio ratio={21 / 9}> <img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&dpr=2&q=80" alt="Photo by Simon Berger" className="rounded-md object-cover w-full h-full" /> </AspectRatio> </> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="21 / 9"> <img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&dpr=2&q=80" alt="Photo by Simon Berger" class="rounded-md object-cover w-full h-full" /> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<AspectRatio ratio={21 / 9}> <img src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&dpr=2&q=80" alt="Scene by Simon Berger" class="rounded-md object-cover w-full h-full" /></AspectRatio>With Video
---import { AspectRatio } from "@/components/starwind/aspect-ratio";---
<AspectRatio ratio={16 / 9}> <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" class="w-full h-full rounded-md" ></iframe></AspectRatio>import { AspectRatio } from "@/components/starwind/aspect-ratio";
export function Example() { return ( <> <AspectRatio ratio={16 / 9}> <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" className="w-full h-full rounded-md" ></iframe> </AspectRatio> </> );}<script setup lang="ts">import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<template> <AspectRatio :ratio="16 / 9"> <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player" allow=" accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; " class="w-full h-full rounded-md" ></iframe> </AspectRatio></template><script lang="ts"> import { AspectRatio } from "@/components/starwind/aspect-ratio";</script>
<AspectRatio ratio={16 / 9}> <iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player" allow="\n accelerometer;\n autoplay;\n clipboard-write;\n encrypted-media;\n gyroscope;\n picture-in-picture;\n " class="w-full h-full rounded-md" ></iframe></AspectRatio>Migrating to version 2
This update removes the outer wrapper in Astro, React, Vue, and Svelte. Update the copied component, variants.ts, and index.ts together. The default ratio remains 1.
Replace [data-slot="aspect-ratio-wrapper"] selectors with [data-slot="aspect-ratio"]. Replace direct aspectRatioWrapper imports and AspectRatioVariants.aspectRatioWrapper() calls with aspectRatio and AspectRatioVariants.aspectRatio(). The element now owns its data-slot; use a separate data-* attribute for a caller hook.
The old wrapper always applied w-full. A normal block fills its host, while a flex or grid parent with centered children can need w-full or an explicit width. An overlay-only box can collapse without that width. Put sizing, padding and border classes on AspectRatio or keep a caller-owned host when the layout needs a separate padded frame.
Styles merge after the default --ratio. A caller’s --ratio overrides the ratio prop; an explicit aspect-ratio style overrides the class. Remove the override to use the current prop again. Astro accepts style strings or objects, React accepts objects, Vue accepts its native style strings, objects and arrays, and Svelte accepts strings.
Use absolute positioning for overlays. Native CSS supplies a preferred ratio, so tall in-flow content can grow the box. Optional overflow-hidden clips that content and keeps the preferred height. Explicit dimensions follow normal CSS sizing.
A React ref and Svelte bind:ref reach the single element. A Vue component ref exposes it through $el. Omit children when as selects a void tag, such as img; Vue and Svelte also skip children for void tags.
See the full migration guide for selector, import and layout examples.
API Reference
Styled Component API
These props are added or materially changed by the installed styled component. Standard HTML attributes remain available through the inherited interfaces noted below. Expand a prop to see named type definitions and framework-specific imports. Follow the Primitive and Runtime links for lower-level behavior props.
Aspect Ratio
Inherits div attributes.
Contains the following additional props:
as keyof HTMLElementTagNameMap "div"
- Description
- Selects the rendered element or component.
- Classification
- Wrapper prop
ratio number 1
- Description
- Sets the rendered width-to-height ratio.
- Classification
- Wrapper prop
Changelog
View version history v2.0.0 4 releases
v2.0.0
- AspectRatio now renders one element in Astro, React, Vue, and Svelte. Classes, styles, attributes and refs reach the element that controls the ratio. The default ratio remains square.
- Replace
aspect-ratio-wrapperselectors andaspectRatioWrapperimports withaspect-ratioandaspectRatio. Review width, padding and border placement. Addw-fullor an explicit width in flex or grid parents that do not stretch their children. - Native CSS supplies a preferred ratio. Tall in-flow content can grow the box. Use
h-full w-full object-coverfor media and addoverflow-hiddenwhen you want clipping. Caller styles can override--ratiooraspect-ratio. - The component now owns
data-slot="aspect-ratio". Use anotherdata-*attribute for a caller hook. Vue and Svelte skip children for void tags. - In React, the
aselement now sets the accepted props, soas="img"takessrcandas="a"takeshref. Svelte AspectRatio gets the same types. If a React wrapper types its props withComponentProps<typeof AspectRatio>, useAspectRatioPropsinstead. In Svelte, if you spread typed props next to anasthat is notdiv, type them asAspectRatioProps<"tag">, for exampleAspectRatioProps<"img">. - In Vue, this component now needs Vue 3.5.21 or newer.
- Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.
v1.1.0
- The component is now available for React as well as Astro. Its props and styles stay the same.
v1.0.3
- The Tailwind variants now live in their own
variants.tsfile in the component folder.
v1.0.0
- Initial release with starwind v1.10.0