Sub topic 8
Styling children from a parent with group
This chapter assumes you know basic HTML and CSS, including pseudo-classes such as :hover, and that you already use Tailwind utilities like colors, padding, and rounded corners in a project where Tailwind is set up. You should also know the hover: prefix from the earlier chapters.
The problem group solves
The hover: variant styles an element when the pointer is over that element. Often you want a different effect: hover over a card, and change something inside the card. The heading should change color, or an arrow should slide. The heading itself is not hovered in that case. Its parent is.
Plain CSS handles this with a descendant selector such as .card:hover h3 { ... }. Tailwind has no stylesheet where you write that selector, so it provides a pair of classes instead:
groupmarks the parent element as the one whose state matters.group-hover:(and othergroup-*:variants) goes on a descendant and applies when the marked parent is in that state.
A first example
<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>The link carries group. It also has its own hover:bg-slate-50, which is an ordinary variant on the element itself. The heading and the arrow are children, and each has a group-hover: class.
When the pointer is anywhere over the link, three things happen together:
- The link background becomes
slate-50, fromhover:. - The heading turns indigo, from
group-hover:text-indigo-600. - The arrow moves 0.25rem to the right, from
group-hover:translate-x-1. Thetransition-transformclass makes the move animate.
Hovering the heading alone also triggers all three, because hovering a child means the pointer is also over the parent.
How group works internally
group is a marker class. It has no CSS declarations of its own. Tailwind uses it only as an anchor in generated selectors. For group-hover:text-indigo-600, Tailwind generates CSS roughly like this:
.group:hover .group-hover\:text-indigo-600 {
color: oklch(0.511 0.262 276.966);
}The exact color value depends on your Tailwind version, but the shape of the selector is the point. The state (:hover) sits on the .group element, and the utility sits on a descendant.
This explains several behaviors:
- A
group-hover:class only works on a descendant of an element withgroup. If the element is a sibling, or an ancestor, nothing happens. - The marker and the target can be separated by any number of levels. A
group-hover:class works on a grandchild as well as a direct child. - The element with
groupcan also use its own variants. The marker does not stop it from usinghover:.
Which properties to change
group-hover: accepts any utility, so you can combine it with anything you would use elsewhere. Common patterns include:
- Changing text or icon color:
group-hover:text-indigo-600. - Moving or scaling an element:
group-hover:translate-x-1,group-hover:scale-105. - Revealing hidden content:
invisible group-hover:visibleoropacity-0 group-hover:opacity-100. - Changing borders, shadows, or underline:
group-hover:underline,group-hover:border-indigo-500.
Revealing content
A common use is an action that appears only when a row is hovered:
<li class="group flex items-center justify-between rounded p-3 hover:bg-slate-50">
<span>Quarterly report.pdf</span>
<button class="rounded bg-slate-200 px-2 py-1 text-sm opacity-0 transition-opacity group-hover:opacity-100">
Download
</button>
</li>The button is present in the page, so it stays in the layout and in the accessibility tree. It is only transparent until the row is hovered. This choice matters, and the next section explains why.
Gotchas
Hidden content and keyboard users
A button that is opacity-0 still receives keyboard focus. A keyboard user can tab to it and press Enter, but they see nothing. In the row example above, a keyboard user would tab onto an invisible button.
The fix is to reveal the content for focus as well as hover. Tailwind lets you add focus-visible: to the button itself:
<button class="rounded bg-slate-200 px-2 py-1 text-sm opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100">
Download
</button>The next chapter section covers group-focus and related states. The point here is that a hover-only reveal is incomplete.
Note that invisible (visibility: hidden) behaves differently from opacity-0. An element with visibility: hidden cannot receive focus and is skipped by assistive technology. If you use invisible group-hover:visible, keyboard users cannot reach the control at all unless you add a focus-based reveal on a parent. Choose the approach that fits the control. Decorative hints can use invisible. Interactive controls should stay focusable.
Missing group class
The most common error is forgetting group on the parent. The group-hover: classes are valid and Tailwind generates CSS for them, but the selector never matches. There is no warning. If a group-hover: style does nothing, check the ancestor chain first.
Hover on the wrong element
group reacts to the state of the element it sits on. If you put group on an inner wrapper instead of the card, only hovering the wrapper triggers the children. Place group on the element whose area should be the hover target.
Nested groups
If two ancestors both have group, a group-hover: class responds to either one. A child inside an inner group and an outer group reacts to hovering the outer card as well as the inner item. That is often not what you want, and it is the reason named groups exist. They are covered in a later chapter.
Pseudo-class on the target
group-hover: changes the child when the parent is hovered. It does not require the child itself to be hovered. If you want an effect that depends on both, combine the two variants: group-hover:hover:bg-indigo-100 applies only when the parent is hovered and the child is hovered too. Variants stack left to right, as covered in a later chapter.
Comparison with hover: on the child
| Goal | Use |
|---|---|
| Style an element when it is hovered | `hover:` on that element |
| Style a descendant when an ancestor is hovered | `group` on the ancestor, `group-hover:` on the descendant |
| Style a later sibling based on an earlier sibling | `peer` and `peer-*:`, covered later |
Choose group when the trigger area is bigger than the thing that changes. A whole card or table row is a good trigger. A small icon inside it is a good target.
Using group with transitions
group-hover: changes properties instantly unless the target has a transition. Put the transition utility on the element that changes, not on the group:
<div class="group rounded border p-4">
<img src="/photo.jpg" alt="Mountain lake" class="transition-transform duration-300 group-hover:scale-105" />
</div>The transition-transform and duration-300 classes are on the image, because the image is the element whose transform changes. If you put them on the parent, nothing animates.
Worked example: a clickable card
This card combines several ideas. The whole card is a link, so the entire surface works as a hover and keyboard target.
<a href="/guide" class="group block max-w-sm rounded-lg border border-slate-200 p-5 transition-colors hover:bg-slate-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
<h3 class="font-semibold text-slate-900 transition-colors group-hover:text-indigo-600">Read the guide</h3>
<p class="mt-1 text-sm text-slate-600">Learn how state variants work.</p>
<span class="mt-3 inline-block text-indigo-600 transition-transform group-hover:translate-x-1">→</span>
</a>Check each piece:
groupis on the link, which is the element that receives the pointer and keyboard focus.- The link has its own
hover:bg-slate-50and a focus ring fromfocus-visible:. - The heading and arrow are descendants with
group-hover:classes. - Each changing element has its own transition utility.
Common mistakes at a glance
| Symptom | Likely cause | Fix |
|---|---|---|
| `group-hover:` does nothing | No `group` on an ancestor | Add `group` to the parent |
| Child changes when you hover a different card section | `group` is on the wrong element | Move `group` to the intended trigger |
| Style changes but does not animate | Transition is missing or on the parent | Add `transition-*` to the child |
| Revealed button cannot be reached by keyboard | Hidden with `invisible`, or no focus reveal | Use `opacity-0` with `focus-visible:opacity-100` |
| Inner item reacts to the outer card | Nested unnamed groups | Use named groups |
Exercise
A list row has the classes flex justify-between p-3. Inside it is an edit link with opacity-0 group-hover:opacity-100. Hovering the row does not show the link. What is missing, and how would you also make the link visible to keyboard users?
Show answer
The row needs the group class, because group-hover: only matches descendants of an element marked group. For keyboard users, add focus-visible:opacity-100 to the link so it becomes visible when it receives focus by keyboard.
Exercise
An image inside a card uses group-hover:scale-110 and the card has group. The image jumps to the larger size with no animation. Which element needs which classes?
Show answer
The image needs transition-transform (and optionally a duration such as duration-300). The transition must be on the element whose property changes, which is the image, not the card.