Tailwind group hover, the state you missed

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
I didn't even know this much about Tailwind π This is awesome and thanks for the share. Bookmarked
I didn't know about group hover, I was adding hover to multiple divπ . Thanks. Will use this
Awesome, i'm glad you learned something new Vaishav! β€οΈ
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Hover states are an essential part of web development and making those final UI tweaks.
If you worked with Tailwind, you might know the hover states, which are amazing, but they never included sub-selectors.
This is something the group-hover class can help us with!
You can see the end result of what we'll be building today in the CodePen below.
Let's first take a look at how this effect works.
It needs to have a group class on the parent element.
Then we can add hover classes to children of this group element by using the group-hover: class.
Let's try and make a super simple effect, where we have a div with two spans inside. On hover, each span will get its own color.
<div class="group">
<span class="group-hover:text-yellow-500 text-black">Hello</span>
<span class="group-hover:text-red-500 text-black">World</span>
</div>
Try it out in the following CodePen demo:
Let's look at how we can create the card hover effect like the top demo shows.
This uses the Tailwind square div effect, as seen in the article about Tailwind CSS responsive square divs.
But let's sketch the high-level overview of what we'll be needing:
These two child elements are only visible once we hover over the parent div.
In the basic structure, it will look like this:
<div
class="relative group flex justify-center w-1/2 h-0 bg-center bg-cover border-2 border-gray-300 shadow-lg pb-1-2 rounded-xl bg-image"
>
<div
class="absolute bg-black w-full h-full rounded-xl bg-opacity-25 opacity-0 group-hover:opacity-100"
></div>
<div
class="absolute p-6 text-2xl tracking-widest text-white uppercase -translate-y-1/2 bg-black bg-opacity-75 opacity-0 top-1/2 rounded-xl group-hover:opacity-100"
>
Tadaa π
</div>
</div>
This will give us the desired effect and style of the elements, so everything will line up nicely.
However, at this point, we get a pretty static flicker effect. Let's change that by adding the following two classes to our child elements: transform transition-opacity.
These will make sure we animate the opacity transition.
You might have spotted that the black background div is an absolute positioned element, using the full width and height. We use a center hack for the text layer since it's not the full size of the div. This hack means we place the element 50% from the top and then offset the y-axis by -50%, making sure the element is perfectly centered.
I hope you learned something new, as this group-hover is just a fantastic addition to Tailwind π.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter