Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Responsive Design and State Variants

Group, Peer and Has Variants

Style one element from the state of another in Tailwind CSS: group-hover, peer-checked, named groups and the has-, in- and not- variants, with live demos.

Plain variants like hover: look at the element they are on. Often you want something different: when the card is hovered, change the color of the icon inside it. Or when a checkbox is checked, style the label next to it. CSS can do this with parent and sibling selectors, and Tailwind exposes them as group-*, peer-*, has-*, in-* and not-* variants.

All of them follow one idea: mark an element as the source of state (or let the browser find it by structure), and style another element from that state.

group: parent state, child style

Add the class group to the parent. Then any descendant can use group-hover:, group-focus:, group-active: and so on. The descendant's style applies when the parent is in that state.

A card whose parts react together

Hover the card: the icon tile, the title and the hidden link all react to the card hover state, not their own.

Named groups

When groups nest, group-hover: becomes ambiguous: which ancestor? Name the groups with a slash: group/card and group/item, and use group-hover/card: or group-hover/item:.

HTML
<ul class="group/list">
  <li class="group/item flex justify-between hover:bg-slate-50">
    <span>Row</span>
    <button class="invisible group-hover/item:visible">Edit</button>
  </li>
</ul>

peer: sibling state, sibling style

Mark an element with peer. A later sibling can then use peer-checked:, peer-focus:, peer-invalid:, peer-disabled: and the rest. The rule comes from the CSS subsequent-sibling combinator, so the peer must come before the element you style in the HTML.

A toggle switch with no JavaScript

The real checkbox is hidden with sr-only (visually hidden but still available to keyboards and screen readers). The pill after it is the visual. When the checkbox is checked, peer-checked: colors the pill and moves the knob using its ::after element. This pattern is how most Tailwind toggles, custom radios and accordion-like reveals are made.

Never hide an input with hidden or display: none when you use the peer pattern for accessibility: it removes the control from the keyboard order and from screen readers. Use sr-only.

A floating hint with peer-invalid

Show a message only when the field is invalid

Here peer-[...]: takes an arbitrary selector in square brackets, which lets you express a condition with no built-in variant. The message only appears once the user has typed something that is not a valid address.

has: style a parent from its children

has-* wraps the CSS :has() selector. Put it on the parent and test the children. has-checked:bg-indigo-50 applies when any descendant is checked; has-[img]:p-0 applies when the element contains an image.

Selectable cards with has-checked

Compared with peer, has- does not need the input to come before the styled element, and the whole card (not just a sibling) can change. :has() is supported in all current evergreen browsers.

in-, not- and nth- helpers

  • in-* is a lighter group: in-focus: applies when any ancestor has that state, with no group class needed.
  • not-* negates another variant: not-first:mt-2, not-hover:opacity-70, not-md:text-center (below md).
  • *: styles every direct child: *:rounded *:p-2 on a list container. **: styles all descendants.
HTML
<ul class="*:border-b *:py-2 last:*:border-0">...</ul>

Prefer group and peer when you want explicitness, has- when the state sits in a child, and *: for repeated children.

flowchart TD
  Q{"Where is the state?"} -->|"on an ancestor"| G["group-hover:, in-focus:"]
  Q -->|"on an earlier sibling"| P["peer-checked:"]
  Q -->|"in a descendant"| H["has-checked:, has-[img]:"]
  Q -->|"on the element itself"| S["hover:, focus:"]

Choose the variant by asking where the state lives.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which class must be on the parent for group-hover: to work on its children?

    group marks the element whose state its descendants can read with group-* variants.
  2. 2. For peer-checked: to style an element, where must the peer be?

    Peer variants use the subsequent-sibling selector, so the marked element must come before the styled one.
  3. 3. Which variant styles a parent according to what a child contains or its state?

    has- wraps :has(), so it looks downward from the element at its descendants.
  4. 4. How do you tell two nested groups apart?

    Named groups use a slash: group/card on the ancestor and group-hover/card: on the descendant.
  5. 5. Why use sr-only on a hidden checkbox instead of hidden?

    display: none removes the control from the accessibility tree; sr-only only hides it visually.

Summary

  • group plus group-hover: (and friends) styles descendants from the parent's state; name groups with group/name when they nest.
  • peer plus peer-checked: styles a later sibling, which powers custom toggles and radios.
  • has-checked: and has-[img]: style a parent from its children; in-* reads any ancestor; not-* negates a variant; *: styles direct children.
  • Hide real inputs with sr-only, not hidden.

Next: Container Queries and More Variants