The hover: prefix works on one element only: the cursor is over that element, and that same element changes. Sometimes you want something else. The cursor is over a card, but the title inside that card changes colour. Or a checkbox gets ticked, but the text next to it changes. For that Tailwind has group and peer. You mark one element as the trigger, and other elements react to that trigger's state: hovered, ticked, and so on.
Think of a marching drill. When the squad leader gives a command, every member of the squad reacts. That is how group works: the leader is the parent with the group class, the command is the cursor entering that parent, and the members are the children inside that parent. Each marcher also copies the person in front: when the one in front crouches, the one behind crouches too. But nobody can see behind them. That is how peer works. The person in front is the element with the peer class, for example a checkbox, and crouching is its state, for example ticked. The people behind are its siblings (elements with the same parent) written after it in the HTML.
group: the parent triggers, the children react
Mark the parent with the group class. Any element inside it (child, grandchild, and so on) can use group-hover:, and then reacts when that parent is hovered.
<div id="card" class="group p-4 rounded-lg border border-gray-200 cursor-pointer">
<h3 id="heading" class="font-semibold group-hover:text-blue-600">Learning Tailwind</h3>
<p id="info" class="text-sm text-gray-400 group-hover:text-gray-600">23 lessons, from utilities to a project</p>
</div>
groupproduces no style at all. It's only a marker: "I'm the trigger".p-4= 16px padding,rounded-lg= 8px border-radius,border= a 1px line,cursor-pointer= the hand cursor, a sign the card can be clicked.font-semibold= font-weight 600.group-hover:text-blue-600makes the title blue, but only while the cursor is over the.group.text-gray-400 group-hover:text-gray-600: the description is light grey, then dark grey while the card is hovered.
| State | Title #heading |
Description #info |
|---|---|---|
| Cursor outside the card | black | light grey (gray-400) |
| Cursor anywhere over the card | blue (blue-600) | dark grey (gray-600) |
Cursor over the card, but group removed |
black | light grey |
One hover on the parent, two children change at once. The cursor doesn't need to be right over the title: anywhere inside the card is enough. Without group, group-hover: has no trigger, so nothing changes.
peer: the sibling in front triggers the ones behind
Mark the trigger element with the peer class. Its siblings written after it in the HTML can use peer-checked: (while the trigger is ticked), peer-focus: (while the trigger has focus), and so on. In this example, the trigger is a checkbox:
<div class="flex flex-wrap items-center gap-2">
<input id="agree" type="checkbox" class="peer">
<label for="agree" class="text-gray-500 peer-checked:font-semibold peer-checked:text-green-700">I agree to the class rules</label>
<p class="hidden w-full text-sm text-green-700 peer-checked:block">Ready, your sign-up can be sent.</p>
</div>
peeron the checkbox: this checkbox is the trigger.peer-checked:text-green-700on the label: the label is green while the checkbox is ticked, andpeer-checked:font-semiboldmakes it bold.hidden peer-checked:blockon the paragraph: the message is hidden (display none), then shown (display block) once the box is ticked. The message appears without a single line of JavaScript.
| Order in the HTML | Ticked? | Label | Message |
|---|---|---|---|
| input, label, p | no | grey | hidden |
| input, label, p | yes | bold green | shown |
| label, input, p | yes | still grey | shown |
Look at the last row: the label doesn't turn green. peer only reaches siblings written after it, like a marcher who can only see forward. The label written before the input isn't affected, but the message written after the input still appears. Want the label shown to the left of the checkbox? Still write the input first in the HTML, then use CSS to change where the label shows, for example order-first on the label (order-first moves a flex item to the front).
The difference in one sentence: group is for a parent and its children (from the parent to what is inside it), peer is for siblings (only towards the siblings written after it).
Other states and has-*
Besides hover:, other state prefixes can also go after group- or peer-:
group-hover:,group-focus:,group-focus-within:(some element inside the parent has focus).peer-checked:,peer-focus:,peer-invalid:(for example an error messageinvisible peer-invalid:visibleunder an email input whose value isn't valid).
For error messages, peer-invalid: has a problem. An input with the required attribute (it must be filled in) already counts as :invalid (not valid) when the page opens, because it is still empty. So the message shows before the user types anything. Use peer-user-invalid: instead. With this prefix, the message shows only after the user fills in the input and then leaves it:
<input type="email" required class="peer border p-2">
<p class="invisible peer-user-invalid:visible text-sm text-red-600">The email format isn't right yet.</p>
| Moment | peer-invalid: |
peer-user-invalid: |
|---|---|---|
| the page has just opened | message shows | not yet |
| while typing "bu" | message shows | not yet |
| after leaving the input | message shows | message shows |
peer can't change a parent. If the thing to change is the parent itself, use has-*: on that parent. For example has-checked:: a class with this prefix applies when the element that uses it contains an element that is ticked. <label class="border has-checked:border-blue-600"> makes the label's border blue when a checkbox or radio inside it is ticked.
📌 Nested groups: give them names
group-hover: reacts to any .group above it: its parent, its parent's parent, and so on. So with a group inside a group, hovering the outer group also triggers the children of the inner group. Give each group a name with a slash, so it's clear which group you mean:
<ul class="group/list">
<li class="group/item">
<span class="group-hover/item:text-blue-600">Flexbox</span>
</li>
</ul>
group/item gives the group on the li the name item. group-hover/item: only reacts when that li named item is hovered, not when the list (group/list) is hovered.
📌 Behind the scenes: the CSS
Tailwind v4 turns group-hover:text-blue-600 and peer-checked:text-green-700 into this CSS:
@media (hover: hover) {
.group-hover\:text-blue-600:is(:where(.group):hover *) { color: var(--color-blue-600); }
}
.peer-checked\:text-green-700:is(:where(.peer):checked ~ *) { color: var(--color-green-700); }
:where(.group):hover *means "this element sits inside a.groupthat is being hovered".~means "any sibling after.peer". CSS has no selector for a sibling written earlier, which is why the HTML order matters.@media (hover: hover): since v4, every prefix that uses hover,group-hover:included, only applies on devices that can hover, such as a laptop with a mouse. On a phone, a tap doesn't triggergroup-hover:.
⚠️ When the trigger never reaches its target
- Forgetting
groupon the parent.group-hover:on the children reacts to nothing. There's no error, and nothing on the page changes. peerwritten after its target. A label written before the checkbox never changes. Write thepeerelement first.- The target isn't a sibling.
peeronly reaches elements with the same parent. If the input is wrapped in its owndiv, a label outside thatdivisn't affected. - Important info that only shows on hover. Phones have no hover, so a button or text that only appears through
group-hover:is never seen.
TL;DR: group on the parent + group-hover: (or group-focus:) on a child: the child reacts when its parent is hovered. peer on one element + peer-checked: (or peer-focus:, peer-invalid:) on a sibling (an element with the same parent) written after it: that sibling reacts to the state of the peer element, for example ticked. So group is for parent and child, peer is for siblings, and the peer element has to come first. A group inside a group? Name them: group/item + group-hover/item:. Want to change a parent from its child's state? Use has-checked: on the parent: it applies when something inside is ticked. The exercise card: group p-4 rounded-lg border cursor-pointer (16px padding, 8px radius, hand cursor), its title font-semibold group-hover:text-blue-600 (weight 600).
⚡ Hover the parent, the child changes
Write:
<div id="card" class="group p-4 rounded-lg border border-gray-200 cursor-pointer">
<h3 id="heading" class="font-semibold group-hover:text-blue-600">Learning Tailwind</h3>
<p id="info" class="text-sm text-gray-400 group-hover:text-gray-600">23 lessons, from utilities to a project</p>
</div>
Guess first: group-hover: on the title waits for a hover on a parent that has the group class. Remove the group class from the #card card. Does the title still turn blue when the card is hovered?
No. group-hover: has no trigger, so nothing changes, and there's no error.
Meaning: group on the parent (here, the card) only marks the trigger. group-hover:text-blue-600 turns the title, a child of the card, blue while the cursor is anywhere over the card.
⚡ peer: the sibling in front triggers
Write:
<div class="flex flex-wrap items-center gap-2">
<input id="agree" type="checkbox" class="peer">
<label for="agree" class="text-gray-500 peer-checked:font-semibold peer-checked:text-green-700">I agree to the class rules</label>
<p class="hidden w-full text-sm text-green-700 peer-checked:block">Ready, your sign-up can be sent.</p>
</div>
Meaning: peer on the checkbox makes that checkbox the trigger. The label and the message are its siblings (elements with the same parent), and both are written after the checkbox. peer-checked: on those siblings reacts when the box is ticked: the label turns green, and the hidden message becomes block, with no JavaScript.
⚡ The peer comes first
Guess first: a label with peer-checked:text-green-700 is written before the peer checkbox. Does the label turn green when the box is ticked?
No. peer only reaches siblings written after it.
Meaning: group is for parent and child, peer is for siblings, and the peer element is always written before the siblings that react to its state.