Using Tailwind CSS to change the style of a parent div in response to the selected state of a radio button
P粉718165540
2023-08-15 23:49:52
<p>I have a table with multiple columns and a radio button per row. I need to change the background color of the entire row when I select a specific button. Can this be achieved? </p>
<pre class="brush:php;toolbar:false;"><tr class="flex items-center p-4 space-x-4 border hover:bg-indigo-50">
<input type="radio" name="user" id="{{ user.id }}">
<label for="{{ user.id }}">{{ user.email }}</label>
<div>{{ user.name }}</div>
</tr></pre>
<p>In Tailwind, you can use the "peer" class to style adjacent blocks. But I need to style the parent block (when the <input> is selected, the background color of the parent block should change). </p>
You can use the pseudo-class
:has()
, but its support in browsers is limited (for example: Mozilla does not support it). You can check the browser support of this selector.