How to use hover, focus and active variants in Tailwind CSS ?
Last Updated :
24 Apr, 2025
In this article, we will see how to use hover, focus, and active variants in Tailwind CSS. Tailwind CSS uses the Hover, Focus, and Active variants to style an element when the user mouses move over it, focuses it, or actively clicks/tapped it. These variants allow you to create interactive and dynamic user interfaces without writing custom CSS. These variants are very useful for creating interactive and dynamic user interfaces. You can create hover effects, focus styles, and active styles without writing custom CSS.
Why use hover, focus, and active variants in a tailwind?
The hover, focus, and active variants of Tailwind CSS help you add interactivity to your designs without writing a lot of additional CSS. By using these flavors in conjunction with utility classes, you can easily change the style of elements based on user interaction. By hovering over an element, focusing an input, or actively clicking a button. This can make your theme more dynamic and appealing to your users.
In addition, these variants let you group styles for different states of an element, making your CSS cleaner and easier to maintain. This will make future CSS easier to understand and edit. Additionally, these variants can improve the user experience by providing visual feedback to the user.
For example, when the mouse is moved or pressed over a button, you can change its background color to indicate that it is interactive and clickable. Alternatively, when an input has focus, you can change the border color to indicate that the input is ready.
Hover variant: The hover variant is used to style an element when the user hovers the mouse pointer over it. For example, you can use the hover:bg-red-500 class to change the background color of an element to red when the user hovers the mouse over it.
Syntax:
hover: {property}
Example 1: In this example, we will see how to use the hover variant.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< meta name = "viewport"
content = "width=device-width, initial-scale=1.0" >
< title >Welcome to GFG</ title >
< link rel = "stylesheet" href = "style.css" >
< link href = "/dist/output.css" rel = "stylesheet" >
</ head >
< body >
< div class="flex justify-center
items-center min-h-screen">
< div >
< button class="bg-green-600 p-4
rounded-md hover:bg-green-700">
Hello Geek!
</ button >
</ div >
</ div >
</ body >
</ html >
|
CSS
@tailwind base;
@tailwind components;
@tailwind utilities;
|
Output: When you hover over the button the color of the button changes as shown in the image:
Focus Variant: Focus variants are typically used to style an element when it has focus when the user clicks or tabs to it using the tab he key. For example, you can use the focus:outline-none class to remove the outline of the focused element.
Syntax:
focus:{property}
Example 2: In this example, we will see how to use the focus variant.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< meta name = "viewport"
content = "width=device-width, initial-scale=1.0" >
< title >Welcome to GFG</ title >
< link rel = "stylesheet" href = "style.css" >
</ head >
< body >
< div class="flex justify-center
items-center min-h-screen">
< div >
< button class="bg-green-600 p-4
rounded-md focus:bg-green-700">
Hello Geek!
</ button >
</ div >
</ div >
</ body >
</ html >
|
CSS
@tailwind base;
@tailwind components;
@tailwind utilities;
|
Output:
Active Variant: Active variants are used to style elements when the user actively clicks or taps them.
Syntax:
active:{property}
Example 3: In this example, it is showing you how to use the active variant.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< meta name = "viewport"
content = "width=device-width, initial-scale=1.0" >
< title >Welcome to GFG</ title >
< link rel = "stylesheet" href = "style.css" >
</ head >
< body >
< div class="flex justify-center
items-center min-h-screen">
< div >
< button class="bg-green-600 p-4
rounded-md active:bg-green-700">
Hello Geek!
</ button >
</ div >
</ div >
</ body >
</ html >
|
CSS
@tailwind base;
@tailwind components;
@tailwind utilities;
|
Output: When you click over the button the color of the button changes as shown in the image:
In summary, using the hover, focus, and active variants in Tailwind CSS can be an effective way to add interactivity and visual feedback to your designs. Elements can be styled based on user interaction. By hovering over an element, focusing an input, or actively clicking a button. This allows you to create a more dynamic and engaging user experience.
Similar Reads
How to Create Group Hover on Text using Tailwind CSS?
Creating a group hover effect on text with Tailwind CSS involves styling text to change its appearance when hovering over a parent container. This method allows multiple elements to respond to a single hover action, enhancing interactivity and creating cohesive UI designs. Using utility classes in T
2 min read
How to Change Image on Hover using Tailwind CSS?
One common effect is changing an image when the user hovers over it. We use Tailwind CSS, a utility-first CSS framework, to accomplish this without any additional JavaScript logic for the hover effect. By utilizing Tailwind's built-in classes we can create smooth transitions between two images where
2 min read
How to handle Hover and Focus States in Tailwind CSS ?
Handling hover and focus states in Tailwind CSS involves using utility classes that specifically target these states. The hover: prefix is used for hover states, and the focus: prefix is used for focus states. Syntax// For Hover state<button class="bg-blue-500 hover:bg-blue-700"> Click me</
2 min read
How to Modify Hover Effect using Tailwind CSS ?
In Tailwind CSS, the term "modify hover" refers to changing the styles that are applied to an element when it is hovered over. With Tailwind CSS "hover" variation, you can quickly apply particular utility classes and custom classes to control how components appear when you hover over them, giving yo
3 min read
How to change the width on hover using Tailwind CSS ?
In this article, we will change the width on hover using Tailwind. There is no inbuilt method in Tailwind, so you have to customize the tailwind.config.js file. Let's discuss the whole process further in this article. By default, tailwind CSS only generates responsive variants for width utilities. T
3 min read
How to use Google Fonts in Tailwind CSS?
Google Fonts in Tailwind CSS enhances webpage design with free and easy-to-use fonts. It provides a library of 1000+ fonts served by Google servers. Add the Google Fonts API link to your HTML file's <head>.Configure tailwind.config.js to include the font under fontFamily.Tailwind comes with th
4 min read
How to use CSS Animations with Tailwind CSS ?
Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility Class
3 min read
How to use ::before and ::after elements in Tailwind CSS ?
In Tailwind CSS, we can use the ' :: before ' and ' :: after ' pseudo-elements by adding utility classes directly in your HTML. Simply apply the "before:" or "after:" prefix followed by the desired utility classes to style elements before or after their content. Using Utility ClassesTailwind CSS off
1 min read
How to write a:hover in inline CSS?
The :hover pseudo-selector in CSS enables you to modify the style of elements when a user hovers their mouse over them. This selector is widely used to improve user experience by adding visual feedback on elements like buttons, links, images, or any interactive items on a webpage. For the :hover eff
2 min read
How to use calc() in Tailwind CSS?
The calc() function in CSS allows you to perform calculations for property values dynamically. While Tailwind CSS doesnât directly support calc(), you can use it inline or with custom utilities. 1. Using Inline StylesYou can use calc() directly within the style attribute for dynamic property values.
2 min read