Discover how to style exactly what you want without leaving your HTML!
Why Arbitrary variants for custom selectors in Tailwind? - Purpose & Use Cases
Imagine you want to style a button only when it is inside a special container or when a certain attribute is present, so you write CSS selectors by hand for each case.
Writing and maintaining these custom selectors manually is slow and error-prone. Every time the HTML structure changes, you must update your CSS selectors, which can break styles or cause unexpected results.
Arbitrary variants let you write any custom CSS selector directly inside Tailwind classes, so you can target exactly what you want without leaving your HTML or writing separate CSS files.
.special-container button { background-color: blue; }
[data-state='open'] > .menu { display: block; }[.special-container_&]:bg-blue-500 [[data-state='open']_>_.menu]:block
You can create precise, complex style rules inline with your HTML, making your code easier to read, maintain, and adapt.
For example, you want a card to have a shadow only when it is inside a modal with a certain class, so you use an arbitrary variant to apply the shadow only in that context.
Manual CSS selectors are hard to maintain and update.
Arbitrary variants let you write custom selectors inside Tailwind classes.
This makes styling complex states and contexts easier and faster.
Practice
Solution
Step 1: Understand Tailwind's arbitrary variants
Arbitrary variants let you write custom CSS selectors inside Tailwind by wrapping them in square brackets.Step 2: Compare options
Only Use custom CSS selectors inside Tailwind classes correctly describes this feature; others describe unrelated Tailwind features.Final Answer:
Use custom CSS selectors inside Tailwind classes -> Option AQuick Check:
Arbitrary variants = custom selectors [OK]
- Thinking arbitrary variants change colors
- Confusing with JavaScript or responsive features
- Assuming it disables Tailwind features
Solution
Step 1: Understand arbitrary variant syntax
Arbitrary variants use square brackets with a selector inside, e.g., [&:hover].Step 2: Identify correct usage for hover
[&:hover]:text-blue-500 uses the correct syntax: [selector]:utility, here [&:hover]:text-blue-500.Final Answer:
[&:hover]:text-blue-500 -> Option BQuick Check:
Arbitrary variant syntax = [&:hover]:utility [OK]
- Adding 'hover:' before arbitrary variant
- Using brackets without &
- Misplacing brackets around 'hover'
<button class="[&:focus-visible]:ring-2 [&:focus-visible]:ring-blue-600">Click me</button>
Solution
Step 1: Analyze the arbitrary variant selectors
The class uses [&:focus-visible] which applies styles only when the button is focused and visible.Step 2: Understand the applied styles
ring-2 and ring-blue-600 add a blue outline ring of thickness 2 on focus-visible state.Final Answer:
The button gets a blue ring when it is focused and visible -> Option CQuick Check:
Arbitrary variant with :focus-visible = ring on focus [OK]
- Thinking ring is always visible
- Confusing focus-visible with hover
- Ignoring the arbitrary variant selector
<div class="[&hover]:bg-red-500">Alert</div>
Solution
Step 1: Check the arbitrary variant syntax
The pseudo-class hover must be preceded by a colon and ampersand inside brackets: [&:hover].Step 2: Identify the mistake
The class uses [&hover] missing the colon, so it is invalid syntax.Final Answer:
Incorrect pseudo-class syntax, should be [&:hover] -> Option AQuick Check:
Pseudo-class needs colon after & [OK]
- Omitting colon before pseudo-class
- Using brackets incorrectly
- Assuming bg-red-500 is invalid
span inside a div only when the div is hovered, using Tailwind arbitrary variants. Which class should you add to the div?Solution
Step 1: Understand the selector goal
You want to style thespanchild only when thedivis hovered, so the hover is on the parent.Step 2: Choose correct arbitrary variant syntax
The selector [&:hover>span] means when the div is hovered, style its direct child span.Step 3: Check other options
[&>span:hover]:text-green-600 styles span on its own hover, not div's hover. hover:[&>span]:text-green-600 and D have incorrect syntax or order.Final Answer:
[&:hover>span]:text-green-600 -> Option DQuick Check:
Parent hover affecting child = [&:hover>span] [OK]
- Applying hover on child instead of parent
- Misplacing brackets or colons
- Using hover: outside arbitrary variant incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
