Tailwind CSS Arbitrary Value & Variant Builder

Construct highly complex, nested Tailwind CSS pseudo-class selector strings using a simple, visual dropdown interface.

Variant Stack

Use & to reference the base element. Use _ for spaces. E.g. [&_li]

bg-blue-500
CSS Specificity
0,1,0
ID CLASS ELEM
Force Dark Mode
Force Hover
Force Group Hover
Force Focus
/* CSS generated here */
/* JS generated here */

The PostCSS addVariant() API

When arbitrary variant strings become too long (e.g., group-hover:[&>div:nth-child(3)>svg]:fill-blue-500), they severely impact HTML readability. The official Tailwind best practice is to extract these complex DOM traversals into custom variants using the tailwind.config.js file.

By utilizing the PostCSS Plugin API, you can call addVariant(name, selector) to register a clean prefix. Our [PostCSS Plugin] generator tab automatically converts your arbitrary stack into raw JavaScript. For example, registering addVariant('custom-hover', '&:hover > div:nth-child(3) > svg') allows you to simply write custom-hover:fill-blue-500 in your HTML.

CSS Specificity Scoring Explained

We built a live Specificity Gauge into this tool because Tailwind's JIT compiler often loses CSS specificity battles when developers misuse arbitrary variants. Specificity is calculated as an array: [IDs, Classes/Attributes/Pseudo-classes, Elements/Pseudo-elements].

  • A standard Tailwind class like .bg-blue-500 has a score of 0,1,0.
  • Adding a pseudo-class like hover: creates .hover\:bg-blue-500:hover, pushing the score to 0,2,0.
  • However, if your variant relies on raw elements (e.g., [&_p]), the element tag only adds 1 to the lowest tier (0,1,1).

If your arbitrary class is not visually updating the DOM, check the Specificity Gauge. If a legacy CSS file has a rule with 0,2,0, it will override your 0,1,1 Tailwind class. The solution is to prefix your Tailwind utility with ! (e.g., !bg-blue-500) to inject the !important flag.

The Power of the Ampersand (&)

In CSS processors like SASS or PostCSS, the & symbol represents the current element. In Tailwind, this behaves exactly the same way. When you write [&_p]:text-red-500, the compiler replaces the & with the generated class name, and replaces the underscore _ with a space, resulting in .\[\&\_\p\]\:text-red-500 p.

Parent Selectors (has-) and Data Attributes

Tailwind has evolved beyond basic hover states. You can now style an element based on the state of its children or its custom data attributes.

  • Parent Selectors: has-[.active]:bg-blue-500 compiles to .has-\[ \.active \]:has(.active) { ... }. This means the element turns blue if *any* child inside it has the .active class.
  • Data Attributes: data-[state=open]:opacity-100 compiles to .data-\[state\=open\][data-state="open"] { ... }. This is the industry standard for wiring Radix UI, Headless UI, and other modern headless component libraries.

Responsive Arbitrary Data

The true power of Tailwind's variant stack is that it compiles recursively. This allows you to combine Responsive breakpoints with Data Attributes and Arbitrary values.

Example: md:data-[state=open]:[&_li]:opacity-100. Our JIT Sandbox Engine visually demonstrates how this translates into a responsive media query wrapping a complex data-attribute selector targeting a specific nested child tag.

Frequently Asked Questions

Tailwind Basics

What is a Tailwind CSS arbitrary value?
In Tailwind CSS, arbitrary values allow you to use custom values that are not included in your theme design system, by wrapping the value in square brackets, like `w-[32rem]` or `bg-[#bada55]`.

Tool Usage

How does this tool help with arbitrary variants?
This builder provides a visual interface to construct complex arbitrary variants, ensuring your syntax is correct and helping you combine multiple pseudo-classes, media queries, and attribute selectors seamlessly.

Compatibility

Can I use the generated variants in any Tailwind CSS project?
Yes, as long as your project is using Tailwind CSS v3.0 or higher, the arbitrary values and variants generated by this tool will work out-of-the-box via the Just-in-Time (JIT) compiler.

Rate Tailwind CSS Arbitrary Value & Variant Builder

Help us improve by rating this tool.

5.0/5
814 reviews