Sub topic 5
Pressed and disabled states
This chapter covers the active: and disabled: variants. It assumes you know basic HTML and CSS, including pseudo-classes such as :hover and :focus. It also assumes you know common Tailwind utilities (colors, padding, rounded corners) and have a project where Tailwind is already set up. You should also know how a variant prefix works, as covered in the earlier chapters on hover: and focus:.
What the two variants map to
Tailwind's active: and disabled: variants are thin wrappers around CSS pseudo-classes.
| Variant | CSS pseudo-class | Matches when |
|---|---|---|
| `active:` | `:active` | The element is being pressed, for example while a mouse button is held down on it |
| `disabled:` | `:disabled` | A form control has the `disabled` attribute |
Neither variant does anything special on its own. active:bg-indigo-800 generates a rule that sets the background color only while the element matches :active. When the element stops matching, the browser returns to the other rules that apply.
Styling the pressed state
A pressed state tells the user that the click registered. It is usually a small change: a darker background, a slight shrink, or a reduced shadow.
<button class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 active:bg-indigo-800">
Save
</button>The button gets progressively darker: bg-indigo-600 normally, bg-indigo-700 on hover, and bg-indigo-800 while pressed. Pressing a button also means the pointer is hovering over it, so both :hover and :active match at the same moment. The two rules then compete, and the result depends on which one the browser considers more important.
Why `active:` wins over `hover:`
Tailwind orders its generated CSS so that variants appear in a fixed sequence. active: is generated after hover: and focus:. When two rules have the same CSS specificity, the later one wins. This means active:bg-indigo-800 overrides hover:bg-indigo-700 while the button is pressed, which is the behavior you want.
You do not have to manage this order yourself. You only need to know that a pressed element shows the active: value even though the hover: rule also matches.
Pressed feedback beyond color
Color is not the only option. Any utility can take the active: prefix.
<button class="rounded bg-indigo-600 px-4 py-2 text-white shadow-md transition hover:bg-indigo-700 active:scale-95 active:shadow-none">
Submit
</button>Here active:scale-95 shrinks the button to 95% of its size while pressed, and active:shadow-none removes the shadow so the button looks pushed into the page. The transition utility, covered in detail in a later chapter, makes both changes animate instead of snapping.
Where `:active` applies
:active matches more than buttons. Links, labels, and other interactive elements match it while pressed. A few details are worth knowing:
- On a keyboard, pressing Space on a focused button also triggers
:activein most browsers. Pressing Enter usually does not hold the state for long. - On touch screens, the pressed state may appear only briefly, or in some older mobile browsers not at all unless the page has a touch listener. Do not rely on it as the only signal that an action happened.
- If the user drags the pointer off the element while still holding the button down, the state ends in most browsers.
Styling disabled elements
A disabled control is one the user cannot interact with. In HTML, the disabled attribute applies to button, input, select, textarea, fieldset, and a few others. The browser then blocks clicks and keyboard input, removes the element from the tab order, and excludes it from form submission.
The disabled: variant lets you make that state visible.
<button
disabled
class="rounded bg-indigo-600 px-4 py-2 text-white disabled:cursor-not-allowed disabled:opacity-50"
>
Save
</button>This button is dimmed with disabled:opacity-50. The cursor changes to a "not allowed" icon with disabled:cursor-not-allowed.
Disabled does not cancel hover and active
The browser still reports :hover over a disabled button. A disabled button with hover:bg-indigo-700 will still change color under the pointer, which suggests it is clickable when it is not.
There are two common fixes. The first is to give the disabled: rule its own color, so the dimmed look stays consistent. The second is to use Tailwind's enabled: variant, which matches :enabled, and attach the interactive styles to it.
<button
disabled
class="rounded bg-indigo-600 px-4 py-2 text-white enabled:hover:bg-indigo-700 enabled:active:bg-indigo-800 disabled:cursor-not-allowed disabled:opacity-50"
>
Save
</button>With enabled:hover: the hover color applies only when the button is not disabled. This is also an example of stacking variants, which a later chapter covers fully. The variants apply from left to right, so enabled:hover:bg-indigo-700 means "when enabled and hovered".
Another option is pointer-events-none on the disabled state. Use it with care, because it also stops the cursor-not-allowed icon from showing, since the element no longer receives pointer events.
Disabled links and non-form elements
The disabled attribute has no effect on an <a> element, and :disabled does not match it. A link cannot be disabled with the attribute alone. If you need a disabled-looking link, use the aria-disabled attribute and match it with the aria-disabled: variant.
<a
href="/checkout"
aria-disabled="true"
tabindex="-1"
class="rounded bg-indigo-600 px-4 py-2 text-white aria-disabled:cursor-not-allowed aria-disabled:opacity-50 aria-disabled:pointer-events-none"
>
Checkout
</a>The aria-disabled attribute only informs assistive technology. It does not stop the click. In this example pointer-events-none blocks mouse clicks and tabindex="-1" removes the link from the keyboard tab order. Keyboard users who reach the link by other means could still activate it, so for anything important, also guard the action in JavaScript or on the server.
Disabled as a parent state
Disabling a fieldset disables all form controls inside it, and each control then matches :disabled. Tailwind's disabled: variant therefore works on children of a disabled fieldset without any extra markup. A plain div with a disabled attribute does not do this, because :disabled only applies to elements that support the attribute.
Putting it all together
The combined button below uses the interactive states from this and the previous chapters. It matches the first code example in the plan: hover, keyboard focus, pressed, and disabled, plus a smooth color change.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 active:bg-indigo-800 disabled:cursor-not-allowed disabled:opacity-50">
Save
</button>The states layer like this:
In the diagram, Hovered is styled by hover:, Pressed by active:, and Disabled by disabled:. Focus is a separate axis, since a button can be focused while in any of the other states.
Common errors
- Disabled button still reacts to hover. The
hover:rule matches regardless of thedisabledattribute. Useenabled:hover:or override the color in adisabled:rule. disabled:has no effect on a link ordiv. The pseudo-class only matches elements that support thedisabledattribute. Usearia-disabled:or adata-*variant for other elements.- Pressed state is invisible because of a missing transition. The state may last only a few milliseconds on a quick click. Pair
active:with a small, noticeable change such asactive:scale-95. - Using
disabled:opacity-50as the only cue. Low-contrast text on a dimmed background may be hard to read, and color alone is not enough for everyone. Keep the label legible and consider a text or icon cue where the reason matters. - Removing the disabled button from the page flow of an explanation. A disabled button gives no feedback about why it is disabled. Where possible, show a short message nearby explaining what the user needs to do.
Exercises
Exercise
A button has the classes bg-blue-600 hover:bg-blue-700 active:bg-blue-800 disabled:opacity-50 and the disabled attribute. A user moves the mouse over it. What does the user see, and how would you stop the hover color from appearing?
Show answer
The button is dimmed to 50% opacity by disabled:opacity-50, but its background still changes to bg-blue-700 because :hover matches a disabled button. To stop this, change hover:bg-blue-700 to enabled:hover:bg-blue-700 (and likewise enabled:active:bg-blue-800), so the interactive colors apply only when the button is enabled.
Exercise
You want an <a> styled as a button to look and behave as disabled. Why does disabled:opacity-50 not work, and which variant and attributes would you use instead?
Show answer
The :disabled pseudo-class only matches elements that support the disabled attribute, such as buttons and form controls, and a link is not one of them. Add aria-disabled="true" and style it with aria-disabled:opacity-50 aria-disabled:pointer-events-none. Add tabindex="-1" to remove it from the tab order, and guard the action in JavaScript or on the server, since aria-disabled does not block activation.
Exercise
Write the classes for a button that shrinks to 95% of its size and loses its shadow while pressed, with the change animated.
Show answer
shadow-md transition active:scale-95 active:shadow-none. The active: prefix limits the scale and shadow changes to the pressed state, and transition animates them.