Combining State and Responsive Variants
What you'll learn: How to stack Tailwind's state prefixes (like hover:) and breakpoint prefixes (like md:) together for precise, conditional styling that responds to both screen size and user interaction.
The Power of Prefix Stacking
In Tailwind, you've learned that you can use breakpoint prefixes (md:, lg:) for responsive design and state prefixes (hover:, focus:) for interactivity. But here's where it gets powerful: you can combine them.
When you stack these prefixes together, you're creating a compound condition: "Only apply this style at this breakpoint AND in this state."
The Syntax
The order matters: breakpoint first, then state.
<button class="bg-blue-400 md:hover:bg-blue-500">
Click me
</button>
This reads as: "On medium screens and larger, when hovering, make the background blue-500."
Real-World Example
<nav class="text-gray-700
hover:text-blue-500
md:hover:text-purple-500
lg:focus:underline">
Link
</nav>
- Mobile: Gray text, turns blue on hover
- Medium screens: Gray text, turns purple on hover
- Large screens: Gray text, purple on hover, and underlines on focus
Why This Matters
Different screen sizes often need different interaction patterns. A subtle hover effect on mobile might need to be more pronounced on desktop where hover is more common. This syntax lets you fine-tune interactions per breakpoint without writing custom CSS.
Key Takeaway: Stack breakpoint and state prefixes (like md:hover:, lg:focus:) to create conditional styles that respond to both screen size and user interaction simultaneously.