html

How to Use Hover, Focus, and Active on Tailwind?

Tailwind is a well-known CSS framework that permits users to make customizable designs. It provides the ability to apply different styles based on user interactions, such as hovering, focusing, or activating an element. Hover is a pseudo-class that applies styles when the user moves the cursor over an element, focus applies when the user focuses on an element, and actively applies when the user activates an element, such as clicking or pressing a key.

This article will present the method to use hover, focus, and active on Tailwind CSS.

How to Use Hover, Active, and Focus on Tailwind CSS?

To style an element on hover, active, and focus states, use the “hover”, “active”, and “focus” modifiers in the HTML program. Then, verify the output by viewing the web page. The step-by-step procedure is mentioned below:

Step 1: Utilize Hover, Focus, and Active Modifier in the HTML Program

Create an HTML program and utilize the “hover”, “focus” and “active” modifiers to style an element on hover, focus, and active states. For instance, we have specified the following modifiers:

<div class="h-screen flex justify-center items-center">

<button class="bg-violet-500 hover:bg-blue-700 active:bg-red-700

focus:ring focus:ring-yellow-500 py-2 px-4 rounded">


Click me

</button>

</div>

Here:

  • bg-violet-500” sets the background color of the button to a shade of violet.
  • hover:bg-blue-700” changes the button’s background color to blue when the button is hovered over.
  • active:bg-red-700” changes the background color of the button to red when the button is clicked or activated.
  • focus:ring focus:ring-yellow-500” adds a yellow ring around the button when it is in focus.
  • py-2” adds padding on the y-axis (top and bottom) of the button.
  • px-4” adds padding on the x-axis (left and right) of the button.
  • rounded” rounds the corners of the button.

Step 2: Verify Output

To verify whether the hover, focus, and active states are working properly, view the HTML web page:

In the above output, the color of the button is violet. Its color changed to blue when it hovered over. While clicking, the color is changing to the red color which shows the active state. The yellow ring represents the focus state. The output indicates that the hover, focus, and active states are working properly.

Conclusion

In Tailwind CSS, utilize the “hover”, active”, and “focus” modifiers in the HTML program to style an element on hover, active and focus states. Then, view the web page to verify the output. Users can also apply different styles according to the requirement. This article has demonstrated the method of using hover, focus, and active in Tailwind.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.