Sub topic 6
Coordinating elements with group and peer
This chapter assumes you know basic HTML and CSS, can read Tailwind utility classes, and have seen how a state variant such as hover: triggers a transition on a single element. Here you will learn how to make one element's state drive the style of a different element.
The problem: state lives on one element
A variant like hover:bg-indigo-700 applies only when the element carrying the class is hovered. That is often not enough. Consider a link with an arrow inside it. You want the arrow to slide when the user hovers anywhere on the link, not only when the pointer is directly over the arrow.
Tailwind solves this with two features:
groupandgroup-*variants, which let a child react to the state of an ancestor.peerandpeer-*variants, which let an element react to the state of an earlier sibling.
Both are only selectors under the hood. They do not run JavaScript. Transitions and animations still work the way they did in earlier chapters: the target element needs a transition utility, and the variant supplies the end state.
Group: a parent drives its children
Mark the parent with the group class. On any descendant, prefix a utility with group-hover:, group-focus:, group-active: and so on. The utility applies when the parent is in that state.
<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 group class has no visual effect. It is a marker. The arrow span carries the transition (transition-transform duration-200) and the group variant (group-hover:translate-x-1). Hovering the link moves the arrow 0.25rem to the right over 200 milliseconds. Moving the pointer away reverses the transition.
Put the transition utility on the element that changes, not on the group. The group element only supplies the state.
Several children, one trigger
A single group can drive many children, each with its own transition settings. This is how you build cards where the image zooms, the title changes color, and an icon fades in, all from one hover.
<a href="#" class="group block w-72 overflow-hidden rounded-lg bg-white shadow transition-shadow duration-300 hover:shadow-lg">
<div class="overflow-hidden">
<img src="photo.jpg" alt="Mountain lake at sunrise"
class="h-40 w-full object-cover transition-transform duration-500 ease-out group-hover:scale-105">
</div>
<div class="p-4">
<h3 class="font-semibold text-slate-900 transition-colors duration-200 group-hover:text-indigo-600">
Mountain lake
</h3>
<p class="mt-1 text-sm text-slate-600 opacity-80 transition-opacity duration-200 group-hover:opacity-100">
A short trip, a long view.
</p>
</div>
</a>Notice three details:
- The image wrapper has
overflow-hidden, so the scaled image is clipped instead of spilling outside the card. - Each child chooses its own duration. The image takes 500 ms, the text 200 ms. The different timings make the effect feel layered.
- The card itself uses plain
hover:shadow-lg, because the card is the element being hovered.
Group focus and keyboard users
Hover does not exist for keyboard users. If a link or button reveals something on hover, also reveal it on focus. For a group, use group-focus-visible: or group-focus-within:.
group-focus-visible:applies when the group element itself shows a focus ring (for example, when tabbed to).group-focus-within:applies when the group element or any descendant has focus. Use it when the group is a wrapper and the focusable element is inside.
<div class="group relative">
<button class="rounded bg-slate-100 px-3 py-2">Options</button>
<div class="absolute left-0 top-full mt-1 rounded bg-white p-3 opacity-0 shadow transition-opacity duration-200 group-focus-within:opacity-100 group-hover:opacity-100">
Rename, duplicate, delete
</div>
</div>One caution applies here. An element with opacity-0 is invisible but still present, so it stays in the tab order and can be clicked. For real menus, combine opacity with invisible or pointer-events-none, or use a pattern such as the one in the enter transitions chapter. For a short tooltip with no interactive content, opacity alone is fine.
Named groups: nesting without confusion
A plain group-hover: responds to the nearest ancestor marked group, but in fact it matches any ancestor with that class. When groups are nested, an inner element reacts to the hover of the outer group as well as the inner one. To target a specific group, give each a name with a slash suffix.
<div class="group/card rounded-lg border p-4">
<h3 class="transition-colors group-hover/card:text-indigo-600">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">
Remove
</button>
</li>
</ul>
</div>The heading changes color when the whole card is hovered. The Remove button appears only when its own row is hovered, not when the pointer is anywhere in the card. The name after the slash is arbitrary, but it must match on both sides: group/item and group-hover/item.
Peer: a sibling drives a later sibling
A group reaches down to descendants. A peer reaches sideways. Mark an element with peer, and a following sibling can use peer-hover:, peer-focus:, peer-checked: and similar variants.
The key limit: the peer must come before the element that reacts to it. CSS has no selector that looks backward to an earlier sibling in this way, and Tailwind's peer variants use the general sibling combinator (~). If the marked element comes later in the HTML, the variant never matches.
Example: floating label
A floating label is a classic peer pattern. The input is the peer, and the label after it reacts to the input's state.
<div class="relative w-64">
<input id="email" type="email" placeholder=" "
class="peer w-full rounded border border-slate-300 px-3 pb-2 pt-5 outline-none transition-colors duration-200 focus:border-indigo-600">
<label for="email"
class="pointer-events-none absolute left-3 top-4 origin-left text-slate-500 transition-all duration-200 ease-out peer-focus:-translate-y-3 peer-focus:scale-75 peer-focus:text-indigo-600 peer-[:not(:placeholder-shown)]:-translate-y-3 peer-[:not(:placeholder-shown)]:scale-75">
Email
</label>
</div>The placeholder=" " (a single space) is deliberate. It makes :placeholder-shown true while the field is empty. The arbitrary variant peer-[:not(:placeholder-shown)]: then keeps the label floated when the field contains text, so the label does not fall back onto the user's input after blur.
The label uses transition-all here because it changes transform and color together. For a production component you could narrow it to transition-[transform,color]. That choice is covered in the performance chapter.
Example: peer-checked
Form state is a good fit for peers because inputs expose state through pseudo-classes such as :checked and :disabled.
<label class="flex cursor-pointer 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 peer-checked:bg-indigo-600 peer-focus-visible:ring-2 peer-focus-visible:ring-indigo-400 peer-focus-visible:ring-offset-2
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 after:content-[''] peer-checked:after:translate-x-5"></span>
<span>Notifications</span>
</label>This is a switch built from a real checkbox. The checkbox is visually hidden with sr-only but still focusable and accessible to screen readers. The track span is the next sibling, so peer-checked:bg-indigo-600 colors it. The knob is the track's ::after pseudo-element, and peer-checked:after:translate-x-5 slides it. Because the real checkbox holds the state, clicking the label, pressing Space, and form submission all work without script.
Group versus peer
| Feature | `group` | `peer` |
|---|---|---|
| Marker class | `group` on an ancestor | `peer` on an earlier sibling |
| Who reacts | Any descendant | Later siblings only |
| Underlying selector | Descendant combinator | General sibling combinator (`~`) |
| Typical use | Cards, links with icons, row actions | Form controls and their labels or messages |
| Naming | `group/name`, `group-hover/name` | `peer/name`, `peer-hover/name` |
Peers can also be named, which matters when a form has several inputs whose siblings should not mix up.
<div>
<input type="text" class="peer/name rounded border px-2 py-1" placeholder="Name">
<input type="text" class="peer/email rounded border px-2 py-1" placeholder="Email">
<p class="text-sm text-slate-400 transition-colors peer-focus/name:text-indigo-600">
Your full name
</p>
</div>Without the names, peer-focus: would respond to focus on either input.
The coordination model
The mental model has two roles. The marked element only supplies state. The target element holds both the transition utilities and the variant utilities. If an animation does not play, check that the target, not the marker, carries the transition-* class.
Common errors
- The peer is after the target. Move the
peerelement earlier in the HTML. If the visual order must differ, reorder with CSS (flex-row-reverse,order-*, or absolute positioning) while keeping the DOM order correct. - Another element sits between them. The
~combinator tolerates elements between siblings, so this usually works. It fails only when the peer and target have different parents. - No transition on the target. The style jumps instantly because only the variant utility is present. Add
transitionand a duration to the target. - Nested groups trigger each other. Use named groups (
group/card,group-hover/card). - Hover-only reveals. Add
group-focus-within:orgroup-focus-visible:so keyboard users get the same result. - Hover on touch devices. In Tailwind v4,
hover:andgroup-hover:apply only on devices that support hover. In v3, tapping can leave an element stuck in the hover state on touch screens. Do not hide essential content behind hover alone. - Hidden controls remain focusable. Elements at
opacity-0still receive focus and clicks unless you also useinvisibleorpointer-events-none.
Exercises
Exercise
A card has the class group. Inside it is an icon with the classes group-hover:rotate-90. The icon turns instantly instead of smoothly. What is missing, and where should it go?
Show answer
The icon has no transition. Add transition-transform duration-200 (or a similar transition and duration-* pair) to the icon element itself, because the target element holds the transition. Putting it on the card would not animate the icon.
Exercise
You write <span class="peer-checked:text-green-600">Done</span> followed by <input type="checkbox" class="peer">. Checking the box does not change the text. Why, and how do you fix it?
Show answer
The peer must come before the element that reacts to it. Here the span comes first, so the sibling selector cannot match. Place the checkbox with the peer class before the span in the HTML.
Exercise
A list of rows each has a Delete button that should fade in only when its own row is hovered, inside a card that also uses group for a hover shadow. Which classes make the button react only to its row?
Show answer
Name the groups. Use group/row on the row, and opacity-0 transition-opacity group-hover/row:opacity-100 on the button. The card can keep group/card or plain hover: utilities, so the two states do not interfere.