The peer modifier lets one element change style when its sibling changes state, like when a checkbox is checked.
Peer modifier (sibling state reaction) in Tailwind
peer:[state]:[utility]
The element that changes state must have the class peer.
The sibling uses peer- prefix to react to that state.
<input type="checkbox" class="peer" /> <label class="peer-checked:text-blue-600">Check me</label>
<input type="radio" class="peer" /> <div class="peer-checked:bg-green-200">Selected</div>
<input type="text" class="peer" /> <p class="peer-focus:text-red-500">Input is focused</p>
This example has a hidden checkbox with class peer. The label changes background and text color when the checkbox is checked. The sibling span text color also changes using peer-checked.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Peer Modifier Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex flex-col items-center justify-center min-h-screen bg-gray-50 p-4"> <h1 class="text-2xl font-semibold mb-4">Peer Modifier Demo</h1> <div class="flex items-center space-x-3"> <input type="checkbox" id="toggle" class="peer hidden" /> <label for="toggle" class="cursor-pointer px-4 py-2 border rounded peer-checked:bg-blue-600 peer-checked:text-white"> Toggle Me </label> <span class="text-gray-700 peer-checked:text-blue-600"> Status changes when toggle is checked </span> </div> </body> </html>
The peer class must be on the element whose state you want to watch.
Only siblings after the peer element can react to its state.
This works well for inputs like checkboxes, radios, and focus states.
The peer modifier lets one element style its siblings based on its state.
Use peer on the element changing state, and peer-* on siblings reacting.
This helps create interactive UI without JavaScript.
Practice
peer modifier in Tailwind CSS allow you to do?Solution
Step 1: Understand the role of
Thepeerpeermodifier is used on an element to mark it as a reference for sibling elements.Step 2: Recognize sibling styling with
Siblings can then usepeer-*peer-*classes to change their style based on thepeerelement's state.Final Answer:
Style sibling elements based on the state of the element withpeer. -> Option BQuick Check:
Peer modifier = sibling state styling [OK]
- Thinking peer styles the element itself
- Confusing peer with hover or focus on the same element
- Assuming peer adds animations automatically
peer modifier in Tailwind CSS?Solution
Step 1: Identify correct usage of
The element that changes state (checkbox) must have the classpeeron the inputpeer.Step 2: Use
The sibling usespeer-checkedon sibling labelpeer-checked:prefix to style based on the checkbox state.Final Answer:
<input type='checkbox' class='peer' /><label class='peer-checked:text-blue-500'>Check me</label> -> Option AQuick Check:
Peer on input, peer-checked on sibling [OK]
- Using peer-checked on the input instead of sibling
- Mixing up class names like checked-peer
- Not adding peer class to the element changing state
<span> text be when the checkbox is checked?<input type='checkbox' class='peer' id='toggle' /> <span class='peer-checked:text-green-600'>Status</span>
Solution
Step 1: Check the peer class on checkbox
The checkbox has classpeer, so siblings can react to its state.Step 2: Analyze the sibling span's class
The span usespeer-checked:text-green-600, so when checkbox is checked, text color changes to green.Final Answer:
The text will be green (#16a34a) when checked. -> Option CQuick Check:
peer-checked:text-green-600 means green text on check [OK]
- Assuming text changes color without peer class
- Confusing green-600 with red or blue
- Ignoring that peer must be on the input
<input type='checkbox' id='check' class='checked' /> <p class='peer-checked:text-red-500'>Warning</p>
Solution
Step 1: Understand the role of 'peer' and 'checked'
The 'peer' class marks the element for sibling state detection. 'checked' is not a valid Tailwind class for inputs.Step 2: Identify why sibling styles don't apply
The sibling uses 'peer-checked', which reacts to the input's checked state only if the input has 'peer' class. 'checked' class on input does nothing.Final Answer:
Because the 'checked' class on input does not trigger sibling styles; only 'peer' does. -> Option AQuick Check:
Only 'peer' triggers sibling state styles [OK]
- Thinking 'checked' class triggers styles
- Assuming multiple classes break peer
- Confusing 'checked' class with input state
<input type='checkbox' class='peer' /> <button class='???'>Submit</button>
Solution
Step 1: Identify the states to react to on the checkbox
The checkbox can becheckedorfocused. We want the button to change background in both cases.Step 2: Use correct peer modifiers on the button
Usepeer-checked:bg-blue-600for checked state andpeer-focus:bg-blue-600for focus state on the sibling button.Final Answer:
peer-checked:bg-blue-600 peer-focus:bg-blue-600 -> Option DQuick Check:
Use peer-checked and peer-focus for sibling states [OK]
- Using focus:bg-blue-600 instead of peer-focus
- Using checked:bg-blue-600 without peer prefix
- Confusing focus-within with peer-focus
Ready to prove it?
Pass the test and this pattern moves unknown
