The peer modifier lets you style an element based on the state of a sibling element that has the peer class. It helps create interactive UI without JavaScript.
Click to reveal answer
beginner
How do you use peer-hover in Tailwind CSS?
Add <code>peer</code> class to one element and <code>peer-hover:</code> prefix to a sibling's class. The sibling changes style when the peer is hovered.
Click to reveal answer
intermediate
Why use peer modifier instead of JavaScript for sibling state changes?
Using peer keeps styling in CSS, making code simpler, faster, and easier to maintain. It also improves accessibility and performance.
Click to reveal answer
beginner
Example: What does this code do?
<input type="checkbox" class="peer" />
<label class="peer-checked:text-blue-600">Check me</label>
When the checkbox is checked, the label text color changes to blue. The label reacts to the checkbox state using peer-checked.
Click to reveal answer
intermediate
Can peer modifier work with any sibling element?
Yes, as long as the sibling has the peer class, you can style other siblings based on its state using peer-* variants.
Click to reveal answer
What does the peer class do in Tailwind CSS?
AApplies a hover effect to the element
BMakes the element hidden
CMarks an element whose state can affect sibling styles
DAdds margin to the element
✗ Incorrect
The peer class marks an element so sibling elements can react to its state using peer-* variants.
Which prefix would you use to style a sibling when the peer is focused?
Apeer-hover:
Bpeer-focus:
Cpeer-active:
Dpeer-checked:
✗ Incorrect
Use peer-focus: to style siblings when the peer element is focused.
If an input has class peer, how do you change a sibling's text color on input checked state?
AUse <code>peer-checked:</code> on sibling
BUse <code>focus:</code> on sibling
CUse <code>hover:</code> on sibling
DUse <code>checked:</code> on sibling
✗ Incorrect
The sibling uses peer-checked: to react to the peer input's checked state.
Can peer modifier be used to style parent elements?
AYes, always
BOnly with JavaScript
COnly with special plugins
DNo, only siblings
✗ Incorrect
peer modifier only works for sibling elements, not parents.
What is a benefit of using peer modifier over JavaScript for sibling state styling?
AFaster page load and simpler code
BMore complex code
CRequires extra libraries
DOnly works on mobile
✗ Incorrect
Using peer keeps styling in CSS, making code simpler and faster without extra scripts.
Explain how the peer modifier works in Tailwind CSS and give a simple example.
Think about how one element's state can change another sibling's style.
You got /3 concepts.
Describe a real-life scenario where using the peer modifier improves user experience without JavaScript.
Consider checkboxes or toggles that change label styles.
You got /3 concepts.
Practice
(1/5)
1. What does the peer modifier in Tailwind CSS allow you to do?
easy
A. Add margin to an element when it is focused.
B. Style sibling elements based on the state of the element with peer.
C. Change the color of the element itself on hover.
D. Create animations on the element when clicked.
Solution
Step 1: Understand the role of peer
The peer modifier is used on an element to mark it as a reference for sibling elements.
Step 2: Recognize sibling styling with peer-*
Siblings can then use peer-* classes to change their style based on the peer element's state.
Final Answer:
Style sibling elements based on the state of the element with peer. -> Option B
Quick Check:
Peer modifier = sibling state styling [OK]
Hint: Peer lets siblings react to one element's state [OK]
Common Mistakes:
Thinking peer styles the element itself
Confusing peer with hover or focus on the same element
Assuming peer adds animations automatically
2. Which of the following is the correct way to use the peer modifier in Tailwind CSS?
easy
A. <input type='checkbox' class='peer' /><label class='peer-checked:text-blue-500'>Check me</label>
B. <input type='checkbox' class='peer-checked' /><label class='peer:text-blue-500'>Check me</label>
C. <input type='checkbox' class='checked-peer' /><label class='text-blue-500'>Check me</label>
D. <input type='checkbox' class='peer' /><label class='checked-peer:text-blue-500'>Check me</label>
Solution
Step 1: Identify correct usage of peer on the input
The element that changes state (checkbox) must have the class peer.
Step 2: Use peer-checked on sibling label
The sibling uses peer-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 A
Quick Check:
Peer on input, peer-checked on sibling [OK]
Hint: Put peer on state element, peer-* on siblings [OK]
Common Mistakes:
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
3. Given this HTML snippet, what color will the <span> text be when the checkbox is checked?