Course contentsShow
← Web Development
Lesson 642 of 2,98211. Utility-First CSS with TailwindFree lesson

Arbitrary Variants

Using square bracket notation to create one-off variants like [@media(orientation:portrait)]: for special cases.

Arbitrary Variants

What you'll learn: How to create custom, one-off variants in Tailwind CSS using square bracket notation for special cases that don't have built-in support.

The Core Idea

Tailwind CSS comes with many built-in variants like hover:, focus:, md:, and dark:. But what happens when you need to apply a style based on an unusual condition—like a specific media query for landscape orientation, or a unique @supports rule? Arbitrary variants let you write these one-off conditions directly in your class names using square brackets.

How It Works

The syntax follows the pattern: [your-custom-condition]:utility-class

For example, to apply different padding when the screen is in portrait mode:

<div class="[@media(orientation:portrait)]:p-8">
  Content
</div>

This compiles to a CSS rule that only applies when the media query matches—no need to write custom CSS or extend your Tailwind config.

Common Use Cases

  • Custom media queries: [@media(orientation:landscape)]:hidden
  • Feature detection: [@supports(display:grid)]:grid
  • Attribute selectors: [&[data-active="true"]]:bg-blue-500
  • Unique pseudo-classes: [@nth-child(3)]:text-red-500

Think of arbitrary variants like a "wildcard" escape hatch. The built-in variants cover 95% of your needs, but arbitrary variants handle that last 5% of edge cases without forcing you to leave Tailwind's utility system.

Key Takeaway: Arbitrary variants using [...] notation let you create one-off conditional styles for special cases like custom media queries or feature detection, keeping you in Tailwind's utility-first workflow even for unusual requirements.