Sub topic 7
Using Arbitrary Values with hover, Breakpoints and Other Variants
What you need before this chapter
This chapter assumes you can already do the following:
- Write a square-bracket arbitrary value such as
w-[317px], with underscores for spaces andlength:orcolor:type hints when needed. - Write an arbitrary property such as
[mask-type:luminance]. - Use named variants such as
hover:andmd:.
The examples work in Tailwind 3.4 and in Tailwind 4. Where the two differ, the text says so.
Variants and values are independent
A class has two independent parts. The variant prefixes (hover:, md:, dark:) decide when the rule applies. The utility with its value (bg-[#bada55]) decides what the rule sets. Brackets belong to the second part, so every variant works with every arbitrary value or property.
Take md:hover:w-[calc(100%-2rem)]. Tailwind splits it at the colons that sit outside brackets. It finds two variants, md and hover, and one utility, w, with the arbitrary value calc(100%-2rem). Colons inside brackets do not split the class. That is why [mask-type:luminance] and supports-[display:grid]:grid parse correctly.
The generated rule is roughly the following. Tailwind inserts the spaces around the - in calc() for you.
@media (min-width: 768px) {
.md\:hover\:w-\[calc\(100\%-2rem\)\]:hover {
width: calc(100% - 2rem);
}
}State variants: hover, focus and active
Put the variant in front of the bracketed utility. The rule applies only in that state.
<button
class="w-[220px] bg-[#1e3a8a] text-white
transition-[background-color,transform] duration-200
hover:bg-[#bada55] hover:text-[#111]
active:scale-[0.97]
focus-visible:ring-[3px] focus-visible:ring-[#f59e0b]">
Save
</button>Four details in this example matter.
transition-[background-color,transform]is an arbitrary value fortransition-property. A comma list contains no spaces, so it needs no underscores.- The transition sits on the base element, not behind
hover:. If you put it only onhover:, the element animates when the pointer enters but jumps back when the pointer leaves. The base element owns the transition, and the variants only change values. ring-[3px]andring-[#f59e0b]share a prefix. Tailwind decides from the value that the first is a width and the second is a color. When the value is a CSS variable, this inference fails. In that case usering-[length:var(--w)]orring-[color:var(--c)], as covered in the type hints chapter.- Variant rules always come after the base utilities in the generated CSS.
hover:bg-[#bada55]therefore beatsbg-[#1e3a8a]no matter which one you write first in the class attribute.
Responsive variants
Breakpoint variants are mobile-first. An unprefixed utility applies at every width. A prefixed one applies from that breakpoint upward. Arbitrary values follow the same rule, so you can set a different one-off value at each breakpoint:
<div class="absolute top-[117px] left-[calc(50%-2rem)] w-[317px]
md:top-[160px] lg:left-[calc(50%-6rem)]">
</div>This element sits 117px from the top until the md breakpoint, then 160px. Its left offset changes again at lg. The width stays 317px throughout.
Arbitrary breakpoints
Sometimes the named breakpoints (sm, md, lg, xl, 2xl) do not match a design. min-[...] and max-[...] take any length and generate a media query:
<aside class="hidden min-[900px]:block min-[900px]:w-[280px] max-[420px]:p-[10px]">
Sidebar
</aside>| Class | Generated condition |
|---|---|
| `min-[900px]:block` | `@media (min-width: 900px)` |
| `max-[420px]:p-[10px]` | `@media (width < 420px)` in Tailwind 4, `@media not all and (min-width: 420px)` in some 3.x builds |
| `md:block` | `@media (min-width: 768px)` with the default theme |
The exact text of the max- query differs between versions, but the meaning is the same. Use an arbitrary breakpoint for a single component that needs one odd threshold. If you find yourself typing min-[900px] in many places, define a named breakpoint in the theme instead.
When you mix arbitrary and named breakpoints on the same property, check the generated CSS. Tailwind sorts breakpoint rules so that larger ones win, but its handling of arbitrary values has changed across versions. For example, min-[900px]:w-[280px] and lg:w-[320px] set the same property at nearby widths. Pick one style per element, or inspect the output.
Dark mode and other condition variants
dark: takes arbitrary values like any other variant:
<div class="bg-[#f8fafc] dark:bg-[#0b1020] dark:hover:bg-[#111a33]"></div>The same holds for the attribute- and feature-based variants. Brackets can appear in the variant itself and in the utility:
<div data-state="open"
class="data-[state=open]:bg-[#e0f2fe] data-[state=closed]:opacity-[0.6]">
Panel
</div>
<div class="block supports-[display:grid]:grid supports-[display:grid]:grid-cols-[1fr_320px]">
<div>Content</div>
<div>Aside</div>
</div>data-[state=open]:matches an element withdata-state="open".supports-[display:grid]:wraps the rule in@supports (display: grid), so browsers without grid ignore it.
Group and peer variants work the same way. group-hover: reacts to a parent's state. peer-checked: reacts to an earlier sibling marked peer:
<label class="flex gap-[8px]">
<input type="checkbox" class="peer">
<span class="peer-checked:text-[#15803d] peer-checked:underline">Subscribe</span>
</label>The peer element must come before the element that reacts to it in the HTML, because CSS sibling selectors only look forward. Where you need to style a parent from a child, use has-[...] (Tailwind 3.4 and later):
<li class="has-[:checked]:bg-[#eef2ff]">
<input type="checkbox"> Option
</li>Writing your own selector or at-rule inside brackets is covered in the next chapter on arbitrary variants.
Arbitrary properties under variants
Arbitrary properties accept variants too, because the bracketed declaration is just another utility. This is the main way to make a property with no utility respond to state or width:
<div class="[mask-type:luminance] hover:[mask-type:alpha]"></div>A particularly useful pattern is to put the responsive logic into a CSS variable and read it elsewhere:
<section class="[--scroll-offset:56px] lg:[--scroll-offset:44px] scroll-mt-[var(--scroll-offset)]">
...
</section>Only the variable changes at lg. The utility scroll-mt-[var(--scroll-offset)] is written once. This keeps the class list short when many properties depend on one value. Here is a card whose padding and gap both follow one spacing variable:
<article class="[--space:12px] md:[--space:24px] p-[var(--space)] gap-[var(--space)] flex flex-col">
<h2>Title</h2>
<p>Body</p>
</article>At the md breakpoint, only --space changes. Both p-[var(--space)] and gap-[var(--space)] pick up the new value. The two utilities have no variant prefix at all.
Properties like [grid-auto-flow:dense] also work under a variant, as in lg:[grid-auto-flow:dense].
Stacking variants
You can chain as many variants as you need. Each one adds a condition, and the rule applies only when all of them hold:
<a class="text-[#334155]
md:text-[#0f172a]
md:hover:text-[#1d4ed8]
dark:md:hover:text-[#93c5fd]">
Docs
</a>Read dark:md:hover:text-[#93c5fd] as "in dark mode, from md upward, when hovered". Order within the chain rarely changes the result for the variants in this chapter. Tailwind 3 and Tailwind 4 differ in how they apply a chain to the more unusual variants such as *: and file:. Tailwind 3 reads the chain right to left, and Tailwind 4 reads it left to right. If you rely on those, check your version's documentation.
Chains also make class lists long. When you stack more than three variants on several properties, the markup is hard to read. Consider moving the element into a component, or into a theme value with a name.
Why variant plus dynamic value fails
Tailwind scans your files as plain text and generates CSS only for complete class strings it finds. Variants make the problem easier to hit, because there are now two places to build a string: the variant and the value.
const hex = "bada55";
const state = "hover";
// Not detected: neither string appears in full in the source
const a = `hover:bg-[#${hex}]`;
const b = `${state}:bg-[#bada55]`;Neither hover:bg-[#bada55] nor any other full class appears in the file, so Tailwind generates no CSS. The page loads and no error is reported. The element simply never changes on hover.
The fix is to write each full class name in the source, then choose between them at runtime:
const tones = {
info: "hover:bg-[#e0f2fe]",
danger: "hover:bg-[#fee2e2]",
};
function styleAlert(element, kind) {
element.className = `p-[12px] ${tones[kind]}`;
}Both classes appear in full, so both are generated, and only the matching one is applied.
When the value really is computed at runtime, such as a percentage from an API, do not build a class. Set a CSS variable with JavaScript and let one static class read it:
<div id="bar" class="h-[8px] md:h-[12px] w-[var(--progress)] [--progress:0%] bg-[#2563eb]"></div>function setProgress(element, percent) {
element.style.setProperty("--progress", `${percent}%`);
}
setProgress(document.querySelector("#bar"), 64);The class list never changes. [--progress:0%] supplies a default, and the inline style set by setProgress overrides it. The bar is 8px tall on small screens and 12px from md upward, and its width tracks the variable.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Hover or breakpoint style never applies and no CSS is generated | The class is built with string interpolation | Write the whole class in the source, or use a lookup map |
| Value is ignored under a variant but works without it | The class name has a typo, or the variant needs a parent or sibling marker such as `group` or `peer` | Check the class in the output CSS; add the marker |
| `ring-[var(--x)]` sets the wrong property | Ambiguous value | Add a type hint: `ring-[color:var(--x)]` |
| Hover snaps back with no animation | `transition` placed only on the `hover:` class | Move the transition to the base element |
| Spaces break the class | A literal space inside brackets ends the class | Use `_` where you need a space |
| `peer-checked:` does nothing | The `peer` element comes after the reacting element | Reorder the HTML |
When a variant with a value is the wrong tool
A one-off like md:top-[160px] is fine. The pattern becomes a problem when the same bracketed value repeats. If hover:bg-[#bada55] appears in twenty places, the color has become part of your design system, and it should be a theme color with a name such as bg-accent. A single theme change then updates every use. The final chapter covers the decision in detail.
Exercise
Why does ` md:w-[${px}px] produce no CSS, and what is one way to fix it when px` comes from user input at runtime?
Show answer
Tailwind reads source files as text and never runs your code, so the complete class never appears in the source and nothing is generated. Instead of building the class, write a static class such as md:w-[var(--w)] and set the variable at runtime with element.style.setProperty("--w", px + "px"). Alternatively, if there are only a few possible widths, list every complete class in a map and look one up.
Exercise
Write classes for a <div> that sets --gap to 8px by default and 20px from the lg breakpoint, and that uses --gap as its flex gap.
Show answer
flex [--gap:8px] lg:[--gap:20px] gap-[var(--gap)]. The arbitrary property defines the variable, lg: changes it at the breakpoint, and the arbitrary value reads it.
Exercise
A button should turn #bada55 on hover, animate the color change over 300ms, and use a 3px ring only for keyboard focus. Which variant goes on each class, and where does the transition go?
Show answer
bg-[#1e3a8a] transition-[background-color] duration-[300ms] hover:bg-[#bada55] focus-visible:ring-[3px]. The transition and duration have no variant, so they stay on the base element and the color animates in both directions. Only the color change and the ring carry hover: and focus-visible:.