Sub topic 11
Reacting to a sibling with peer
This chapter assumes you know how group-hover: works, can read basic HTML and CSS, and have a Tailwind project set up. The group variant styles children from a parent. The peer variant solves a different problem: styling an element based on the state of a sibling that comes before it.
Why peer exists
CSS can only style an element from the state of an ancestor or an earlier sibling. It cannot look "backward" to a later sibling. Tailwind's peer follows the same rule, using the CSS subsequent-sibling combinator (~). That is why the order of your HTML matters.
The pattern has two parts:
- Add the
peerclass to the element whose state you want to watch. - Add
peer-*variants, such aspeer-invalid:orpeer-checked:, to a later sibling that should react.
A first example: validation message
An email input is marked peer. The message after it appears when the input's value is invalid.
<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>The invalid: variant on the input styles the input itself. The peer-invalid: variant on the paragraph styles the paragraph when the peer is invalid. The paragraph uses invisible rather than hidden so it keeps its space and the layout does not jump.
One detail: an empty email input is valid unless it has the required attribute. The message appears only after the user types something that is not an email address.
Order matters
The reacting element must come after the peer in the HTML. In this example the message sits before the input, so nothing happens:
<p class="invisible text-sm text-red-600 peer-invalid:visible">Enter a valid email address.</p>
<input type="email" class="peer rounded border px-3 py-2" />If you need the message visually above the input, keep the HTML order and move it with layout utilities such as flex flex-col-reverse on the parent.
Other peer states
Every state you can use with group-* has a peer-* form. Common ones are peer-focus:, peer-checked:, peer-disabled:, and peer-placeholder-shown:.
This checkbox toggle changes the label text and a track color through peer-checked::
<label class="flex items-center gap-3">
<input type="checkbox" class="peer sr-only" />
<span class="h-6 w-11 rounded-full bg-slate-300 transition-colors peer-checked:bg-indigo-600 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-indigo-600"></span>
<span class="text-slate-700 peer-checked:text-indigo-700">Notifications</span>
</label>The checkbox is hidden visually with sr-only but stays available to keyboards and screen readers. Both spans come after it, so both can react. Note peer-focus-visible: on the track: because the real input is hidden, the visible element must show the keyboard focus ring.
Multiple peers on one page
An unnamed peer matches any earlier sibling marked peer. With several peers in the same parent, the variant reacts to whichever one is a previous sibling, which can produce surprises. Name them, as you did with groups:
<div>
<input id="email" type="email" class="peer/email border px-3 py-2" />
<input id="phone" type="tel" class="peer/phone border px-3 py-2" />
<p class="invisible text-red-600 peer-invalid/email:visible">Check the email address.</p>
</div>Here peer-invalid/email: responds only to the email input.
Common errors
- Placing the target before the peer. The sibling selector only looks forward.
- Adding
peerto a parent or child. The two elements must share the same parent. - Wrapping the target in another element. If a
divwraps the message, the message is no longer a direct sibling, so thedivmust be the one carrying the variant. - Expecting the peer to react to the target. The relationship is one way.
Exercise
A page has <p class="peer-checked:font-bold">Agree</p> placed above <input type="checkbox" class="peer" /> in the same parent. Why does the text never become bold, and how do you fix it?
Show answer
The paragraph comes before the peer, and the peer-* variants compile to a subsequent-sibling selector, which only matches elements that come after the peer. Move the paragraph after the checkbox in the HTML. If it should still appear above visually, reorder it with layout utilities such as flex-col-reverse.
Exercise
A form has two inputs marked peer in the same parent, followed by one error message. How do you make the message react only to the second input?
Show answer
Give the second input a named peer, such as peer/phone, and give the message the matching variant, such as peer-invalid/phone:visible.