Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Adding Hover and Focus Effects | Advanced Tailwind and Interactive UI
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Tailwind CSS Styling in React Applications

bookAdding Hover and Focus Effects

When you want your React components to feel responsive and interactive, providing visual feedback for user actions is essential. Tailwind CSS offers a set of state-based utility modifiers such as hover:, focus:, and active: that allow you to style elements based on their interaction state. These modifiers are prepended to any utility class and apply the style only when the element is in the corresponding state.

  • The hover: modifier applies styles when the user points to the element with a mouse or similar device;
  • The focus: modifier is triggered when the element, such as a button or input, receives keyboard focus;
  • The active: modifier applies styles while the element is being pressed or clicked.

By combining these modifiers with transition and animation utilities, you can create smooth, engaging feedback for your users. For instance, you might want a button to change its background color on hover, add a ring on focus, and scale slightly when pressed. This approach keeps your UI consistent and accessible, since keyboard users also benefit from focus styles.

Consider a simple Button component in React. You can enhance its interactivity and visual appeal by using Tailwind's state-based utilities directly in the className attribute. Here is how you might style a button to react to user interactions:

<button
  className="
    px-4 py-2 bg-blue-600 text-white rounded 
    transition duration-200 
    hover:bg-blue-700 
    focus:outline-none focus:ring-2 focus:ring-blue-400 
    active:scale-95
  "
>
  Click Me
</button>

In this example:

  • hover:bg-blue-700 darkens the button when hovered;
  • focus:outline-none focus:ring-2 focus:ring-blue-400 removes the default outline and adds a blue ring when focused via keyboard;
  • active:scale-95 slightly shrinks the button when it is pressed;
  • transition duration-200 ensures that these state changes animate smoothly.

This pattern lets you deliver a polished, interactive experience while keeping your code concise and readable.

question mark

Which Tailwind CSS modifier would you use to style a button when it is hovered by the mouse?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookAdding Hover and Focus Effects

Deslize para mostrar o menu

When you want your React components to feel responsive and interactive, providing visual feedback for user actions is essential. Tailwind CSS offers a set of state-based utility modifiers such as hover:, focus:, and active: that allow you to style elements based on their interaction state. These modifiers are prepended to any utility class and apply the style only when the element is in the corresponding state.

  • The hover: modifier applies styles when the user points to the element with a mouse or similar device;
  • The focus: modifier is triggered when the element, such as a button or input, receives keyboard focus;
  • The active: modifier applies styles while the element is being pressed or clicked.

By combining these modifiers with transition and animation utilities, you can create smooth, engaging feedback for your users. For instance, you might want a button to change its background color on hover, add a ring on focus, and scale slightly when pressed. This approach keeps your UI consistent and accessible, since keyboard users also benefit from focus styles.

Consider a simple Button component in React. You can enhance its interactivity and visual appeal by using Tailwind's state-based utilities directly in the className attribute. Here is how you might style a button to react to user interactions:

<button
  className="
    px-4 py-2 bg-blue-600 text-white rounded 
    transition duration-200 
    hover:bg-blue-700 
    focus:outline-none focus:ring-2 focus:ring-blue-400 
    active:scale-95
  "
>
  Click Me
</button>

In this example:

  • hover:bg-blue-700 darkens the button when hovered;
  • focus:outline-none focus:ring-2 focus:ring-blue-400 removes the default outline and adds a blue ring when focused via keyboard;
  • active:scale-95 slightly shrinks the button when it is pressed;
  • transition duration-200 ensures that these state changes animate smoothly.

This pattern lets you deliver a polished, interactive experience while keeping your code concise and readable.

question mark

Which Tailwind CSS modifier would you use to style a button when it is hovered by the mouse?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
some-alt