Interactive Button Group with Peer Modifier in Tailwind CSS
📖 Scenario: You are building a simple button group where clicking one button highlights it and changes the style of its sibling buttons. This is a common UI pattern for toggles or filters.
🎯 Goal: Create a group of three buttons styled with Tailwind CSS. When you hover over one button, the other buttons change their background color using the peer modifier to react to sibling state changes.
📋 What You'll Learn
Use Tailwind CSS utility classes
Create three buttons inside a container
Use the peer class on each button
Use peer-hover to change sibling buttons' background color on hover
Ensure the buttons have accessible labels and keyboard focus styles
💡 Why This Matters
🌍 Real World
Peer modifiers are useful for interactive UI elements like tabs, toggles, and filters where one element's state affects others.
💼 Career
Understanding peer modifiers in Tailwind CSS helps build accessible, interactive, and visually responsive web components used in modern web applications.
Progress0 / 4 steps
1
Create the button group container and three buttons
Write HTML to create a <div> container with the class flex. Inside it, create three <button> elements with the text Button 1, Button 2, and Button 3. Each button should have the Tailwind class px-4 py-2 border.
Tailwind
Hint
Use a <div> with class flex to arrange buttons horizontally. Add three <button> elements with the exact classes and text.
2
Add the peer class to each button
Add the Tailwind class peer to each of the three buttons inside the container.
Tailwind
Hint
The peer class allows sibling elements to react to this button's state. Add it to all buttons.
3
Use peer-hover to change sibling buttons' background on hover
Add the class peer-hover:bg-gray-200 to each button so that when a sibling button is hovered, this button's background changes to gray-200.
Tailwind
Hint
The peer-hover modifier changes this button's style when a sibling with peer is hovered. Add it to all buttons.
4
Add accessible focus styles and aria-labels
Add the Tailwind class focus:outline-none focus:ring-2 focus:ring-blue-500 to each button for keyboard focus styles. Also add aria-label attributes with values Button 1, Button 2, and Button 3 respectively.
Tailwind
Hint
Focus styles help keyboard users see which button is focused. Aria-labels improve screen reader accessibility. Add both to each button.
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?