📖 Scenario: You are creating a simple interactive card for a website. When the user moves their mouse over the card, a hidden description inside the card should appear. This effect is done by making the parent container detect the hover and then change the child's style.
🎯 Goal: Build a card using Tailwind CSS where hovering over the card container changes the text color of a child description from transparent to visible.
📋 What You'll Learn
Use a div with the class group as the card container
Inside the card container, add a title and a description
The description text should be initially invisible using text-transparent
When hovering over the card container, the description text color changes to visible using group-hover:text-black
Use Tailwind CSS utility classes only, no custom CSS
💡 Why This Matters
🌍 Real World
Interactive cards like this are common on websites to show extra information only when users hover, keeping the design clean and engaging.
💼 Career
Understanding group-hover in Tailwind helps you build interactive UI components efficiently, a valuable skill for frontend web development jobs.
Progress0 / 4 steps
1
Create the card container with a title and description
Create a div with the class group. Inside it, add an h2 with the text "Welcome to the Card" and a p with the text "This is the description.".
Tailwind
Hint
Remember to add the class group to the outer div so you can use group-hover later.
2
Make the description text initially invisible
Add the Tailwind class text-transparent to the p element to make the description text invisible initially.
Tailwind
Hint
Use the class text-transparent on the paragraph to hide the text initially.
3
Add the group-hover effect to show the description text
Add the Tailwind class group-hover:text-black to the p element so that when the parent div is hovered, the description text becomes black and visible.
Tailwind
Hint
Use group-hover:text-black on the paragraph to make the text visible on hover.
4
Add styling for padding and border to the card container
Add the Tailwind classes p-6 and border to the div with class group to give the card padding and a border.
Tailwind
Hint
Use p-6 for padding and border for a border on the card container.
Practice
(1/5)
1. What does the group-hover utility in Tailwind CSS do?
easy
A. It applies hover styles only to sibling elements.
B. It changes the background color of the hovered element only.
C. It disables hover effects on child elements.
D. It allows a parent element to change the style of its child elements when hovered.
Solution
Step 1: Understand the role of group and group-hover
The group class is added to a parent element to enable hover state tracking.
Step 2: Effect of group-hover on child elements
When the parent with group is hovered, child elements with group-hover: classes change style accordingly.
Final Answer:
It allows a parent element to change the style of its child elements when hovered. -> Option D
A. The child should use 'hover:text-green-600' instead of 'group-hover:text-green-600'.
B. The parent is missing the 'group' class; 'group-hover' on parent is invalid.
C. The parent needs 'hover' class to trigger hover effects.
D. The child element must be a button for group-hover to work.
Solution
Step 1: Check the parent's class
The parent has group-hover class, which is incorrect; it should have group.
Step 2: Understand group-hover usage
group-hover: is a prefix for child classes, not a class for parents. Parent must have group to enable this.
Final Answer:
The parent is missing the 'group' class; 'group-hover' on parent is invalid. -> Option B
Quick Check:
Parent needs 'group', not 'group-hover' class [OK]
Hint: Parent must have 'group', not 'group-hover' class [OK]
Common Mistakes:
Adding 'group-hover' class to parent instead of 'group'
Using 'group-hover:' on parent element
Confusing 'hover:' and 'group-hover:' prefixes
5. You want a card component where hovering the card changes the border color of a nested button and also shows a hidden icon inside the button. Which Tailwind setup correctly uses group-hover to achieve this?