Sub topic 11
Reacting to a sibling with peer
This chapter assumes you know basic HTML and CSS, including pseudo-classes such as :hover and :focus. It also assumes you know common Tailwind utilities for color, padding, and borders, and that you have a project where Tailwind CSS is already set up. You should also know how group and group-hover: work, because peer follows the same idea with a different relationship.
The problem peer solves
A variant such as hover: or focus: applies a utility when the element that carries the class is in that state. Sometimes you want the opposite: an element should change when a different element is in a state. You have seen one case already. With group, a child reacts to its parent.
Siblings are the other common case. Consider a form field. The input holds the state, such as invalid, focused, or checked. A message or label next to the input needs to react to that state. The message cannot use invalid: because the message itself is never invalid. The input is.
Tailwind solves this with the peer class and peer-* variants. You mark the element that holds the state with peer. Then you prefix utilities on a later sibling with peer- plus the state, for example peer-invalid: or peer-focus:.
How peer works
peer is a marker class. It adds no styles by itself. Tailwind uses it as an anchor in the CSS it generates.
When you write peer-invalid:visible on an element, Tailwind generates a rule that uses the CSS subsequent-sibling combinator (~). The rule says, in effect: apply visibility: visible to this element when it follows a .peer element that matches :invalid.
This is plain CSS selector logic, so the CSS rule decides what is possible. One rule matters most.
Here is the example from the plan. The input is the peer. The message is a later sibling.
<input type="email" class="peer rounded border px-3 py-2 invalid:border-red-500" placeholder="[email protected]" />
<p class="invisible text-sm text-red-600 peer-invalid:visible">Enter a valid email address.</p>Two variants appear here, and they do different jobs:
invalid:border-red-500is on the input. It styles the input from its own state.peer-invalid:visibleis on the paragraph. It styles the paragraph from the input's state.
The paragraph uses invisible instead of hidden. The invisible utility sets visibility: hidden, which keeps the paragraph's space in the layout. That means the form does not jump when the message appears. If you prefer the message to take no space until needed, use hidden with peer-invalid:block.
A gotcha with invalid
The :invalid pseudo-class matches as soon as the page loads. An empty input with the required attribute is invalid immediately, so the error would show before the user has typed anything. An email input without required is valid while empty, which is why the example above shows no error at first.
To avoid early errors on required fields, combine peer-invalid: with a placeholder trick. Add placeholder-shown to the stack so the message only appears once the field is no longer showing its placeholder:
<input
type="email"
required
placeholder="[email protected]"
class="peer rounded border px-3 py-2 invalid:[&:not(:placeholder-shown)]:border-red-500"
/>
<p class="hidden text-sm text-red-600 peer-[&:not(:placeholder-shown):invalid]:block">
Enter a valid email address.
</p>This uses arbitrary variants, which the last chapter in this tutorial covers in more detail. The bracket syntax lets you write a custom selector. Here &:not(:placeholder-shown):invalid means "the peer is not showing its placeholder and is invalid". Note that placeholder-shown only works when the input has a placeholder attribute, even a single space.
Common peer states
Any state variant you can use directly on an element can be used with the peer- prefix. These are the ones you will use most.
| Variant | Triggers when the peer is |
|---|---|
| `peer-hover:` | hovered |
| `peer-focus:` | focused |
| `peer-focus-visible:` | focused in a way that should show a focus ring, such as by keyboard |
| `peer-checked:` | a checked checkbox or radio button |
| `peer-disabled:` | disabled |
| `peer-invalid:` | failing validation |
| `peer-required:` | marked required |
| `peer-placeholder-shown:` | showing its placeholder text |
Example: a custom checkbox
A common use of peer-checked: is a custom checkbox or toggle. The native input is visually hidden but still present, so it stays keyboard accessible and still submits with the form. A sibling element draws the visible control.
<label class="flex cursor-pointer items-center gap-3">
<input type="checkbox" class="peer sr-only" />
<span class="flex h-6 w-6 items-center justify-center rounded border-2 border-slate-400 text-transparent transition-colors peer-checked:border-indigo-600 peer-checked:bg-indigo-600 peer-checked:text-white peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-indigo-600">
✓
</span>
<span class="text-slate-900 peer-checked:font-semibold">Subscribe to updates</span>
</label>Notice the following details:
- The input uses
sr-only, nothidden. Tailwind'shiddensetsdisplay: none, which removes the input from the tab order and from screen readers.sr-onlyhides it visually but keeps it focusable. - Both
<span>elements are later siblings of the input, so both can react to it. One peer can drive any number of siblings. peer-focus-visible:draws a focus outline on the custom box when the hidden input is focused with the keyboard. Without it, keyboard users would see no focus indicator at all. This ties back to the earlier chapter on keeping focus visible.- Clicking the label toggles the input because the input sits inside the
<label>.
Example: a floating label
Peers can also style a label based on an input's focus and content. In this pattern the input comes first, and the label follows it.
<div class="relative">
<input
id="name"
type="text"
placeholder=" "
class="peer w-full rounded border border-slate-300 px-3 pt-5 pb-2 focus:border-indigo-600 focus:outline-none"
/>
<label
for="name"
class="pointer-events-none absolute left-3 top-1 text-xs text-slate-500 transition-all peer-placeholder-shown:top-3.5 peer-placeholder-shown:text-base peer-focus:top-1 peer-focus:text-xs peer-focus:text-indigo-600"
>
Full name
</label>
</div>The placeholder is a single space so that placeholder-shown is true only while the field is empty. When the field is empty and unfocused, the label sits large inside the box. When the user focuses the field or types, the label shrinks to the top.
The order of the peer-focus: utilities after peer-placeholder-shown: matters. When the field is empty and focused, both variants match. Tailwind generates variant rules in a defined order, and the later one wins when specificity ties, so the focus styles override the placeholder-shown styles. If you see the wrong style win, check which variants match at once.
Also note the focus handling here. The input removes its default outline with focus:outline-none and replaces it with a visible border color change. That is acceptable only because a replacement indicator exists. Never remove the outline without one.
Named peers
Like group, peer can be named. Naming matters when a page has several peers among the same siblings and you need to say which one you mean. Add a name after a slash: peer/name, then use peer-checked/name:.
<div class="flex flex-col gap-2">
<input id="draft" type="radio" name="status" class="peer/draft sr-only" />
<input id="published" type="radio" name="status" class="peer/published sr-only" />
<label for="draft" class="rounded border p-2 peer-checked/draft:border-amber-500 peer-checked/draft:bg-amber-50">Draft</label>
<label for="published" class="rounded border p-2 peer-checked/published:border-green-600 peer-checked/published:bg-green-50">Published</label>
</div>Without names, both labels would respond to any checked peer before them. With names, each label responds only to its own radio input.
Peer versus group
| Row | `group` | `peer` |
|---|---|---|
| Marker goes on | The ancestor | The earlier sibling |
| Reacting element | A descendant | A later sibling (and its descendants are not reached directly) |
| Variant syntax | `group-hover:`, `group-focus:` | `peer-hover:`, `peer-focus:` |
| Named form | `group/card`, `group-hover/card:` | `peer/email`, `peer-invalid/email:` |
| Underlying CSS | descendant selector | subsequent-sibling selector (`~`) |
If the element you need to style is inside the state-holding element's parent, but not after it, neither one works directly. Restructure the HTML so the state holder comes first, or put a group on the shared parent and use a state that the parent can detect, such as group-has-[:checked]:. The has-* approach needs a browser that supports :has().
Common errors
- The peer comes after the reacting element. The style never applies. Move the peer earlier in the markup. If layout requires the opposite visual order, use
flex-col-reverseor theorder-*utilities to change the display order and leave the HTML order alone. - The elements are not siblings. If a wrapper
<div>sits between the input and the message, the combinator no longer matches. Keep them under the same parent. - The peer class is missing. The
peer-*variants do nothing without apeermarker on the state-holding element. - Using
hiddenon a native checkbox. The control cannot be reached by keyboard. Usesr-only. - Error messages visible on load.
:invalidmatches empty required fields immediately. Useplaceholder-shownas shown earlier, or show errors only after interaction. - Unnamed peers collide. More than one
peeramong the same siblings means everypeer-*variant reacts to all of them. Name them.
Summary
peermarks an element whose state other siblings need to read.peer-*variants apply only to later siblings under the same parent, because they compile to the~selector.- Hide native inputs with
sr-only, nothidden, and keep a visible focus indicator withpeer-focus-visible:. - Name peers with
peer/namewhen several peers share one parent.
Exercise
A form has <input class="peer" required> followed by <div><p class="hidden peer-invalid:block">Required</p></div>. The message never appears, even when the input is invalid. Give the cause and one fix.
Show answer
The <p> is not a sibling of the input. It sits inside a wrapper <div>, and peer-invalid: compiles to a subsequent-sibling selector, which only matches elements that share the peer's parent. Fix it by moving the <p> to be a direct later sibling of the input, or by putting the peer-invalid:block class on the wrapper <div> itself, which is a later sibling, and making the <p> visible by default inside it.
Exercise
Write the classes for a <label> placed after <input type="checkbox" class="peer sr-only"> so the label text turns indigo-600 and bold when the box is checked.
Show answer
class="text-slate-900 peer-checked:font-bold peer-checked:text-indigo-600". The checkbox is the peer, the label is a later sibling, and peer-checked: applies the utilities when the checkbox is checked.
Exercise
Two radio inputs share a parent with two labels. Both radios use the plain peer class, and both labels use peer-checked:bg-green-100. What goes wrong, and how do you fix it?
Show answer
Each label reacts to any earlier checked peer, so a label can turn green because the other radio is checked. Name each peer, for example peer/a and peer/b, and use peer-checked/a:bg-green-100 on the label tied to the first radio and peer-checked/b:bg-green-100 on the other.