Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Aspect Ratio

Photo by Drew Beamer

Installation

Usage

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>

16:9 Ratio (Default Video)

16:9 Aspect Ratio

4:3 Ratio (Classic Photo)

Photo by Kari Shea

1:1 Ratio (Square)

Photo by Ayo Ogunseinde

21:9 Ratio (Ultrawide)

Photo by Simon Berger

With Video

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:

Prop Type Default Toggle details
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-wrapper selectors and aspectRatioWrapper imports with aspect-ratio and aspectRatio. Review width, padding and border placement. Add w-full or 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-cover for media and add overflow-hidden when you want clipping. Caller styles can override --ratio or aspect-ratio.
  • The component now owns data-slot="aspect-ratio". Use another data-* attribute for a caller hook. Vue and Svelte skip children for void tags.
  • In React, the as element now sets the accepted props, so as="img" takes src and as="a" takes href. Svelte AspectRatio gets the same types. If a React wrapper types its props with ComponentProps<typeof AspectRatio>, use AspectRatioProps instead. In Svelte, if you spread typed props next to an as that is not div, type them as AspectRatioProps<"tag">, for example AspectRatioProps<"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.ts file in the component folder.

v1.0.0

  • Initial release with starwind v1.10.0