Course contentsShow
← Web Development
Lesson 639 of 2,98211. Utility-First CSS with TailwindFree lesson

Peer State Variants

Leveraging peer and peer-checked:, peer-focus: to style elements based on sibling state, useful for custom form controls.

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

  1. Mark the control element with the peer class (usually an <input>)
  2. Target sibling elements using peer-*: variants
  3. 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 checked
  • peer-focus: — styles apply when the peer has focus
  • peer-hover: — styles apply when hovering over the peer
  • peer-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 peer class
  • Label gets peer-checked:bg-blue-500 to 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.