Sub topic 9
Using group with focus and other states
This chapter assumes you know how group and group-hover: work, and that you are comfortable with focus, focus-visible, and focus-within. Here you extend the same pattern to other states, so that a parent can react to keyboard focus, and not only to the mouse.
The pattern behind group-*
group-hover: is one case of a general rule. Mark a parent with group. Then write group-<state>: on any descendant to apply a utility when the parent is in that state. Tailwind generates a selector like .group:focus .group-focus\:underline, so the child styles only when the marked ancestor matches the state.
Any state variant you already know has a group- form:
group-focus:when the parent itself has focus.group-focus-visible:when the parent has keyboard focus.group-focus-within:when the parent or any descendant has focus.group-active:while the parent is pressed.group-disabled:when the parent is disabled.
Why hover alone is not enough
A card that reveals an arrow on hover gives keyboard users nothing. They never trigger hover. Pair every group-hover: with a focus form on the same element so that both input methods get the same feedback.
<a href="#" class="group block rounded-lg border border-slate-200 p-4 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 group-hover:text-indigo-600 group-focus-visible:text-indigo-600">Read the guide</h3>
<span class="inline-block transition-transform group-hover:translate-x-1 group-focus-visible:translate-x-1">→</span>
</a>The link is the group. Tabbing to it moves the arrow and recolors the heading, the same as hovering. Because group-focus-visible: matches only keyboard-style focus, a mouse click does not trigger it.
Choosing between focus and focus-within
The choice depends on which element the group is.
| Group element | Use | Reason |
|---|---|---|
| A link or button | `group-focus-visible:` | The group itself receives focus. |
| A container with inputs inside | `group-focus-within:` | Focus lands on a child, not on the container. |
group-focus: on a div does nothing unless the div is focusable, for example with tabindex. This is a common error. If you mark a wrapper as group and use group-focus:, the style never appears when the user tabs into an input inside it.
The following field wrapper highlights its label when the input inside has focus.
<div class="group">
<label for="email" class="block text-sm text-slate-600 group-focus-within:text-indigo-600">
Email
</label>
<input id="email" type="email" class="w-full rounded border border-slate-300 px-3 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-600" />
</div>Here the group is the div, and the label is a sibling of the input, not a descendant of it. group-focus-within: handles this because it matches when anything inside the div has focus.
Combining states
Group variants stack with other variants in the same way as ordinary ones. This example changes a child only when the parent is hovered and the viewport is at least md:
<div class="group rounded border p-4">
<p class="text-slate-500 md:group-hover:text-slate-900 dark:md:group-hover:text-white">Details</p>
</div>Common errors
- Forgetting the
groupclass on the ancestor. Thegroup-*utilities then have no effect. - Using
group-focus:on a non-focusable wrapper. Switch togroup-focus-within:. - Writing only
group-hover:, which leaves keyboard users without feedback. - Putting
groupon the child that should change. The marker belongs on the ancestor whose state you are reading.
Exercise
A card is a div with class group that contains a text input. You want the card's title to turn indigo when the user tabs into the input. Which variant should the title use, and why does group-focus: fail?
Show answer
Use group-focus-within:text-indigo-600. Focus lands on the input, not on the div, so the div never matches :focus. It does match :focus-within, because a descendant has focus. group-focus: would work only if the div itself were focusable.