Sub topic 6
Coordinating elements with group and peer
This chapter assumes you know basic state variants such as hover: and focus:. It also assumes you can use transition-*, duration-* and the transform utilities from the earlier chapters.
A state variant normally styles the element that is hovered or focused. Often you want a different element to react instead. Hovering a card might move an arrow inside it, or checking a box might restyle the label next to it. Tailwind covers both cases with group and peer.
How group works
Add the group class to a parent. Children can then use group-hover:, group-focus:, group-active: and other group-* variants. These respond to the state of the parent, not the child.
<a href="#" class="group inline-flex items-center gap-2 text-indigo-600">
<span>Read more</span>
<span class="transition-transform duration-200 group-hover:translate-x-1">→</span>
</a>The arrow is not hovered itself. Hovering anywhere on the link sets the arrow's translate-x-1, and transition-transform animates the move. Put the transition-* classes on the element that changes, which is the arrow here.
Named groups for nesting
A plain group is ambiguous when groups are nested. A child reacts to its nearest matching ancestor, or to any ancestor with group, which may not be the one you mean. Name each group with a slash suffix and use the same name in the variant.
<div class="group/card rounded-lg bg-white p-6 shadow transition duration-200 hover:shadow-lg">
<h3 class="font-semibold">Project</h3>
<ul>
<li class="group/item flex items-center justify-between py-1">
<span>Task one</span>
<button class="opacity-0 transition-opacity duration-150 group-hover/item:opacity-100">
Edit
</button>
</li>
</ul>
<p class="text-slate-500 transition-colors group-hover/card:text-slate-900">
This text darkens when the whole card is hovered.
</p>
</div>Each Edit button appears only when its own row is hovered. The paragraph follows the card.
How peer works
Add peer to an element. Later siblings can then use peer-hover:, peer-focus:, peer-checked:, peer-invalid: and similar variants. This suits form controls, whose state is exposed through CSS pseudo-classes.
<label class="flex items-center gap-3">
<input type="checkbox" class="peer sr-only" />
<span
class="relative h-6 w-11 rounded-full bg-slate-300 transition-colors duration-200
after:absolute after:left-0.5 after:top-0.5 after:size-5 after:rounded-full after:bg-white
after:transition-transform after:duration-200
peer-checked:bg-indigo-600 peer-checked:after:translate-x-5
peer-focus-visible:ring-2 peer-focus-visible:ring-indigo-400 peer-focus-visible:ring-offset-2"
></span>
<span>Enable notifications</span>
</label>The hidden checkbox holds the state. When it is checked, the track changes color and the knob (the after: pseudo-element) slides 1.25rem to the right. peer-focus-visible draws a focus ring for keyboard users, even though the input itself is invisible.
Common errors
- The peer comes after the target.
peerrelies on the CSS subsequent-sibling combinator, so it only affects elements that appear later in the HTML. Move thepeerelement earlier, or restructure the markup. - The target is not a sibling. A peer cannot affect a nested descendant of a sibling. Style the sibling itself, or use a pseudo-element as above.
- The transition is on the wrong element.
group-hover:opacity-100changes the value, but the child still needs its owntransition-opacity. - Unnamed nested groups. Use
group/nameonce groups nest. - Touch devices. Tailwind v4 applies
hover:variants only when the device supports hover. Do not hide essential controls behindgroup-hover:.
Exercise
A search icon should turn blue while the input before it is focused. You wrote <svg class="peer-focus:text-blue-600 ..."> followed by <input class="peer ...">, and nothing happens. What is wrong and how do you fix it?
Show answer
A peer only affects siblings that come after it in the HTML, and here the input comes after the icon. Place the input first, with the peer class, then the svg. Then position the svg visually, for example with absolute positioning. Also add transition-colors to the svg so the color change animates.