In Tailwind CSS, how do `group` and `peer` let you style an element from a parent's hover state or a sibling checkbox's checked state?
answer
- mark the element you depend on
- group goes on an ancestor
- peer goes on an earlier sibling
- the ~ combinator only looks forward
- name markers when they nest
basics
~20 sPut the group class on an ancestor and use group-hover: on its descendants; put the peer class on an earlier sibling and use peer-checked: on elements after it. Name them, as group/card or peer/notify, when markers nest or repeat.
solid answer
~40 sA utility only sees the element it sits on, so Tailwind uses **marker classes**. Add `group` to an ancestor, such as a recipe card, and a descendant can use `group-hover:underline`; the rule matches the descendant of a hovered `.group`. Add `peer` to a sibling, such as a checkbox, and a later sibling can use `peer-checked:block`; the rule uses the CSS subsequent-sibling combinator `~`, so the peer must come **before** the styled element and share its parent. Both work with every pseudo-class variant (`group-focus`, `peer-invalid`, `peer-disabled`). When groups nest, an unnamed `group-hover:` reacts to *any* hovered `.group` ancestor, so you name them: `group/card` with `group-hover/card:`, and likewise `peer/notify` with `peer-checked/notify:`. Names need no configuration. To restyle the parent itself from a descendant's state, use `has-*` instead.
code
html · 12 lines<article class="group/card rounded-lg border p-4">
<h3 class="font-semibold group-hover/card:underline">Lemon risotto</h3>
<button class="group/save rounded p-1">
<svg class="size-5 text-gray-400 group-hover/save:text-rose-600"></svg>
</button>
</article>
<form class="flex flex-col gap-2">
<input id="notify" type="checkbox" class="peer/notify" />
<label for="notify">Email me when someone replies</label>
<p class="hidden peer-checked/notify:block">At most one email a day.</p>
</form>go deeper
Recall the two markers: group on the ancestor, peer on an earlier sibling, then group-hover: or peer-checked: on the element you want to change.
Explain the selectors behind them: a descendant of a hovered .group, and a later sibling through the ~ combinator, and why that makes markup order matter for peer.
Diagnose the silent failures: a peer placed after its target, nested unnamed groups leaking hover, a wrapper breaking the sibling relationship, and pick has-* or named markers to fix each.
Weigh state-driven styling in markup against component state in JavaScript for a design system, and set conventions such as always naming groups inside reusable components.
## Why a utility cannot see another element's state A Tailwind utility is a class on one element, and a variant such as `hover:` adds a condition about **that same element**. Interfaces often need something else: a recipe card's title underlines when *the card* is hovered, or a comment form's helper text appears when *a checkbox next to it* is checked. CSS can express both with combinators, and Tailwind exposes them through two **marker classes**, `group` and `peer`. The marker itself produces no styles; it gives the generated selector something to point at. ## `group`: styling from an ancestor's state Put `group` on the ancestor and `group-*` variants on any descendant: ```html <article class="group rounded-lg border p-4"> <h3 class="font-semibold group-hover:underline">Lemon risotto</h3> <p class="text-gray-600 group-hover:text-gray-900">35 minutes</p> </article> ``` `group-hover:underline` matches the `h3` when it is a **descendant of a hovered element carrying `group`**. It works with every pseudo-class variant: `group-focus`, `group-active`, `group-odd`, and attribute variants such as `group-aria-expanded`. ## `peer`: styling from a preceding sibling's state Put `peer` on the sibling whose state matters and `peer-*` variants on a sibling that comes after it: ```html <form class="flex flex-col gap-2"> <input id="notify" type="checkbox" class="peer" /> <label for="notify">Email me when someone replies</label> <p class="hidden peer-checked:block">At most one email a day.</p> </form> ``` The generated rule uses the **subsequent-sibling combinator** `~`, which only matches elements that come *later* under the same parent. That is the rule people miss: - the peer must appear **earlier in the markup** than the element you style; - the two must be **siblings**, children of the same parent, not cousins in different wrappers; - they do not have to be adjacent; any later sibling can react. If the design puts the helper text above the checkbox, keep the DOM order and move things visually with flex or grid ordering, or switch approach (see `has-*` below). ## What the build generates Tailwind turns each of these classes into an ordinary CSS rule at build time; nothing runs in the browser. Conceptually: - `group-hover:underline` becomes a rule that matches the element when it is inside a hovered element carrying the `group` class, through a **descendant combinator**; - `peer-checked:block` becomes a rule that matches the element when an earlier sibling carrying `peer` is checked, through the **subsequent-sibling combinator** `~`; - a name such as `/card` is part of the marker's class, so `group-hover/card:` points at the element whose class is `group/card` and at nothing else. Because the marker is part of the selector, the browser keeps the style in step with the state by itself: no event listeners and no re-render. The exact selector text is an implementation detail; the relationship, ancestor or earlier sibling, is the part to remember. ## Named groups and peers An unnamed `group-hover:` matches when **any** ancestor with `group` is hovered, not just the nearest one. Nest a save button that is itself a `group` inside a card that is a `group`, and hovering the card triggers the button's icon too. The fix is a name after a slash: | Marker | Variant | Reacts to | |---|---|---| | `group/card` | `group-hover/card:` | hover on the element named `card` | | `group/save` | `group-hover/save:` | hover on the save button only | | `peer/notify` | `peer-checked/notify:` | the checkbox named `notify` | | `peer/terms` | `peer-checked/terms:` | a second checkbox in the same form | Names are written straight into the markup; nothing has to be registered. For a one-off condition, an arbitrary form such as `group-[.is-published]:block` takes your own selector in brackets. ## Styling the parent itself: `has-*` `group` and `peer` style *other* elements. When the element to restyle is the **parent** of the thing whose state changes, use `has-*`: `has-checked:bg-amber-50` on the form wrapper applies while any descendant is checked. It also helps when markup order rules out `peer`: put `group` on the shared parent and `group-has-checked:block` on the helper text, wherever it sits. Exactly what the native `:has()` selector matches is a CSS question; the variant simply wraps it. ## Pitfalls to recognise 1. The helper text placed before the checkbox never reacts: `~` only looks forward. 2. Inner elements react to an outer card's hover: an unnamed group matches any ancestor. 3. The peer is wrapped in its own `div`: it is no longer a sibling of the target. 4. Expecting JavaScript to be needed: these are pure CSS selectors, generated at build time. 5. Registering group names in configuration: unnecessary, the name is read from the class.
- The helper text sits before the checkbox in the markup, and `peer-checked:block` never fires. Why, and what are the fixes?`peer-*` compiles to a rule using the subsequent-sibling combinator `~`, which only matches elements after the peer. Either move the checkbox earlier in the DOM and reorder visually with flex or grid, or mark the shared parent with `group` and give the text `group-has-checked:block`, which does not depend on sibling order.
- Hovering a recipe card also recolours the icon of a save button inside it that has its own `group`. Why?An unnamed `group-hover:` matches when any ancestor carrying `group` is hovered, not only the nearest. The icon has two such ancestors, the card and the button. Name them, `group/card` and `group/save`, and give the icon `group-hover/save:` so it watches the button alone.
- When would you choose `has-*` over `peer-*` for a checkbox-driven style?When the element to style is the checkbox's parent, or sits before the checkbox. `has-checked:` on the wrapper restyles the wrapper itself while a descendant is checked, and `group-has-checked:` lets any descendant of a marked parent react, regardless of order. `peer-*` only reaches later siblings.
- peer works on any sibling, whether it comes before or after the peer
- group-hover reacts only to the nearest ancestor that has the group class
- group and peer names must be registered in configuration before use
- group-hover needs JavaScript to track when the parent is hovered
- a peer can sit inside its own wrapper div and still drive a sibling's style