DEV Community

Cover image for How to Make Parent Div Activate Styling of Child Div for Hover and Active States
Bobby Iliev
Bobby Iliev

Posted on โ€ข Originally published at devdojo.com

3 1 1 1 1

How to Make Parent Div Activate Styling of Child Div for Hover and Active States

Hey there developers! ๐Ÿ‘‹

Today, we're diving into a Tailwind CSS trick! We'll explore how to make a parent div control the styling of its child elements on hover and active states. Let's jump right in!

The Problem

You've probably encountered situations where you want an entire component to respond to user interactions, not just individual elements.

For example, you might want a card to change its appearance when hovered, including all its child elements. Tailwind CSS has a elegant solution for this: the group and group-hover utilities.

The Solution: Group and Group-Hover

Tailwind CSS provides a powerful feature called "group hover" that allows us to style child elements based on the state of a parent element. Here's how it works:

  1. Add the group class to your parent element.
  2. Use group-hover: prefix on child elements to apply styles when the parent is hovered.

Let's see this in action with a cool example:

<div class="group p-6 bg-gray-100 rounded-lg transition-all duration-300 hover:bg-blue-100">
  <h2 class="text-2xl font-bold text-gray-800 group-hover:text-blue-600">Awesome Feature</h2>
  <p class="mt-2 text-gray-600 group-hover:text-blue-500">This feature will blow your mind!</p>
  <button class="mt-4 px-4 py-2 bg-blue-500 text-white rounded group-hover:bg-blue-600">
    Learn More
  </button>
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, when you hover over the parent div, the heading, paragraph, and button all change color simultaneously. Pretty cool, right?

Taking It Further: Group-Active

But wait, there's more! We can also use the group-active variant to style child elements when the parent is in an active state (e.g., being clicked). Here's an enhanced version of our previous example:

<div class="group p-6 bg-gray-100 rounded-lg transition-all duration-300 hover:bg-blue-100 active:bg-blue-200">
  <h2 class="text-2xl font-bold text-gray-800 group-hover:text-blue-600 group-active:text-blue-800">Awesome Feature</h2>
  <p class="mt-2 text-gray-600 group-hover:text-blue-500 group-active:text-blue-700">This feature will blow your mind!</p>
  <button class="mt-4 px-4 py-2 bg-blue-500 text-white rounded group-hover:bg-blue-600 group-active:bg-blue-800">
    Learn More
  </button>
</div>
Enter fullscreen mode Exit fullscreen mode

Now, when you click on the component, you'll see an additional style change. It's like magic, but it's just the power of Tailwind CSS! ๐ŸŽฉโœจ

Pro Tip: Extending Core Plugins

It's worth noting that not every property supports group-hover or group-active out of the box. In some cases, you might need to extend Tailwind's core plugins. You can do this in your tailwind.config.js file:

module.exports = {
  // ...other config
  plugins: [
    plugin(({ addVariant }) => {
      addVariant('group-hover', ':merge(.group):hover &')
      addVariant('group-active', ':merge(.group):active &')
    })
  ],
}
Enter fullscreen mode Exit fullscreen mode

This will allow you to use group-hover and group-active with any utility in Tailwind CSS.

Wrapping Up

And there you have it! You've just learned how to make parent divs control the styling of their child elements for hover and active states using Tailwind CSS.

As a next step, check out DevDojo's Tails page builder at https://devdojo.com/tails/. It's a fantastic visual builder that lets you create stunning Tailwind CSS powered pages with ease. Give it a spin and see how it can supercharge your development workflow!

Keep coding, stay curious, and until next time, may your builds be bug-free and your coffee strong! ๐Ÿ’ปโ˜•๏ธ

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, weโ€™ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, weโ€™ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Image of Docusign

๐Ÿ› ๏ธ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more