11

In the process of converting a website using tailwind css. I have an element which on hover, animates its child element. I am struggling to see how to do this with tailwind css.

Here is the code using basic CSS

.cont:hover .hoverme{
  animation: hoverwave;
  animation-duration: .5s;
  animation-direction: normal;
  animation-fill-mode: forwards;
}
@keyframes hoverwave {
  0%{transform: scale(1,1);}
  100%{transform: scale(1.2,1.2);}
}
<!-- PROJECT1 START-->
<div class="cont overflow-hidden w-projmob mx-4 sm:w-projmob-md lg:w-projmob-xl opacity-90 cursor-pointer transition duration-300 mt-20 h-auto border-box bg-white rounded-lg shadow-lg hover:opacity-100">

////////////////

<div class="w-full h-20 absolute bottom-0 -left-5 z-20">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320" style="width: 150%;" class="hoverme"><path fill="#fff" fill-opacity="1" d="M0,32L48,53.3C96,75,192,117,288,128C384,139,480,117,576,106.7C672,96,768,96,864,117.3C960,139,1056,181,1152,186.7C1248,192,1344,160,1392,144L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>
</div>
</div>

///////////////

</div>
<!--PROJECT END-->

Whilst hovering container .cont, class .hoverme animates.

Thanks

6
  • Hi can you please include your HTML as well? Thanks
    – John
    Commented Dec 27, 2020 at 23:23
  • Added, apologies.
    – jiml123
    Commented Dec 27, 2020 at 23:27
  • I presume i may need to extend something in the tailwind.config.js... but not sure what
    – jiml123
    Commented Dec 27, 2020 at 23:32
  • Hi rounin... the code above is working perfectly. I am looking for an answer on how to code this using TAILWIND CSS.
    – jiml123
    Commented Dec 27, 2020 at 23:43
  • My apologies - I thought you were presenting the code above as an example of tailwind. I'll bow out. Sorry I couldn't help further.
    – Rounin
    Commented Dec 27, 2020 at 23:45

3 Answers 3

35

give group class to the hovered element like that

<div class="group">
  <p class="group-hover:text-gray-900">afected element</p>
</div>
9

See the documentation:

When you need to style an element based on the state of some parent element, mark the parent with the group class, and use group-* modifiers like group->hover to style the target element:

<a href="#" class="group block max-w-xs mx-auto rounded-lg p-6 bg-white ring-1 ring-slate-900/5 shadow-lg space-y-3 hover:bg-sky-500 hover:ring-sky-500">
  <div class="flex items-center space-x-3">
    <svg class="h-6 w-6 stroke-sky-500 group-hover:stroke-white" fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
    <h3 class="text-slate-900 group-hover:text-white text-sm font-semibold">New project</h3>
  </div>
  <p class="text-slate-500 group-hover:text-white text-sm">Create a new project from a variety of starting templates.</p>
</a>

This pattern works with every pseudo-class modifier, for example group-focus, group-active, or even group-odd.

0

HOVERME is different than hoverme CSS classes are case sensitive. Change either the CSS or HTML so they have the same casing.

1
  • Thanks John, but the HOVERME was added to make the class stand out above, but vie since changed it. The hover is working, but what i need to know is how to implement this using tailwind css.
    – jiml123
    Commented Dec 27, 2020 at 23:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.