1. Layout
  2. aspect-ratio

Layout

aspect-ratio

Utilities for controlling the aspect ratio of an element.

ClassStyles
aspect-<ratio>
aspect-ratio: <ratio>;
aspect-square
aspect-ratio: 1 / 1;
aspect-video
aspect-ratio: var(--aspect-ratio-video); /* 16 / 9 */
aspect-auto
aspect-ratio: auto;
aspect-(<custom-property>)
aspect-ratio: var(<custom-property>);
aspect-[<value>]
aspect-ratio: <value>;

Examples

Using a specific aspect ratio

Use aspect-<ratio> utilities like aspect-3/2 to give an element a specific aspect ratio:

Resize the example to see the expected behavior

<img class="aspect-3/2 object-cover ..." src="/img/villas.jpg" />

Using a named aspect ratio

Tailwind provides a few named aspect ratio utilities for common use cases. For example, use aspect-video to give a video element a 16 / 9 aspect ratio:

Resize the example to see the expected behavior

<iframe  class="aspect-video ..."  src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>

Referencing a variable

Use the aspect-(<custom-property>) syntax to set the aspect ratio based on a custom property:

<div class="aspect-(--my-aspect-ratio) ...">  <!-- ... --></div>

Using a custom value

Use the aspect-[<value>] syntax to set the aspect ratio based on a completely custom value:

<div class="aspect-[calc(4*3+1)/3] ...">  <!-- ... --></div>

Responsive design

Prefix an aspect-ratio utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<iframe  class="aspect-video md:aspect-square ..."  src="https://www.youtube.com/embed/dQw4w9WgXcQ"></iframe>

Learn more about using variants in the variants documentation.

Customizing your theme

Use the --aspect-* theme variables to customize the aspect ratio utilities in your project:

@theme {  --aspect-retro: 4 / 3; }

Now the aspect-retro utility can be used in your markup:

<div class="aspect-retro">  <!-- ... --></div>

Learn more about customizing your theme in the theme documentation.

Copyright © 2024 Tailwind Labs Inc.·Trademark Policy