In Tailwind CSS v4, how do the `data-*` and `aria-*` variants style a save button by its `data-state` and an accordion by `aria-expanded`?
answer
- style follows an attribute
- brackets for a specific value
- bare name checks presence or true
- group-aria-* for the chevron
- arbitrary variants for the rest
basics
~10 sIn Tailwind CSS v4, data-[state=saved]: compiles to a [data-state="saved"] attribute selector and a bare data-saving: checks the attribute is present; aria-expanded: compiles to [aria-expanded="true"]. The script sets the attribute and the styles follow.
solid answer
~40 sAttribute variants let markup state drive styles, so JavaScript sets one attribute and never toggles extra classes. In Tailwind CSS v4, `data-[state=saved]:bg-emerald-600` compiles to `&[data-state="saved"]`; a **bare** name such as `data-saving:opacity-60` compiles to `&[data-saving]` and matches whenever the attribute is present, whatever its value. `aria-expanded:bg-gray-50` compiles to `&[aria-expanded="true"]`, the form for boolean ARIA states; attributes that carry other values use brackets, as in `aria-[sort=ascending]:`. Both combine with markers, so a chevron inside the accordion trigger can use `group-aria-expanded:rotate-180`. Using bare data attributes without any configuration is new in v4. For anything these variants cannot express, an arbitrary variant such as `[&.is-dragging]:cursor-grabbing` takes a selector directly.
go deeper
Recall that data-[name=value]: and aria-expanded: style an element from its attributes, and that the attribute itself still has to be set.
Explain what each form compiles to: an exact value in brackets, a bare data name as a presence check, and a bare aria name as the true value.
Remove duplicated class state by styling from the attributes a component already exposes, and use group-aria-* or group-data-* to reach the element that changes.
Define how components in a design system expose state as data and ARIA attributes so styling, testing and accessibility all read the same source.
## Why style from attributes Interactive components already carry their state in **attributes**. An accordion trigger sets `aria-expanded` for screen readers; a save button can expose `data-state="idle" | "saving" | "saved"`. If the styles read those attributes, the component has **one source of truth**: the script updates the attribute, and the look follows. The alternative, toggling extra classes alongside the attribute, means two pieces of state that can disagree. Tailwind's attribute variants turn an attribute selector into a prefix. ## The `data-*` variant There are two forms, and the difference catches people: | Class | Compiles to | Matches when | |---|---|---| | `data-[state=saved]:bg-emerald-600` | `&[data-state="saved"]` | the attribute has exactly that value | | `data-saving:opacity-60` | `&[data-saving]` | the attribute is present, any value | | `data-state:ring-2` | `&[data-state]` | `data-state` exists at all | The last row is the trap: `data-state:` looks like it means "some state is set" but matches **every** value, including `idle`. When you care about the value, use brackets. In v4, a bare name such as `data-saving:` works without any configuration; v3 needed configuration for named shortcuts, and v4 also lets you declare such shortcuts yourself, which is a custom-variant topic of its own. ```html <button data-state="saving" class="bg-gray-900 text-white data-[state=saving]:opacity-60 data-[state=saved]:bg-emerald-600"> Save recipe </button> ``` ## The `aria-*` variant A bare name targets the **true** value of an ARIA attribute: `aria-expanded:` compiles to `&[aria-expanded="true"]`. The docs list built-ins for the common boolean states: `aria-busy`, `aria-checked`, `aria-disabled`, `aria-expanded`, `aria-hidden`, `aria-pressed`, `aria-readonly`, `aria-required` and `aria-selected`. For attributes whose values are not booleans, brackets give the exact match, such as `aria-[sort=ascending]:` compiling to `&[aria-sort="ascending"]`. Because `aria-expanded:` matches only `"true"`, write the collapsed look **unprefixed** and the expanded look with the variant. An element with `aria-expanded="false"` simply gets the default. ## Reaching other elements: group and peer forms The state usually lives on the trigger, while a different element needs to change. The attribute variants compose with the markers: - `group-aria-expanded:rotate-180` on a chevron inside a trigger marked `group`; - `group-data-[state=saved]:visible` on a check icon inside a button marked `group`; - `peer-aria-*` and `peer-data-*` on a later sibling of an element marked `peer`. ```html <button aria-expanded="true" aria-controls="ingredients" class="group flex w-full justify-between aria-expanded:bg-gray-50"> Ingredients <svg class="size-4 transition group-aria-expanded:rotate-180"></svg> </button> ``` ## Why the two bare forms differ The asymmetry fits how each kind of attribute is used. `data-*` attributes are free-form, and many are flags whose mere presence is the signal, such as `data-active` or `data-loading`, so a bare name checks presence. ARIA states are mostly `true` or `false` tokens that stay on the element in both states, so `aria-expanded="false"` is as present as `"true"` and a presence check would tell you nothing; a bare ARIA name checks for `"true"` instead. ## Who sets the attribute The variants only **read** attributes; something else writes them: - your own script, setting `data-state` to `saving` while a request is in flight; - a component framework binding the attribute to state in its template; - a headless component library that already exposes `data-state` or `aria-expanded` on its parts. Whoever sets the attribute owns the state, and the classes only describe how each value looks. Tests can assert on the same attribute that the styles read. ## Stacking with other variants Attribute variants are ordinary variants, so they stack with the rest: `dark:aria-expanded:bg-gray-800`, `md:data-[state=saved]:bg-emerald-600`, or `hover:data-[state=idle]:bg-gray-700`. All of these are conditions on the same element, so their order in the stack does not change what they match. ## When nothing built in fits An **arbitrary variant** puts a selector in square brackets with `&` marking the element: `[&.is-dragging]:cursor-grabbing` compiles to `&.is-dragging`, and an underscore stands for a space, as in `[&_p]:mt-4`. Two related built-ins cover common native states: `open:` matches `[open]`, `:popover-open` and `:open` together, and `inert:` matches an inert element or anything inside one. ## Pitfalls to recognise 1. Expecting `data-state:` to test the value: it only tests presence. 2. Expecting `aria-expanded:` to match `"false"`: it matches only `"true"`. 3. Believing the variant sets the attribute: it only reads it; your markup or script must set it. 4. Keeping an `is-open` class in sync with `aria-expanded`: style from the attribute and delete the class.
- A save button has `data-state:ring-2`, and the ring shows even while the state is `idle`. Why?A bare name compiles to a presence check, `&[data-state]`, so it matches any value of `data-state`, including `idle`. To react to one value, use brackets: `data-[state=saving]:ring-2` compiles to `&[data-state="saving"]`.
- How do you rotate a chevron inside an accordion trigger when the trigger is expanded?Mark the trigger with `group` and give the chevron `group-aria-expanded:rotate-180`. The rule matches the chevron when an ancestor carrying `group` has `aria-expanded="true"`. Write the collapsed rotation unprefixed, since the variant never matches `"false"`.
- a bare data-state: variant matches only one particular value
- aria-expanded: also matches when the attribute is set to false
- the aria-* variants add the ARIA attribute to the element
- in v4 each data attribute must be configured before data-* can target it
- an is-open class should be toggled alongside aria-expanded for styling
Assumes Tailwind CSS v4 (checked at 4.3.3); bare data-* names such as data-saving: work without configuration since v4.