Sub topic 10
Named groups for nested components
This chapter assumes you know Tailwind utility classes, the hover: and focus: variants, and the basic group and group-hover: pattern. You also need a project where Tailwind CSS is already set up.
The problem with nested groups
The group class marks a parent so that its descendants can react to the parent's state. A plain group-hover: variant matches the nearest... in fact it matches any ancestor with the group class. When two group elements are nested, a child cannot say which one it means.
Consider a card that contains a list. You want the whole card to react when hovered, and each list row to react when only that row is hovered. If both the card and the rows use plain group, then hovering a row also counts as hovering the card. The card-level effect fires whenever the pointer is over any row, and the two states cannot be told apart.
Naming a group
Add a name after a slash: group/card, group/item. Then use the same name in the variant: group-hover/card: or group-hover/item:. A named variant only responds to the ancestor with the matching name.
<div class="group/card rounded-lg border p-4">
<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 group-hover/card:underline">Edit</span>
</li>
</ul>
</div>In this markup:
- The "Item" label turns indigo only while its own row is hovered.
- The "Edit" label appears only while its own row is hovered.
- The "Edit" label is underlined whenever the pointer is anywhere over the card.
The names are labels you choose. Any short word works, as long as the class on the parent and the variant on the child use the same one.
How the matching works
A named group does not change what the browser does. Tailwind generates a selector that targets the named ancestor, so group-hover/item: only matches when the element is inside an ancestor that has group/item and that ancestor is hovered. An ancestor with a different name, or with no name, is ignored by that variant.
Other states work with names too
Names combine with every group-* state, not only hover. For example, group-focus-within/card: reacts when any element inside the card has focus, and group-focus-visible/item: reacts to keyboard focus on the group element itself.
<div class="group/card rounded-lg border p-4 focus-within:ring-2">
<button class="group/item flex items-center gap-2 rounded p-2 hover:bg-slate-50">
<span class="group-focus-visible/item:text-indigo-600">Rename</span>
<span class="hidden group-focus-within/card:inline text-sm text-slate-500">Card active</span>
</button>
</div>Common errors
- Name mismatch.
group/cardon the parent andgroup-hover/Card:on the child do not match, because names are case-sensitive. The style silently does nothing. - Missing group class. A variant such as
group-hover/item:has no effect unless an ancestor actually hasgroup/item. - Mixing named and unnamed. A plain
groupon the parent does not satisfygroup-hover/card:. Use the named class on the parent when the child uses a named variant. - Putting the variant on the group itself. The group-* variants style descendants. To style the group element, use a regular
hover:variant on it.
Exercise
A card has the class group/card. Inside it, a row has the class group/row. A delete icon inside the row should be visible only while the pointer is over that row, and a title in the card header should turn blue while the pointer is anywhere over the card. Which variants do you put on the icon and the title?
Show answer
Put invisible group-hover/row:visible on the icon and group-hover/card:text-blue-600 on the title. Each variant names the group whose hover state it should follow.