Sub topic 10
Named groups for nested components
Hovering a card often needs to change several parts of it, and hovering a row inside that card needs to change other parts. A plain group can't keep those two apart. Named groups can. This chapter covers the group/name syntax, why unnamed groups cause trouble when nested, and how names work with peer.
This chapter assumes you know how group and group-hover: work, and that you can use hover:, focus:, and stacked variants. You need a project with Tailwind CSS v3.2 or later, or v4. Named groups don't exist in earlier v3 releases.
The problem with unnamed groups
A class like group-hover:text-indigo-600 is compiled to roughly this CSS:
.group:hover .group-hover\:text-indigo-600 {
color: #4f46e5;
}The selector uses a descendant combinator. It matches any element with the group class that is an ancestor of the styled element, at any distance. It does not look for the nearest one.
Suppose you nest two unnamed groups:
<div class="group rounded-lg border p-4">
<h3 class="group-hover:text-indigo-600">Project list</h3>
<ul>
<li class="group flex justify-between p-2">
<span>Item</span>
<span class="invisible group-hover:visible">Edit</span>
</li>
</ul>
</div>You probably want "Edit" to appear only when its own <li> is hovered. It will appear whenever the pointer is over the outer card, because the outer div is also a .group ancestor of the "Edit" span. Every Edit label in the list shows at once.
The reverse mistake also happens. You write group-hover: on the heading, expecting it to react to the card, but a group row elsewhere in the tree doesn't affect it, so the behavior depends on where each class sits. With more than one unnamed group in a component, it is hard to tell which one a group-* class refers to.
Naming a group
Add a slash and a name to the group class on the parent. Use the same name after the variant on the child:
<div class="group/card rounded-lg border p-4">
<h3 class="group-hover/card:text-indigo-600">Project list</h3>
<ul>
<li class="group/item flex justify-between p-2">
<span class="group-hover/item:text-indigo-600">Item</span>
<span class="invisible group-hover/item:visible">Edit</span>
</li>
</ul>
</div>The generated CSS now targets a specific class:
.group\/item:hover .group-hover\/item\:visible {
visibility: visible;
}"Edit" depends only on .group/item, so it appears only when its own row is hovered. The heading depends only on .group/card.
The pattern is group-{state}/{name}:{utility}. The state can be any state you could use with group-, such as hover, focus, focus-visible, active, or disabled.
A full example
The next example puts both levels in one component. Hovering the card underlines the title and shows a footer hint. Hovering a row highlights the row and reveals its button. Hovering the card also reveals a "Select all" action that no row controls.
<div class="group/card w-80 rounded-lg border border-slate-200 p-4">
<div class="flex items-center justify-between">
<h3 class="font-semibold text-slate-900 group-hover/card:underline">Team members</h3>
<button class="invisible text-sm text-indigo-600 group-hover/card:visible">
Select all
</button>
</div>
<ul class="mt-3 space-y-1">
<li class="group/item flex items-center justify-between rounded p-2 hover:bg-slate-50">
<span class="text-slate-700 group-hover/item:text-indigo-600">Ada Lovelace</span>
<button class="invisible rounded bg-indigo-600 px-2 py-1 text-xs text-white group-hover/item:visible">
Edit
</button>
</li>
<li class="group/item flex items-center justify-between rounded p-2 hover:bg-slate-50">
<span class="text-slate-700 group-hover/item:text-indigo-600">Grace Hopper</span>
<button class="invisible rounded bg-indigo-600 px-2 py-1 text-xs text-white group-hover/item:visible">
Edit
</button>
</li>
</ul>
</div>Both rows use the name item. That's fine, and it's intended. Each row is its own .group/item, and the selector applies from the row that is hovered. Hovering "Ada Lovelace" shows only her Edit button, because the descendant selector matches inside the hovered row and the other row isn't hovered.
A hovered row is always inside a hovered card, so the card-level styles stay active while you work on a row. This is the usual behavior: the pointer being over the row means it is also over the card.
How the two levels relate
Rules to remember
A named group and an unnamed group are separate and don't affect each other.
| Parent class | Child variant | Does it react? |
|---|---|---|
| `group` | `group-hover:` | Yes |
| `group/card` | `group-hover/card:` | Yes |
| `group/card` | `group-hover:` | No |
| `group` | `group-hover/card:` | No |
| `group/card` | `group-hover/item:` | No |
If the parent has a name, the child must use the same name. A mismatch produces no error. The utility is generated but never matches, so the style just doesn't apply.
An element can carry both an unnamed and a named group class if you need it to take part in both:
<div class="group group/card">
<p class="group-hover:text-slate-900 group-hover/card:underline">Both apply on hover</p>
</div>You rarely need this. Choosing one style per component is easier to read.
Choosing names
Names are arbitrary strings. Pick names that describe the component part, such as card, item, row, or sidebar, not the state they control. A name like hover is confusing next to group-hover/hover:.
Because Tailwind finds classes by scanning your source files for complete strings, write the name in full. This is the same rule as for all utilities. Don't build it from fragments:
<!-- Don't do this: the scanner can't see the full class -->
<div class="group/${name}">...</div>If a name is dynamic, map it to whole class strings in a lookup object, so every class appears literally in your source.
In a framework component, a named group also solves a reuse problem. If a Card component uses an unnamed group, any card you place inside another card's markup, or inside any other group, can react to the wrong ancestor. Naming the group after the component keeps its styles contained.
Names work with peer too
peer accepts names in the same way. Mark the sibling with peer/name and style the later element with peer-{state}/name:. This helps when a form has several inputs whose siblings might each react to a different one.
<div class="flex flex-col gap-1">
<input id="email" type="email" required class="peer/email rounded border px-3 py-2 invalid:border-red-500" placeholder="[email protected]" />
<input id="name" type="text" required class="peer/name rounded border px-3 py-2 invalid:border-red-500" placeholder="Your name" />
<p class="invisible text-sm text-red-600 peer-invalid/email:visible">Enter a valid email address.</p>
<p class="invisible text-sm text-red-600 peer-invalid/name:visible">Name is required.</p>
</div>Without names, peer-invalid: would match any earlier sibling marked peer, so both messages could react to whichever input was invalid. A peer must still come before the element that reacts to it, because CSS sibling selectors only look forward. The rule is the same as for unnamed peers.
Also, peer-invalid on an empty required field matches as soon as the page loads, because :invalid doesn't wait for user input. If you want to wait until the user has touched the field, add a placeholder and use placeholder-shown in a combined condition, or validate with JavaScript.
Common errors
- Every row reacts to the card. You used an unnamed
groupon both the card and the rows. Give each level its own name. - A named group does nothing. The child uses
group-hover:while the parent isgroup/card. Add the name to the child's variant. - The reacting element isn't inside the group.
group-*targets descendants. A sibling or parent of the group element can't react to it. Usepeerfor siblings, or move the markup. - The
peeris placed after its target. Reorder the elements so the peer comes first. - Names are built dynamically. Tailwind never generates the class. Write each name out as a full string.
Summary of the syntax
group/nameon the parent,group-{state}/name:on descendants.peer/nameon the earlier sibling,peer-{state}/name:on later siblings.- Names are separate from the unnamed
groupandpeer. - The same name can be used on many elements. Each instance controls its own descendants.
Exercise
In the markup below, hovering a single <li> makes the Delete button show in every row of the list. Why does this happen, and what is the smallest change that makes each Delete button show only for its own row?
<div class="group">
<ul>
<li class="group flex justify-between">
<span>Report.pdf</span>
<button class="invisible group-hover:visible">Delete</button>
</li>
</ul>
</div>
Show answer
Both the outer div and each li have the unnamed group class, so group-hover:visible matches whenever any .group ancestor is hovered. Hovering anywhere in the list hovers the outer div, so every Delete button shows. Give the li a name, for example group/item, and change the button to group-hover/item:visible. The outer div can keep group, since nothing inside it uses it any more, or it can be removed.
Exercise
A card has the class group/card. A heading inside it has group-hover:underline. The heading never underlines when you hover the card. What is wrong, and how do you fix it?
Show answer
The heading's variant has no name, so it only responds to a parent with the unnamed group class. The parent is group/card, which an unnamed group-hover: doesn't match. Change the heading to group-hover/card:underline.