Sub topic 9
Using group with focus and other states
This chapter assumes you know how group and group-hover work from the previous chapter. It also assumes you know the plain CSS pseudo-classes :focus, :focus-visible, :focus-within, :active, and :disabled. The goal is to extend group beyond hover, so a parent can restyle its children for any state, including the keyboard states that hover-only designs leave out.
The pattern behind every group-* variant
group-hover: is one case of a general rule. Mark a parent with the group class. Then write group-<state>: on any descendant, where <state> is any variant Tailwind supports on the parent. The descendant's utility applies while the parent is in that state.
Tailwind generates a selector with the parent state first and the child second. Here is a simplified view of group-focus:text-indigo-600:
.group:focus .group-focus\:text-indigo-600 {
color: #4f46e5;
}Two consequences follow from this selector.
- The utility applies to descendants of the
groupelement, never to the group element itself. To style the parent, use the plain variant on the parent, such ashover:orfocus:. - The state must be a state the parent can actually have. A
divis not focusable by default, sogroup-focus:on its children never fires unless thedivhastabindex.
Common group states
| Child variant | Applies when the group element matches | Typical group element |
|---|---|---|
| `group-hover:` | `:hover` | Card, link, list row |
| `group-focus:` | `:focus` | Button, link, element with `tabindex` |
| `group-focus-visible:` | `:focus-visible` | Button, link |
| `group-focus-within:` | `:focus-within` | Card or row that contains a link, button, or input |
| `group-active:` | `:active` | Button, link |
| `group-disabled:` | `:disabled` | Button, `fieldset` |
| `group-open:` | `[open]` | `details` |
Other variants work the same way, for example group-checked: on a group element that is a checked input.
group-focus and group-focus-visible
Use group-focus-visible: when a focusable parent such as a button or link wraps decorative children and you want them to react to keyboard focus. The browser applies :focus-visible mainly for keyboard focus, so mouse clicks do not trigger it.
<a href="/docs" class="group inline-flex items-center gap-2 rounded px-3 py-2 text-slate-900 hover:bg-slate-100 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
<span class="group-hover:text-indigo-600 group-focus-visible:text-indigo-600">Documentation</span>
<span class="transition-transform group-hover:translate-x-1 group-focus-visible:translate-x-1">→</span>
</a>The link shows its own outline with focus-visible:. The label and arrow react through group-focus-visible:. The hover and focus-visible utilities repeat the same value, which is deliberate. Keyboard users should get the same feedback as mouse users.
Plain group-focus: also works here. It differs only in that it fires after a mouse click on a button or link, which is often unwanted for decorative changes.
group-focus-within for cards that contain controls
A card usually is not focusable itself. The link or button inside it is. group-focus on the card never fires, because focus sits on the child. The state to use is :focus-within, which matches an element when it or any descendant has focus.
<article class="group relative rounded-lg border border-slate-200 p-4 hover:bg-slate-50 focus-within:bg-slate-50 focus-within:ring-2 focus-within:ring-indigo-600/40">
<h3 class="font-semibold text-slate-900 group-focus-within:text-indigo-600">Quarterly report</h3>
<p class="text-sm text-slate-600">Updated yesterday</p>
<button type="button" class="absolute right-3 top-3 rounded bg-white px-2 py-1 text-sm opacity-0 shadow transition-opacity group-hover:opacity-100 group-focus-within:opacity-100 focus-visible:opacity-100">
Edit
</button>
</article>This card has three parts that work together:
focus-within:on the card styles the card itself when anything inside has focus.group-focus-within:text-indigo-600styles the heading for the same condition.- The Edit button is invisible until the card is hovered or focus is inside the card.
Hide the button with opacity-0, not hidden or invisible. An element with display: none or visibility: hidden cannot receive focus, so keyboard users could never Tab to it. With opacity-0 the button stays focusable. When it receives focus, the card gains :focus-within, which makes the button visible. The extra focus-visible:opacity-100 is a safety net in case the button is ever used outside a group.
group-active for pressed feedback
:active matches while the mouse button or finger is down on the element. A group-active: utility lets child parts respond to that press.
<button type="button" class="group flex items-center gap-2 rounded-lg bg-slate-900 px-4 py-2 text-white hover:bg-slate-800 active:bg-slate-950">
<span class="inline-block transition-transform group-active:scale-90">★</span>
<span class="group-active:opacity-80">Favorite</span>
</button>The button darkens with its own active: variant. The star shrinks and the label dims through group-active:. The star is inline-block because transform has no effect on a plain inline element.
group-disabled for forms
Only some elements can match :disabled: form controls and fieldset. A disabled fieldset also disables the controls inside it, which makes it a convenient group for a whole form section.
<fieldset class="group space-y-2 rounded border border-slate-200 p-4" disabled>
<legend class="px-1 text-sm font-medium text-slate-900 group-disabled:text-slate-400">Billing address</legend>
<label class="block text-sm text-slate-700 group-disabled:text-slate-400" for="street">Street</label>
<input id="street" class="w-full rounded border px-3 py-2 disabled:cursor-not-allowed disabled:bg-slate-100" />
</fieldset>The legend and label are not disabled elements. They match group-disabled: because the fieldset ancestor is disabled. Removing the disabled attribute from the fieldset restores everything at once, with no extra classes to toggle.
group-open for details and summary
The details element toggles an open attribute, and Tailwind exposes it as the open: variant. That makes group-open: useful for rotating a chevron.
<details class="group rounded border border-slate-200 p-3">
<summary class="flex cursor-pointer list-none items-center justify-between font-medium text-slate-900 focus-visible:outline-2 focus-visible:outline-indigo-600">
Shipping details
<span class="transition-transform group-open:rotate-180">▼</span>
</summary>
<p class="mt-2 text-sm text-slate-600">Orders ship within two business days.</p>
</details>The browser handles opening and closing. No JavaScript is needed. The chevron rotates because the details parent gains the open attribute.
Combining states on one child
A child can listen to several group states at once. Variants combine by writing each utility separately, as in the earlier examples. Do not try to write a compound like group-hover-focus:. That variant does not exist. If the effect is long, repeat the pair.
<li class="group flex items-center justify-between p-2 hover:bg-slate-50 focus-within:bg-slate-50">
<a href="#" class="text-slate-900 underline-offset-2 group-hover:underline group-focus-within:underline">Invoice 1042</a>
<span class="text-sm text-slate-500 group-hover:text-slate-900 group-focus-within:text-slate-900">View</span>
</li>Common errors
- Missing
groupon the parent. Without it, nogroup-*class does anything and Tailwind gives no warning. - Using
group-focuson a card that holds a link. Focus is on the link, not the card. Usegroup-focus-within. - Expecting the group element to restyle itself. Use
hover:,focus-within:, oractive:on the group element. - Hiding with
hiddenorinvisible. Hidden controls cannot take focus. Useopacity-0orsr-onlypatterns for reveal effects. - Non-focusable parent.
group-focus:on a plaindivnever fires. Either addtabindex="0"when the element really is interactive, or choose a different state. - Nested groups. An unnamed
groupinside anothergroupmakes both parents match everygroup-*utility. The next chapter covers named groups (group/card) as the fix.
Exercise
A card contains a title link and a "Delete" button. The button has opacity-0 group-hover:opacity-100. Keyboard users Tab to the button and see nothing. What one class should you add to the button, and why does group-focus: on the card not solve the problem?
Show answer
Add group-focus-within:opacity-100 to the button, and make sure the card has the group class. When the button or the link has focus, the card matches :focus-within, so the button becomes visible. group-focus: does not work because it requires the card itself to have focus. The card is not focusable, so focus sits on the link or button inside it.
Exercise
You wrap a form section in <fieldset class="group" disabled>. You want its <label> text to turn gray while the fieldset is disabled. Write the class for the label and explain why disabled:text-slate-400 on the label would be a poor choice.
Show answer
Use group-disabled:text-slate-400 on the label. A label is not a form control, so it never matches :disabled, and disabled:text-slate-400 on it would never apply. The label gets the style through its disabled fieldset ancestor.