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>
bg-blue-500has no prefix: the normal colour, always on.hover:bg-blue-600overrides it with a darker blue while the cursor is over the button. When the cursor leaves, the colour returns to blue-500.active:bg-blue-700is darker still, while the button is being pressed.text-white px-4 py-2 rounded: white text, 16px of padding left and right and 8px top and bottom, 4px corners. These classes have no prefix, so they are the same in every state.
From hover:bg-blue-600, Tailwind v4 makes this CSS:
@media (hover: hover) {
.hover\:bg-blue-600:hover {
background-color: var(--color-blue-600);
}
}
:hoveris a CSS pseudo-class (a state written with a colon in a selector): its rule applies while the cursor is over the element.@media (hover: hover)limits it to devices that can actually hover, like a laptop with a mouse or a touchpad.
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">
- Normally its border is grey (
border-gray-300). As soon as the field is clicked and ready for typing,focus:border-blue-500turns it blue. Click outside the field and it returns to grey. - A focus mark like this blue border matters to keyboard users: they move between elements with the Tab key, and need to know where they are.
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
- Writing only the hover version.
hover:bg-blue-600withoutbg-blue-500leaves the button with no background until it's hovered. A state prefix only overrides the normal colour, so the normal colour still has to be written. - Removing the focus mark with nothing in its place.
focus:outline-noneon its own leaves keyboard users lost. If you remove the browser's outline (the line around an element that has focus), replace it with something visible, likefocus-visible:ring-2. In v4, remove the outline withoutline-hidden, notoutline-none, becauseoutline-hiddenstill shows an outline in Windows high contrast mode. - Important information that only shows on hover. A tooltip (a small box of extra text) or a delete button that only appears on hover can't be reached on a phone.
- The wrong corner size in the exercise. The exercise checker looks for a
4pxborder-radius, fromrounded.rounded-md(6px) orrounded-lg(8px) won't pass.
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.