Sub topic 11
Disabled and loading states
This chapter assumes you know basic HTML, CSS selectors and pseudo-classes, and how to inspect an element in your browser's developer tools. It builds on the interaction states you have already styled: hover, active and focus. Disabled and loading are two more states that change what a button can do, so they need clear visual cues too.
Why these states need care
A disabled button says "not available right now." A loading button says "working, please wait." Both are a promise to the user. If the cue is weak, people click and nothing happens. If the cue is too strong, people cannot find out why the button is unavailable.
The two states have different jobs:
| State | Meaning | Should it accept clicks? | Should it keep focus? |
|---|---|---|---|
| Disabled | The action is not available yet | No | Depends on the method (see below) |
| Loading | The action started and is still running | No, to prevent duplicates | Yes, so keyboard users do not lose their place |
The `disabled` attribute and its cost
The HTML disabled attribute is the simplest option. The browser grays out the control, blocks clicks, and removes it from the tab order. That last effect is the problem. A keyboard or screen reader user may never discover the button, and nothing tells them why it is unavailable. Browsers also apply their own muted colors, which often fall below the contrast you set up in earlier chapters. WCAG exempts disabled controls from the contrast requirement, but very faint text is still hard to read.
Using `aria-disabled` instead
The aria-disabled="true" attribute announces the unavailable state to assistive technology but leaves the button focusable. You must then block the action yourself, because the browser will not.
<form id="signup-form">
<button type="submit" id="submit" aria-disabled="true">Create account</button>
</form>
<style>
[aria-disabled="true"] {
opacity: 0.6;
cursor: not-allowed;
}
</style>
<script>
document.getElementById('signup-form').addEventListener('submit', (event) => {
const button = document.getElementById('submit');
if (button.getAttribute('aria-disabled') === 'true') {
event.preventDefault();
}
});
</script>The CSS attribute selector [aria-disabled="true"] styles the button from the same attribute that assistive technology reads. That keeps the visual state and the announced state in sync. The script cancels the form submission while the attribute is "true".
Make the reason visible
A faded button without an explanation leaves people guessing. Place a short message near the button, such as "Enter your email to continue," and link it to the button with aria-describedby. A screen reader then reads the reason when the button receives focus. Sighted users get the same information from the text on the page.
Styling a loading state
A loading button should look busy, show that it is still the same control, and ignore further clicks. Keep the label, or replace it with a progress label such as "Saving...", so the button does not change width and jump around.
.btn[aria-busy="true"] {
cursor: progress;
opacity: 0.8;
pointer-events: none;
}
.btn[aria-busy="true"]::after {
content: "";
width: 1em;
height: 1em;
margin-left: 0.5rem;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
.btn[aria-busy="true"]::after {
animation-duration: 3s;
}
}The aria-busy attribute marks the button as working. The ::after pseudo-element draws a small spinner with a border. The reduced-motion query slows the spinner for people who ask their system to limit animation. Setting pointer-events: none blocks mouse clicks, but it does not block keyboard activation, so you still need a check in your script.
Common errors
- Relying on color change alone. Add a second cue such as reduced opacity, a changed cursor, or a text label.
- Using
disabledon a button that needs an explanation, so keyboard users never find it. - Leaving the button clickable during loading, which sends duplicate requests.
- Changing the button's size when the spinner appears, which shifts the layout.
- Removing the focus outline on a loading button. Keep
:focus-visiblestyles working in every state.
Exercise
A signup button should stay unavailable until the email field is valid. Why might you choose aria-disabled="true" over the disabled attribute, and what must you add when you do?
Show answer
aria-disabled keeps the button in the tab order, so keyboard and screen reader users can reach it and hear that it is unavailable, ideally with a reason linked through aria-describedby. You must add the blocking behavior yourself, for example calling event.preventDefault() in the submit handler while the attribute is "true", because the browser no longer blocks the action.
Exercise
Why should a loading button keep its width and its label, or use a progress label, instead of being replaced by a spinner alone?
Show answer
A stable size avoids layout shift, and a visible label tells users what is happening. A spinner alone can be missed by screen reader users and gives no information about the action.