Why show a loader?
Have you ever clicked a button and wondered whether anything happened? Without feedback, people often click again, which can send a form twice or place a duplicate order. A small loading spinner inside the button solves this: it shows straight away that the click worked and that something is in progress. Try it:
In this tutorial we'll build that button with HTML, CSS and a little JavaScript, then look at a variation and some tips for making it work well for everyone.
The HTML
<button type="submit" class="load-button">Click me!</button>
<p class="visually-hidden" role="status"></p>
type="submit"makes the button submit its form. If the button isn't in a form, usetype="button"instead.class="load-button"is what we'll use to style the button and to find it with JavaScript.- The empty
role="status"paragraph is a live region: when JavaScript puts text in it, screen readers (software that reads pages aloud) announce it. We'll use it to say "Sending…", because a spinner on its own says nothing to someone who can't see it.
Avoid id="submit": the original version of this post gave the button id="submit". Inside a form, that's a trap: form controls with an id or name become properties of the form, so form.submit would point to the button instead of the form's submit() method, breaking any code that calls it. Use a class, or a more specific id such as contact-submit.
The CSS
.load-button {
position: relative; /* the spinner is positioned inside the button */
min-width: 10em; /* stops the button shrinking when the text hides */
padding: 12px 24px;
border: 0;
border-radius: 8px;
background: #1d4ed8;
color: #ffffff;
font: 600 1rem/1.2 system-ui, sans-serif;
cursor: pointer;
}
/* The spinner: a circle with one bright edge */
.load-button::after {
content: "";
position: absolute;
inset: 0; /* stretch over the button... */
width: 1.25em;
height: 1.25em;
margin: auto; /* ...then centre a fixed-size circle */
border: 0.2em solid rgb(255 255 255 / 0.35);
border-right-color: #ffffff;
border-radius: 50%;
opacity: 0; /* hidden until loading */
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* While loading: hide the label, show and spin the circle */
.load-button.is-loading {
color: transparent;
cursor: progress;
}
.load-button.is-loading::after {
opacity: 1;
animation: spin 0.7s linear infinite;
}
/* Hide the status text visually, but keep it for screen readers */
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
Here's what's happening:
- The spinner is a
::afterpseudo-element: a circle with a faint border, where one side (border-right-color) is bright white. Rotating it makes the bright edge chase round the circle. inset: 0withmargin: autocentres the spinner perfectly, whatever the button's size.color: transparenthides the label while loading. The text is still there, so the button keeps its size, and screen readers still know what the button is called.- The animation is only added while loading, so the browser isn't spinning an invisible circle the rest of the time.
The JavaScript
JavaScript adds the is-loading class when the button is clicked, and removes it when the work is done. In a real project, the "work" is usually sending data to a server with fetch():
const button = document.querySelector('.load-button');
const status = document.querySelector('[role="status"]');
button.addEventListener('click', async (event) => {
event.preventDefault();
// Ignore extra clicks while we're already busy
if (button.getAttribute('aria-disabled') === 'true') return;
button.classList.add('is-loading');
button.setAttribute('aria-disabled', 'true');
status.textContent = 'Sending…';
try {
await fetch('/api/send', { method: 'POST' }); // your real action here
status.textContent = 'Sent!';
} catch (error) {
status.textContent = 'Sorry, something went wrong. Please try again.';
} finally {
// Always reset the button, whether it worked or not
button.classList.remove('is-loading');
button.removeAttribute('aria-disabled');
}
});
aria-disabled="true"tells assistive technology the button is unavailable for now, and ourifcheck ignores any extra clicks, so the action can't run twice.try/catch/finallymakes sure the button always returns to normal, even if the request fails, and that failures get a helpful message.- The status text is announced to screen reader users at each stage.
Why not disabled? Adding the disabled attribute also stops repeat clicks, but in some browsers a disabled button loses keyboard focus, so keyboard users are suddenly sent back to the top of the page. aria-disabled keeps focus where it is. The demos use it, along with a simulated three-second delay in place of fetch().
Variation: a spinner with a label
Hiding the label leaves people guessing what's happening. Another approach keeps text visible: a small spinner appears beside the label, the label changes to describe what's happening, and the button briefly confirms success:
<button type="submit" class="save-button">
<span class="spinner" aria-hidden="true"></span>
<span class="label">Save changes</span>
</button>
.save-button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 10px;
min-width: 10em;
}
.save-button .spinner {
display: none;
width: 1em;
height: 1em;
border: 0.15em solid rgb(255 255 255 / 0.35);
border-right-color: #ffffff;
border-radius: 50%;
}
.save-button.is-loading .spinner {
display: block;
animation: spin 0.7s linear infinite;
}
.save-button.is-done {
background: #15803d; /* green for success */
}
In the JavaScript, change the label's text as well as the classes: "Save changes", then "Saving…", then "Saved", and back again after a couple of seconds. Keep the button's min-width large enough for the longest label, so it doesn't jump in size.
Accessibility and usability tips
- Don't rely on the spinner alone. Announce what's happening with a live region, and say clearly when it has finished or failed.
- Respect reduced motion. For people who prefer less movement, slow the spinner down rather than removing it, so they can still see something is happening:
@media (prefers-reduced-motion: reduce) { .load-button.is-loading::after { animation-duration: 2s; } } - Keep the button the same size. A
min-width, and hiding the text withcolor: transparentrather than removing it, stop the layout jumping around. - Skip the spinner for very quick actions. If something finishes in under about half a second, a spinner just flickers. You can wait 300 milliseconds or so before adding the loading state, and only show it if the action is still running.
- Handle errors. Always reset the button and show a message if something goes wrong, so people aren't left staring at an endless spinner.
- Check contrast. The spinner's bright edge should stand out clearly against the button colour.
Wrapping up
A loader inside a button is a small touch that prevents double clicks and reassures people that something is happening. Build the spinner with a ::after pseudo-element centred using inset and margin: auto, only animate it while loading, block repeat clicks with aria-disabled, and use a live region so everyone hears what's going on, from "Sending…" to "Sent!".