An arbitrary variant lets you create custom CSS selectors inside square brackets to apply styles conditionally, beyond the built-in variants like hover or focus.
Click to reveal answer
intermediate
How do you write an arbitrary variant for a custom selector in Tailwind?
You write it inside square brackets before a colon, like [&:has(input:checked)]:bg-green-500, which applies the style when the custom selector matches.
Click to reveal answer
intermediate
Why use arbitrary variants instead of regular classes?
They let you target complex or dynamic states in CSS that Tailwind's default variants don't cover, like parent selectors or custom pseudo-classes.
Click to reveal answer
intermediate
Example: What does [&:has(input:checked)]:bg-blue-200 do?
It applies a blue background to the element only if it contains an input element that is checked, using the CSS :has() selector inside an arbitrary variant.
Click to reveal answer
advanced
Can arbitrary variants be combined with responsive prefixes in Tailwind?
Yes! You can combine them like md:[&:hover]:text-red-500 to apply styles on hover but only on medium screens and up.
Click to reveal answer
How do you start writing an arbitrary variant in Tailwind CSS?
AWith curly braces {} inside a class
BWith parentheses () after a class
CWith square brackets [] before a colon
DWith angle brackets <> around a selector
✗ Incorrect
Arbitrary variants use square brackets before a colon, like [&:hover]:bg-red-500.
Which CSS selector can you use inside an arbitrary variant to style a parent based on a child state?
A:has()
B:hover
C:focus
D:active
✗ Incorrect
The :has() selector lets you select a parent based on child elements, useful inside arbitrary variants.
What does sm:[&:focus-visible]:outline-none do?
ARemoves outline on hover for all screens
BAdds outline on focus-visible for small screens only
CAdds outline on focus for all screens
DRemoves outline on focus-visible for small screens and up
✗ Incorrect
The sm: prefix applies styles on small screens and larger, combined with the arbitrary variant [&:focus-visible].
Can arbitrary variants target pseudo-elements like ::before or ::after?
ANo, Tailwind does not support pseudo-elements
BYes, by writing them inside square brackets
COnly with JavaScript
DOnly with inline styles
✗ Incorrect
You can target pseudo-elements inside arbitrary variants, for example [&::before]:content-['Hello'].
Which of these is a valid arbitrary variant syntax?
A[&:hover]:text-green-600
B(hover):text-green-600
C{hover}:text-green-600
D<hover>:text-green-600
✗ Incorrect
Only square brackets are valid for arbitrary variants in Tailwind.
Explain how arbitrary variants in Tailwind CSS help you style elements with custom selectors.
Think about how you can write selectors that Tailwind does not provide by default.
You got /4 concepts.
Describe a real-life scenario where you would use an arbitrary variant with a custom selector in Tailwind.
Consider a form with checkboxes that change the container style.
You got /4 concepts.
Practice
(1/5)
1. What does an arbitrary variant in Tailwind CSS allow you to do?
easy
A. Use custom CSS selectors inside Tailwind classes
B. Write JavaScript code inside Tailwind classes
C. Change Tailwind's default color palette
D. Disable Tailwind's responsive features
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 A
Quick Check:
Arbitrary variants = custom selectors [OK]
Hint: Look for custom selector usage in square brackets [OK]
Common Mistakes:
Thinking arbitrary variants change colors
Confusing with JavaScript or responsive features
Assuming it disables Tailwind features
2. Which of the following is the correct syntax to apply a hover style using an arbitrary variant in Tailwind?
easy
A. hover:[&:hover]:text-blue-500
B. [&:hover]:text-blue-500
C. hover:text-blue-500
D. [hover]:text-blue-500
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 B
Quick Check:
Arbitrary variant syntax = [&:hover]:utility [OK]
Hint: Look for square brackets with & and pseudo-class inside [OK]
Common Mistakes:
Adding 'hover:' before arbitrary variant
Using brackets without &
Misplacing brackets around 'hover'
3. What will be the effect of this Tailwind class on a button element?