Sub topic 5
Pressed and disabled states
This chapter assumes you know basic HTML and CSS, including the :active and :disabled pseudo-classes. It also assumes you have used Tailwind utilities for colors and padding, and that Tailwind is already set up in your project. You should also know the hover: prefix from the previous chapters.
What the two variants map to
Tailwind's active: variant applies a utility while an element is being pressed. This means the mouse button is down on it, or a finger is touching it. It compiles to the CSS :active pseudo-class.
The disabled: variant applies a utility when a form control has the disabled attribute. It compiles to :disabled.
Both work like hover:. You put the prefix in front of any utility, and the utility applies only in that state.
<button class="bg-indigo-600 text-white active:bg-indigo-800 disabled:opacity-50">
Save
</button>Pressed feedback with active
A pressed state tells the user that the click registered. The usual choice is a color slightly darker than the hover color.
<button class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 active:bg-indigo-800">
Save
</button>The three background classes differ only by shade. Tailwind sorts its generated CSS so that active: rules come after hover: rules. When the pointer is over the button and pressed, both selectors match, and the active: color wins.
You can also change size or position to make the button feel physical:
<button class="rounded bg-slate-900 px-4 py-2 text-white transition-transform active:scale-95">
Press me
</button>Disabled controls
A disabled control cannot be clicked or focused. Style it so users can see that it is unavailable, and so the pointer gives the same message.
<button
disabled
class="rounded bg-indigo-600 px-4 py-2 text-white disabled:cursor-not-allowed disabled:opacity-50"
>
Save
</button>Here disabled:opacity-50 fades the button and disabled:cursor-not-allowed shows the "not allowed" cursor.
Browsers do not send click events from disabled buttons, but your hover: and active: classes would still change the color unless you guard against it. A disabled button can look like it responds to the pointer. Avoid this by adding a disabled: utility that overrides the color.
<button
disabled
class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 disabled:bg-slate-300 disabled:text-slate-500 disabled:hover:bg-slate-300"
>
Save
</button>disabled:hover:bg-slate-300 stacks two variants. It applies only when the element is disabled and hovered, and it restores the gray color.
Common errors
- Disabling only with a class. Applying
opacity-50andcursor-not-allowedwithout thedisabledattribute makes the button look unavailable but still clickable and focusable. Thedisabled:variant only fires when the real attribute is present. - Using
disabled:on a link. Thedisabledattribute is not valid on<a>, so:disablednever matches. Usearia-disabled="true"with thearia-disabled:variant, and remove or ignore thehrefbehavior in your script. - Relying on color alone. Reduced opacity can fail contrast checks, but disabled controls are exempt from contrast requirements. Still, keep the label readable where you can.
- Forgetting touch devices. A tap triggers
:activebriefly, so pressed feedback works on phones even where hover does not.
Putting it together
The next diagram shows which state utilities are active in each situation for the button above.
Exercise
A button has the classes bg-indigo-600 hover:bg-indigo-700 disabled:opacity-50 and the disabled attribute. A user moves the mouse over it. What color does the button show, and which class would you add so it stays the original color while disabled?
Show answer
It shows the hover color, bg-indigo-700, at 50% opacity, because hover: still matches on a disabled button. Add disabled:hover:bg-indigo-600 so the hover color is overridden while the button is disabled.
Exercise
Why does disabled:opacity-50 have no effect on <a href="/save" class="disabled:opacity-50">Save</a>, and what variant should you use instead?
Show answer
The disabled attribute is not valid on links, so the :disabled pseudo-class never matches. Set aria-disabled="true" on the link and use the aria-disabled: variant, for example aria-disabled:opacity-50.