aspect-ratio

Class Description

aspect

Sets aspect-ratio via --aspect. E.g. 16/9, 4/3.

CSS Rules

.aspect {
  aspect-ratio: var(--aspect);
}

Usage

The .aspect class is flagged as a value-driven selector. The variable --aspect is required.
<element class="aspect" style="--aspect: /* value */;" />

Responsive

Prefix .aspect utility with a breakpoint variant (e.g., md:) to apply styles at specific screen sizes (Supported breakpoints are xm through 2xl).

<element class="md:aspect" style="--md-aspect: /* value */;" />

States

States are generated dynamically at runtime and always come last. Append a state like :hover or :focus to the end of the class. This works in conjunction with breakpoints .

Supported pseudo-classes: :hover, :active, :focus, :focus-visible, :focus-within, :target, :checked, :disabled, and :group-hover.

<element class="aspect:hover" style="--aspect-hover: /* value */;" />

Group Hover Example

The :group-hover state applies styles when a parent element with the .group class is hovered.

<element class="group">
  <element class="aspect:group-hover">...</element>
</element>

Pseudo-Elements

The ::before and ::after utility classes are supported too. Granted that you add the mandatory content-before or content-after class in order for them to work.

<element class="content-before aspect::before" style="--aspect-before: /* value */;" />

<element class="content-after aspect::after" style="--aspect-after: /* value */;" />

Shorthand

Value-driven classes support a shorthand syntax like bg=pink or p=4. The runtime processes this for you. Unlike :hover that is generating CSS on the fly, shorthand only splits it and appends the corresponding style.

<!-- Input -->
<element class="aspect=value" />

<!-- Output -->
<element class="aspect" style="--aspect: value;" />

CLS Alert

This feature may cause CLS (Cumulative Layout Shift). Make sure to include a z-not-ready class in your <head> tag.

.aspect Sets aspect-ratio via --aspect. E.g. 16/9, 4/3.

aspect-square

Sets aspect-ratio: 1 / 1.

CSS Rules

.aspect-square {
  aspect-ratio: 1/1;
}

Usage

<element class="aspect-square" />

Responsive

Prefix .aspect-square utility with a breakpoint variant (e.g., md:) to apply styles at specific screen sizes (Supported breakpoints are xm through 2xl).

<element class="md:aspect-square" />

States

States are generated dynamically at runtime and always come last. Append a state like :hover or :focus to the end of the class. This works in conjunction with breakpoints .

Supported pseudo-classes: :hover, :active, :focus, :focus-visible, :focus-within, :target, :checked, :disabled, and :group-hover.

<element class="aspect-square:hover" />

Group Hover Example

The :group-hover state applies styles when a parent element with the .group class is hovered.

<element class="group">
  <element class="aspect-square:group-hover">...</element>
</element>

Pseudo-Elements

The ::before and ::after utility classes are supported too. Granted that you add the mandatory content-before or content-after class in order for them to work.

<element class="content-before aspect-square::before" />

<element class="content-after aspect-square::after" />
.aspect-square Sets aspect-ratio: 1 / 1.

aspect-video

Sets aspect-ratio: 16 / 9.

CSS Rules

.aspect-video {
  aspect-ratio: var(--aspect-video);
}

Usage

<element class="aspect-video" />

Responsive

Prefix .aspect-video utility with a breakpoint variant (e.g., md:) to apply styles at specific screen sizes (Supported breakpoints are xm through 2xl).

<element class="md:aspect-video" />

States

States are generated dynamically at runtime and always come last. Append a state like :hover or :focus to the end of the class. This works in conjunction with breakpoints .

Supported pseudo-classes: :hover, :active, :focus, :focus-visible, :focus-within, :target, :checked, :disabled, and :group-hover.

<element class="aspect-video:hover" />

Group Hover Example

The :group-hover state applies styles when a parent element with the .group class is hovered.

<element class="group">
  <element class="aspect-video:group-hover">...</element>
</element>

Pseudo-Elements

The ::before and ::after utility classes are supported too. Granted that you add the mandatory content-before or content-after class in order for them to work.

<element class="content-before aspect-video::before" />

<element class="content-after aspect-video::after" />
.aspect-video Sets aspect-ratio: 16 / 9.

aspect-auto

Resets aspect ratio to auto so sizing follows intrinsic content and constraints.

CSS Rules

.aspect-auto {
  aspect-ratio: auto;
}

Usage

<element class="aspect-auto" />

Responsive

Prefix .aspect-auto utility with a breakpoint variant (e.g., md:) to apply styles at specific screen sizes (Supported breakpoints are xm through 2xl).

<element class="md:aspect-auto" />

States

States are generated dynamically at runtime and always come last. Append a state like :hover or :focus to the end of the class. This works in conjunction with breakpoints .

Supported pseudo-classes: :hover, :active, :focus, :focus-visible, :focus-within, :target, :checked, :disabled, and :group-hover.

<element class="aspect-auto:hover" />

Group Hover Example

The :group-hover state applies styles when a parent element with the .group class is hovered.

<element class="group">
  <element class="aspect-auto:group-hover">...</element>
</element>

Pseudo-Elements

The ::before and ::after utility classes are supported too. Granted that you add the mandatory content-before or content-after class in order for them to work.

<element class="content-before aspect-auto::before" />

<element class="content-after aspect-auto::after" />
.aspect-auto Resets aspect ratio to auto so sizing follows intrinsic content and constraints.