Discover how to make elements react to each other's states without writing a single line of JavaScript!
Why Peer modifier (sibling state reaction) in Tailwind? - Purpose & Use Cases
Imagine you have a form with a checkbox and a text input next to it. You want the text input to change color only when the checkbox is checked.
Without peer modifiers, you have to write JavaScript to watch the checkbox state and then manually add or remove classes on the text input. This is slow, error-prone, and makes your code messy.
Tailwind's peer modifier lets you style a sibling element based on the state of another element, like a checkbox, using only CSS classes--no JavaScript needed.
<input type="checkbox" id="agree"> <input type="text" id="name"> <script> const checkbox = document.getElementById('agree'); const input = document.getElementById('name'); checkbox.addEventListener('change', () => { if (checkbox.checked) { input.classList.add('bg-green-200'); } else { input.classList.remove('bg-green-200'); } }); </script>
<input type="checkbox" id="agree" class="peer"> <input type="text" class="peer-checked:bg-green-200">
You can create interactive, state-based styles between sibling elements easily and cleanly without any JavaScript.
On a signup form, when a user checks 'I agree to terms', the submit button can change color to show it is active, all with just CSS using peer modifiers.
Peer modifier lets one element style its sibling based on state.
It removes the need for JavaScript for simple sibling state reactions.
It keeps your code cleaner and easier to maintain.