Hover & States — Tailwind CSS

A state is what is happening to an element. A state prefix makes a class apply only in a certain state: hover: while the cursor is over the element, focus…

Point your cursor at the "Buy" button in any online shop: its colour changes a little. Click the email field in a sign-up form: its border turns blue. In this lesson you build a look that changes with what is happening, like that button and that email field.

What is happening to an element right now is called its state, for example: the cursor is pointing at it, it's picked for typing, it's being pressed, or it's switched off. In Tailwind, you add the state's name as a prefix: hover:bg-blue-600 means "use a blue-600 background, but only while the cursor is over this element". It's like a lift button: its shape never changes, but its look follows its state: normal, lit while pressed, dark when the lift is out of order. On a website, the lift button is your button: "lit while pressed" is active:, and "dark" is disabled:. State prefixes do the same for the buttons and input fields on your website.

State prefixes: classes that wait for a state

Just like md: and dark:, a state prefix only turns on the class after it when its condition is met. This is the same button as in the Lab:

<button id="btn" class="bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white px-4 py-2 rounded">
  Hover Me
</button>

From hover:bg-blue-600, Tailwind v4 makes this CSS:

@media (hover: hover) {
  .hover\:bg-blue-600:hover {
    background-color: var(--color-blue-600);
  }
}

Other states: focus, focus-visible, active, disabled

Prefix Applies when Example
hover: the cursor is over the element hover:bg-blue-600
focus: the element has focus: a field clicked for typing, or an element reached with Tab focus:border-blue-500
focus-visible: the element has focus and the browser decides the focus mark should show, usually with a keyboard focus-visible:ring-2
active: the element is being pressed active:bg-blue-700
disabled: the element has the disabled attribute disabled:opacity-50

This is the email field from the Lab:

<input type="email" placeholder="Email" class="border-2 border-gray-300 focus:border-blue-500 rounded px-3 py-2">

focus-visible: is like focus:. The difference: the browser decides when the mark shows. When you move with Tab, it shows. When a button is clicked with a mouse, it usually doesn't. It suits buttons: focus-visible:ring-2 focus-visible:ring-blue-300 draws a 2px blue-300 ring around the button.

A button with the disabled attribute can't be clicked and can't be reached with Tab. disabled:opacity-50 disabled:cursor-not-allowed fades the button and turns the cursor into a "not allowed" sign, so the user knows the button is off.

Hover on a phone

Guess first: you tap a bg-blue-500 hover:bg-blue-600 button on a phone. Does it turn blue-600?

The answer is no. A touch screen has no cursor floating over the button. The phone's browser reports that its main input can't hover, so @media (hover: hover) isn't met, and hover:bg-blue-600 is never used. Tap the button on a phone and it stays blue-500. active: still works while your finger presses the button.

In Tailwind v3, hover: wasn't limited like this, so on phones the hover colour often stayed on after a tap. v4 fixed that. Because hover: doesn't apply on phones, don't hide important information that only appears on hover: phone users will never see it.

⚠️ What gets forgotten when writing states

TL;DR: A state is what is happening to an element. A state prefix makes a class apply only in a certain state: hover: while the cursor is over the element, focus: while the element has focus (a field being typed in), focus-visible: when the focus mark should show (usually with a keyboard), active: while pressed, disabled: when the element has the disabled attribute. The normal colour is still written with no prefix, and the state version overrides it: bg-blue-500 hover:bg-blue-600. In Tailwind v4, hover: only works on devices that can hover (with a mouse or a touchpad), so it doesn't apply on phones.

⚡ The normal colour, then the hover colour

Write:

<button id="btn" class="bg-blue-500 hover:bg-blue-600 active:bg-blue-700 text-white px-4 py-2 rounded">
  Hover Me
</button>
Guess first: hover:bg-blue-600 only applies while the cursor is over the button. Remove bg-blue-500, and what colour is the button before it's hovered?

It has no background at all. A prefix like hover: only overrides the normal colour (the class with no prefix), so the normal colour still has to be written.

Meaning: hover: and active: are state prefixes (the state is what is happening to the element). hover:bg-blue-600 overrides bg-blue-500 while the cursor is over the button, and active:bg-blue-700 while it's being pressed.

In the exercise, the checker looks for 4px corners, from rounded.

⚡ Focus on a text field

Write:

<input type="email" placeholder="Email" class="border-2 border-gray-300 focus:border-blue-500 rounded px-3 py-2">

Meaning: Once the field is clicked and ready for typing (it has focus), focus:border-blue-500 turns its border blue. A focus mark matters for keyboard users, who move between elements with the Tab key.

⚡ Hover on phones

Guess first: a phone screen has no cursor. You tap a bg-blue-500 hover:bg-blue-600 button on a phone. Does it turn blue-600?

No. In Tailwind v4, hover: only works on devices that can hover. On a phone it stays blue-500, but active: still works while your finger presses it.

Meaning: Don't hide important information that only shows on hover, because phone users will never see it.

What you will learn