Snippets / UI Components /

Loading Button

A button with a CSS spinner that appears during loading state, using animation and pseudo-elements only.

Widely Supported
uino-js

Quick implementation

.loading-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  background-color: oklch(0.55 0.2 250);
  color: oklch(1 0 0);
  border: none;
  border-radius: var(--radius);
  font-family: var(--font-sans);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s ease;
  min-width: 8rem;
  height: 3rem;
}

.loading-btn:hover {
  background-color: oklch(0.65 0.2 250);
}

/* Loading State */
.loading-btn.loading {
  background-color: oklch(0.45 0.2 250);
  cursor: wait;
  opacity: 0.9;
  pointer-events: none;
}

/* Spinner */
.loading-btn.loading::after {
  content: "";
  display: block;
  width: 1rem;
  height: 1rem;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .loading-btn.loading::after {
    animation: none;
    border-top-color: transparent;
    border-left-color: transparent;
    border-right-color: transparent;
    border-bottom-color: currentColor;
    transform: rotate(0deg);
  }
}

Prompt this to your LLM

Includes role, constraints, two framework variants, and edge cases to handle.

Create a reusable CSS-only loading button component.

Requirements:
1. Base button styling with a modern look (padding, rounded corners, hover states).
2. A `.loading` modifier class that changes the cursor and opacity.
3. A spinner implemented using the `::after` pseudo-element.
4. The spinner must use CSS animation (`@keyframes`) to rotate.
5. Use `oklch()` for all colors to ensure modern color gamut support.
6. Include a `@media (prefers-reduced-motion: reduce)` query to disable the spinner animation for accessibility.
7. Ensure the button has `pointer-events: none` when loading to prevent double submissions.

Output the CSS and a small HTML example showing both the default and loading states.

Why this matters in 2026

As web applications become more interactive, providing immediate feedback is crucial for user experience. A loading button bridges the gap between user action (click) and server response (success/error). By handling this state purely in CSS, we reduce the JavaScript bundle size and complexity, making the UI more resilient and performant.

The logic

The core technique relies on the ::after pseudo-element. When the .loading class is added to the button, the pseudo-element becomes visible. We use border: 2px solid transparent with border-top-color: currentColor to create a partial circle. The currentColor keyword ensures the spinner matches the button's text color automatically, which is vital for theming.

Accessibility & performance

Visual feedback is good, but semantic feedback is better. When adding the .loading class via JavaScript, you should also set aria-busy="true" on the button element. This informs screen readers that the control is currently processing. Additionally, the prefers-reduced-motion media query ensures that users who are sensitive to motion do not experience the spinning animation, replacing it with a static indicator.