Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use tailwindcss to change the color of each element when hovering over descendant elements

I want to change the text color when I hover each a tag, but the text color changes when I hover the ul tag.

I would like to know how the text color changes when I hover over each a tag.

<script src="https://cdn.tailwindcss.com"></script>

<ul class="flex gap-3 [&_a]:block [&_a]:text-blue-500 [&_a]:hover:text-red-500">
  <li><a href="https://www.example.com/">foo</a></li>
  <li><a href="https://www.example.com/">bar</a></li>
  <li><a href="https://www.example.com/">baz</a></li>
</ul>

<div class="bg-orange-100 border-l-4 border-orange-500 text-orange-700 p-4 mt-10 mb-3">
  <p class="font-bold">Please</p>
  <p>I would like to use a method other than the one below.</p>
</div>
<ul class="flex gap-3 [&_a]:block [&_a]:text-blue-500">
  <li><a href="https://www.example.com/" class="hover:text-red-500">foo</a></li>
  <li><a href="https://www.example.com/" class="hover:text-red-500">bar</a></li>
  <li><a href="https://www.example.com/" class="hover:text-red-500">baz</a></li>
</ul>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Put :hover inside the arbitrary variant (replace [&_a]:hover:text-red-500 with [&_a:hover]:text-red-500).

<script src="https://cdn.tailwindcss.com"></script>
<ul class="flex gap-3 [&_a]:block [&_a]:text-blue-500 [&_a:hover]:text-red-500">
  <li><a href="https://www.example.com/">foo</a></li>
  <li><a href="https://www.example.com/">bar</a></li>
  <li><a href="https://www.example.com/">baz</a></li>
</ul>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading