Peer State Variants
What you'll learn: How to style elements based on their sibling's state using Tailwind's peer utilities—perfect for custom checkboxes and form controls.
The Core Idea
Sometimes you need to style one element based on the state of a sibling element (not a parent-child relationship). Tailwind's peer system lets you do this by marking one element with the peer class, then using variants like peer-checked:, peer-focus:, or peer-hover: on siblings to apply conditional styles.
This is incredibly useful for custom form controls. For example, when building a custom checkbox, the actual <input> is often hidden, but you want to style a visible label or icon based on whether that input is checked.
How It Works
- Mark the control element with the
peerclass (usually an<input>) - Target sibling elements using
peer-*:variants - The sibling will update its styles when the peer's state changes
Think of it like this: the peer element is the "broadcaster" and its siblings are "listeners" that react to its state changes.
Common Peer Variants
peer-checked:— styles apply when a checkbox/radio is checkedpeer-focus:— styles apply when the peer has focuspeer-hover:— styles apply when hovering over the peerpeer-disabled:— styles apply when the peer is disabled
These work just like the group-*: variants you learned, but for siblings instead of descendants.
Typical Use Case
A hidden checkbox with a visible custom design:
- Input gets
peerclass - Label gets
peer-checked:bg-blue-500to show selection
Key Takeaway: Use peer and peer-*: variants to style sibling elements based on form control states—essential for building custom checkboxes, toggles, and radio buttons without JavaScript.