Sub topic 2
Pressed and Disabled States
Assume basic HTML, CSS pseudo-classes, and Tailwind utility classes. Tailwind variants are prefixes that apply a utility only when a condition is true. For example, active:bg-indigo-800 applies the background utility while a button is being pressed.
Pressed and disabled states
Use active: for a temporary pressed state. Use disabled: for controls that cannot currently be used. Disabled buttons should also communicate their state through opacity and the cursor.
<button
type="button"
class="rounded bg-indigo-600 px-4 py-2 text-white
active:bg-indigo-800
disabled:cursor-not-allowed disabled:opacity-50"
disabled
>
Save
</button>The disabled: utilities apply only when the button has the Boolean disabled attribute. The active: variant applies while the pointer or keyboard activation is in progress.
Making state changes smooth
State changes can feel abrupt when a color changes immediately. Add transition-colors to animate color properties. Use a narrow transition utility when you know which property changes.
<a
href="/guide"
class="text-indigo-600 transition-colors hover:text-indigo-800"
>
Read the guide
</a>The transition changes how the browser displays the change. It does not change which state is active.
Keeping keyboard focus visible
Keyboard users need a visible focus indicator. Prefer focus-visible: for an indicator shown when focus comes from keyboard navigation. Do not remove the browser outline without replacing it with an equally clear style.
<button
type="button"
class="rounded bg-indigo-600 px-4 py-2 text-white
focus-visible:outline-2 focus-visible:outline-offset-2
focus-visible:outline-indigo-600"
>
Continue
</button>focus: responds to every focus method. focus-visible: usually avoids showing the indicator after a mouse click while preserving it for keyboard users.
Styling children from a parent with `group`
Add group to a parent, then use group-hover: or group-focus-within: on a child. This lets a child respond to the parent or its contents.
<a
href="/guide"
class="group block rounded-lg border border-slate-200 p-4
transition-colors hover:bg-slate-50
focus-visible:outline-2 focus-visible:outline-indigo-600"
>
<h2 class="font-semibold text-slate-900 group-hover:text-indigo-600">
Read the guide
</h2>
<span class="inline-block transition-transform group-hover:translate-x-1">
→
</span>
</a>Exercise
Which variant should you use for a button that changes while pressed, and which variant should mark it unavailable?
Show answer
Use active: for the pressed state and disabled: for the unavailable state.