Skip to content

Button with loading state in HTML and CSS

A button that announces the pending request then success, without changing width.

Here is an excerpt of this component's code in HTML and CSS. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in HTML and CSS

index.html
<!doctype html>
<html lang="fr">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Bouton avec état de chargement</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="demo">
      <button type="button" class="loading-button" data-loading-button>
        <span class="loading-button__label">Envoyer ma demande</span>
loading-button.js
// Replace `send` with your own request (fetch, form submission…); it must return a promise.
function send() {
  return new Promise((resolve) => setTimeout(resolve, 2000));
}

document.querySelectorAll("[data-loading-button]").forEach((button) => {
  const status = document.querySelector("[data-loading-status]");
  button.addEventListener("click", async () => {
    if (button.getAttribute("aria-busy") === "true") return;
    button.style.minWidth = `${button.offsetWidth}px`;
    button.setAttribute("aria-busy", "true");
    button.removeAttribute("data-state");
style.css
:root {
  --loading-bg: #0f6b4f;
  --loading-bg-hover: #0b5a42;
  --loading-text: #ffffff;
  --loading-success: #1e8a5f;
  --loading-ring: #f2b84b;
  --loading-radius: 999px;
  --demo-bg: #f4f6f3;
  --demo-text: #33403a;
}

@media (prefers-color-scheme: dark) {

Free component: copy it freely, including for commercial projects.

Buttons components in HTML and CSS
  • A row of buttons to share the page on Facebook, LinkedIn, WhatsApp or by email, with a copy-link button and no third-party script.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Toggle heart buttons that count favourites, remember them between visits and announce them to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that adds an event to Google Calendar, Outlook or any calendar through an .ics file, with no third-party service.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that copies an IBAN, a reference, an address or a promo code and confirms "Copied", on screen and to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A WhatsApp button pinned to the bottom of the screen that opens a welcome bubble with common topics and a pre-filled message, with no third-party widget.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A bar pinned to the bottom of mobile screens with Call and Directions buttons and a live open-now status, built for trades and local shops.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component