<div id="card" class="group">
<p class="text-blue-400 group-hover:text-red-400">
This is sample text to fill the space.
</p>
</div>
For more details, check out: Tailwind CSS Handling Hover
Latest Update
Note that Tailwind 3.1+ is necessary for using inline media queries
You have three options:
1. Allow future flag
Starting from version 4, the desired behavior will be default, but you can enable it in advance:
module.exports = {
future: {
hoverOnlyWhenSupported: true,
},
}
2. Inline
This method is a bit tricky as inline media query with whitespace cannot be used, so you might need to resort to group-hover (since
[@media(hover:hover){.group:hover}]:text-red-400
will not work in all cases); version 3.1+ required:
<div id="card" class="group">
<p class="text-blue-400 group-hover:[@media(hover:hover)]:text-red-400">
This is sample text to fill the space.
</p>
</div>
3. Theme Extend
This may not be the optimal solution, as selecting the parent of an element is not possible, but in some scenarios, it could be effective) - this approach is not highly recommended
module.exports = {
theme: {
extend: {
screens: {
'mygroup-hover': { 'raw': '(hover: hover) {.group :hover}' },
},
},
}
}
<div id="card" class="group">
<p class="text-blue-400 mygroup-hover:text-red-400">
This is sample text to fill the space.
</p>
</div>