Sub topic 8
Styling children from a parent with group
This chapter assumes you know how variant prefixes such as hover: work and that you can write basic Tailwind utilities. A variant makes a utility apply only in a given state. Here you will extend that idea so a parent's state can change its children.
The problem group solves
A hover: variant reacts to the element that carries it. If you hover a card, only the card changes. Often you want the whole card to hover, while a heading or an icon inside it changes. The child is not the element being hovered, so hover: on the child does not help.
Tailwind solves this with two pieces:
- Add the
groupclass to the parent. This marks it as a group. - Add a
group-hover:variant to any descendant. The utility applies when the group is hovered.
Under the hood, Tailwind generates a selector that combines the parent's state with the child. For group-hover:text-indigo-600, the rule has the form .group:hover .group-hover\:text-indigo-600.
A worked example
This link is a card. Hovering anywhere on it changes the heading color and nudges the arrow to the right.
<a href="#" class="group block rounded-lg border border-slate-200 p-4 hover:bg-slate-50">
<h3 class="font-semibold text-slate-900 group-hover:text-indigo-600">Read the guide</h3>
<span class="inline-block transition-transform group-hover:translate-x-1">→</span>
</a>Read it piece by piece:
groupon the<a>makes it the group parent.hover:bg-slate-50on the same element styles the parent itself. Plainhover:still works on the group element.group-hover:text-indigo-600on the<h3>applies when the pointer is over the<a>, even if the pointer is not directly over the heading.transition-transformon the arrow animates the move. Without it, the arrow would jump.inline-blockmatters on the arrow. Transforms do not apply to plain inline elements such as a default<span>.
Where the group-hover utility can go
The group-hover: variant works on any descendant of the group element, however deeply nested. It does not work on the group element itself, because the selector requires a descendant. Use plain hover: there.
The pattern is useful for:
- Revealing hidden controls, such as an edit button in a list row.
- Changing the color of an icon, label, or text when its container is hovered.
- Moving or scaling a decorative element inside a clickable card.
Here is a row that reveals a button only on hover:
<li class="group flex items-center justify-between p-2">
<span>Quarterly report</span>
<button class="invisible rounded bg-slate-200 px-2 py-1 group-hover:visible">Edit</button>
</li>invisible hides the button but keeps its space, so the row does not change height when the button appears.
Common errors
- Forgetting
groupon the parent. Without it,group-hover:never matches and nothing happens. This is the most frequent cause of "my group-hover does nothing." - Putting
grouptoo far up. If a large container is the group, hovering anywhere in it triggers everygroup-hover:inside it. Putgroupon the smallest element that should act as the trigger. - Using
group-hover:on the group itself. Usehover:for the parent's own styling. - Nesting groups without names. An unnamed
group-hover:responds to the nearest matching ancestor group in the selector, which can be ambiguous when components nest. A later chapter covers named groups for this case.
Exercise
A card is <div class="rounded border p-4"> and contains <p class="text-slate-600 group-hover:text-slate-900">. Hovering the card does not darken the text. What single change fixes it, and why?
Show answer
Add the group class to the card div, giving <div class="group rounded border p-4">. The group-hover: variant only matches when an ancestor has group and is hovered. Without that class, the generated selector .group:hover ... never matches.