Sub topic 11
Disabled and loading states
This chapter covers the two states where a control is visible but cannot be used right now: disabled and loading. It assumes you know basic HTML elements and attributes, CSS selectors, the box model, colors, and pseudo-classes like :hover and :focus-visible. You should also be comfortable opening your browser's developer tools. The earlier chapters on interaction states and focus indicators set up the vocabulary used here.
Why these states need their own design
A normal button promises that something will happen when you press it. A disabled or loading button breaks that promise for a moment. If the design does not explain why, users guess. They click again, assume the page is broken, or give up.
Two questions guide every decision in this chapter:
- Can the user tell the control is not available right now?
- Can the user tell why, and what to do next?
Dimming a button answers the first question only. The second question is where most designs fail.
The `disabled` attribute and what it does
The native disabled attribute on a <button>, <input>, <select>, or <textarea> does several things at once:
- The element ignores clicks and key presses.
- It is removed from the tab order, so keyboard users cannot focus it.
- Browsers apply a default dimmed style.
- It matches the
:disabledpseudo-class in CSS. - Its value is left out of form submission (for form fields).
That list shows the trade-off. The attribute is simple and reliable, but removing the element from the tab order has costs:
- A keyboard or screen reader user may never discover that the button exists.
- They cannot focus it to read a tooltip or hint that explains why it is unavailable.
- Mouse users often get no hover or tooltip events on disabled controls, depending on the browser.
Links have no disabled attribute. An <a> element without an href is not a link in the browser's eyes and is not focusable. If a navigation target is unavailable, the usual choice is to remove the link, or render plain text with an explanation. Do not fake a disabled link by blocking clicks with JavaScript and leaving the styling unchanged.
Styling the disabled state
A disabled control should look clearly unavailable but still be readable. A common approach is reduced opacity, a muted color, and a cursor change.
.btn:disabled,
.btn[aria-disabled="true"] {
opacity: 0.6;
cursor: not-allowed;
background: #e5e7eb;
border-color: #d1d5db;
color: #4b5563;
}
.btn:disabled:hover,
.btn[aria-disabled="true"]:hover {
background: #e5e7eb;
border-color: #d1d5db;
}The second rule cancels the hover color from the normal button. Without it, a disabled button would still react to the pointer and look available.
Contrast on disabled controls
WCAG (the Web Content Accessibility Guidelines) exempts inactive user interface components from the contrast requirement. That exemption is not a reason to make the label unreadable. Users with low vision still need to read what the button says, so they can understand what is missing. Note that stacking opacity: 0.6 on top of an already pale color can push the text far below a readable level. Check the final rendered color, not the CSS value.
A practical rule: aim for text that is clearly dimmer than active controls but still around 3:1 contrast against its background.
Do not use color alone
A grey button next to a blue button is a color-only cue. Combine it with at least one other signal: a changed cursor, a message near the button, or an icon. The explanation text is the strongest signal of all.
`aria-disabled` as an alternative
The fourth code example in this tutorial's plan uses aria-disabled="true" instead of the disabled attribute. This keeps the button focusable while telling assistive technology it is unavailable.
<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>Here the browser still treats the button as active. Nothing blocks the click except your script, which calls preventDefault() on the form's submit event. That has consequences you must handle yourself:
- Styling is manual.
:disableddoes not match, so you write the attribute selector. - Blocking is manual. If you forget the script check, the form submits. Every handler that the button triggers needs the same guard.
- Hover and active styles still apply unless you override them, as in the earlier CSS.
- Focus works. The user can tab to the button, and a screen reader announces it as "dimmed" or "unavailable".
Choosing between the two
| Situation | Better choice | Reason |
|---|---|---|
| The action is truly impossible and the reason is obvious | `disabled` | Simplest, safest, built-in blocking |
| The user needs to learn why it is unavailable | `aria-disabled` plus visible text | Button stays focusable, so hints can be reached |
| A form is incomplete and errors should be shown on press | Leave the button enabled | Pressing explains what is missing |
| A request is in flight | `aria-disabled` or a loading state | Focus should not jump away mid-action |
The third row deserves attention. Many usability studies and accessibility reviews favor keeping a submit button enabled and showing validation errors on press. A disabled submit button gives no feedback about which field is wrong.
Explaining why something is disabled
Put the reason where the user will see it without needing a hover. Hover-only tooltips fail on touch screens and for keyboard users.
<button type="submit" class="btn" aria-disabled="true" aria-describedby="submit-hint">
Place order
</button>
<p id="submit-hint">Add a delivery address to continue.</p>The aria-describedby attribute links the button to the hint, so a screen reader reads both together. Sighted users see the same text in the page. This costs one line of copy and removes most of the confusion.
Loading states
A loading state shows that the user's action was received and work is under way. Without it, users click a second time, which can cause duplicate orders or double submissions.
A good loading button does four things:
- Changes its label or adds a spinner so the change is visible.
- Blocks repeated activation.
- Keeps its size stable, so the layout does not jump.
- Tells assistive technology what is happening.
<button type="submit" class="btn" id="pay" aria-disabled="false">
<span class="btn__label">Pay now</span>
</button>
<p id="pay-status" role="status"></p>
<script>
const pay = document.getElementById('pay');
const status = document.getElementById('pay-status');
const label = pay.querySelector('.btn__label');
pay.addEventListener('click', async (event) => {
if (pay.getAttribute('aria-disabled') === 'true') {
event.preventDefault();
return;
}
pay.setAttribute('aria-disabled', 'true');
pay.classList.add('is-loading');
label.textContent = 'Processing…';
status.textContent = 'Processing your payment.';
try {
await new Promise((resolve) => setTimeout(resolve, 1500));
status.textContent = 'Payment complete.';
label.textContent = 'Paid';
} finally {
pay.classList.remove('is-loading');
}
});
</script>The role="status" element is a live region. Screen readers announce changes to its text without moving focus. This matters because the visual spinner means nothing to someone who cannot see it.
The matching CSS adds the spinner and keeps the width steady:
.btn.is-loading {
position: relative;
cursor: progress;
min-width: 9rem;
}
.btn.is-loading::after {
content: "";
width: 1em;
height: 1em;
margin-left: 0.5em;
border: 2px solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: btn-spin 0.8s linear infinite;
}
@keyframes btn-spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.btn.is-loading::after {
animation-duration: 3s;
}
}The reduced-motion query slows the spinner for users who asked their system to limit animation. The text label still carries the meaning, so the spinner is a secondary cue.
Loading state flow
Notice that Error returns to Idle. A failed request must leave the button usable again, or the user is stuck. A common bug is setting the loading state on press and only clearing it in the success branch.
Common errors
- Disabled and unexplained. The button is grey and there is no text saying what is missing.
- Opacity too low. The label becomes unreadable, especially on tinted backgrounds.
- Hover still active. A disabled button still changes color under the pointer.
aria-disabledwithout a script guard. The button looks off, but the click still works.- Spinner only. A screen reader user hears nothing. Add text or a live region.
- Layout shift. The label changes from "Pay now" to "Processing…" and the button resizes, moving the controls around it.
- Stuck loading. An error path never resets the state.
- Focus lost. Using
disabledon the button that has focus makes focus jump to the page body in some browsers. Preferaria-disabledfor in-flight actions.
Checking your work
Open developer tools and force the states. In Chrome and Firefox you can toggle :hover, :active, and :focus on an element from the Styles panel. Then confirm:
- The disabled button does not change on hover.
- The label meets about 3:1 contrast after opacity is applied.
- The reason for the disabled state is visible without hovering.
- Pressing Tab reaches
aria-disabledbuttons and skips nativedisabledones. - A screen reader (or the accessibility tree in dev tools) reports the state.
- The button width does not change when loading starts.
Exercise
A signup form has a "Create account" button that is grey until every field is valid. Users report they do not know why they cannot continue. Give two changes that fix this, and say which attribute choice keeps the button reachable by keyboard.
Show answer
First, show visible text near the button that names what is missing (for example "Enter a password with at least 8 characters") and link it with aria-describedby. Second, consider leaving the button enabled and showing field-level errors when the user presses it, since that tells users exactly which field to fix. If the button must look unavailable, use aria-disabled="true" instead of the disabled attribute: it keeps the button in the tab order, so keyboard and screen reader users can reach it and hear the hint. You must then block submission yourself with a script check.
Exercise
Why can a loading button that only shows a spinning icon fail users, and what two additions fix it?
Show answer
A spinner is only a visual cue, so screen reader users receive no information that work is under way, and some users may not recognize the icon. Add a text label change such as "Processing…" on the button, and announce progress through a live region such as an element with role="status". Also block repeat activation and keep the button width stable.