Skip to content
Knsept
Free playbook

Copy-to-clipboard button

One-click copy with an animated check confirmation and a graceful fallback.

JavaScriptUI Widgets Updated Sep 30, 2026
Live demo Reduced motion is on, so effects are toned down
<div class="copy-row">
  <code id="install-command">npm create astro@latest</code>
  <button class="copy" type="button" data-copy-target="#install-command" aria-label="Copy command">
    <svg viewBox="0 0 24 24" class="icon-copy" aria-hidden="true"><rect x="9" y="9" width="11" height="11" rx="2"/><path d="M5 15V6a2 2 0 0 1 2-2h9"/></svg>
    <svg viewBox="0 0 24 24" class="icon-check" aria-hidden="true"><path d="m5 12 4.5 4.5L19 7"/></svg>
  </button>
  <span class="copy-status" role="status"></span>
</div>

The two icons are stacked, and toggling one class swaps them with a springy scale. The check "draws" itself by animating stroke-dashoffset from its full length to zero.

If the Clipboard API is blocked (insecure origin, sandboxed iframe, older browser), the fallback selects the text so a keyboard copy still works. A hidden role="status" element tells screen-reader users what happened.

Reuse it: point data-copy-target at any element with a CSS selector.

More from the library

Buttons live

Hold-to-confirm button

A safer button for destructive actions. Press and hold until the bar fills; let go early to cancel.

Buttons live

Ripple button

A bright press ripple that starts exactly where you click or tap, and from the center for keyboard users.

Buttons live

Glowing gradient border

A button with a rotating gradient border and soft glow, done with one registered CSS property and no extra elements.