Skip to the main content

Copy to Clipboard Button

Free Utilities

Buttons that copy a line of code, say so for three seconds in a live region, and select the text for a keyboard copy when the clipboard is missing.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="cpy" aria-labelledby="cpy-title">
  <h2 class="cpy__title" id="cpy-title">Quick start</h2>
  <p class="cpy__intro">Press a button to copy the line beside it.</p>

  <div class="cpy__item">
    <p class="cpy__label" id="cpy-label-1">Build the site</p>
    <div class="cpy__row">
      <pre class="cpy__code" tabindex="0"><code id="cpy-code-1">npm run build -- --minify</code></pre>
      <button class="cpy__button" type="button" data-copy="cpy-code-1" data-name="build command" hidden>
        <svg class="cpy__icon" viewBox="0 0 20 20" width="18" height="18" aria-hidden="true" focusable="false"><rect x="7" y="7" width="10" height="10" rx="2" fill="none" stroke="currentColor" stroke-width="1.8"/><path d="M13 4.5V4a1.5 1.5 0 0 0-1.5-1.5H5A1.5 1.5 0 0 0 3.5 4v6.5A1.5 1.5 0 0 0 5 12h.5" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/></svg>
        <span class="cpy__text">Copy</span><span class="cpy__sr"> build command</span>
      </button>
    </div>
  </div>

  <div class="cpy__item">
    <p class="cpy__label" id="cpy-label-2">Centre anything with CSS</p>
    <div class="cpy__row">
      <pre class="cpy__code" tabindex="0"><code id="cpy-code-2">display: grid; place-items: center;</code></pre>
      <button class="cpy__button" type="button" data-copy="cpy-code-2" data-name="CSS line" hidden>
        <svg class="cpy__icon" viewBox="0 0 20 20" width="18" height="18" aria-hidden="true" focusable="false"><rect x="7" y="7" width="10" height="10" rx="2" fill="none" stroke="currentColor" stroke-width="1.8"/><path d="M13 4.5V4a1.5 1.5 0 0 0-1.5-1.5H5A1.5 1.5 0 0 0 3.5 4v6.5A1.5 1.5 0 0 0 5 12h.5" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/></svg>
        <span class="cpy__text">Copy</span><span class="cpy__sr"> CSS line</span>
      </button>
    </div>
  </div>

  <p class="cpy__status" id="cpy-status" role="status" data-state="idle"></p>
</section>

CSS

.cpy {
  --cpy-bg: #ffffff;
  --cpy-tint: #f3f4fb;
  --cpy-ink: #1c2033;
  --cpy-muted: #4b5266;
  --cpy-line: #d5d9e6;
  --cpy-code-bg: #171a2b;
  --cpy-code-ink: #e9ecfb;
  --cpy-accent: #4338ca;
  --cpy-on-accent: #ffffff;
  --cpy-ok: #0f6b3a;
  --cpy-focus: #1c2033;
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  max-width: 36rem;
  padding: 1.25rem 1.5rem 1.4rem;
  color: var(--cpy-ink);
  background: var(--cpy-bg);
  border: 1px solid var(--cpy-line);
  border-radius: 16px;
  box-shadow: 0 1px 2px rgba(28, 32, 51, 0.06), 0 10px 28px -14px rgba(28, 32, 51, 0.22);
}
.cpy *, .cpy *::before, .cpy *::after { box-sizing: inherit; }
.cpy [hidden] { display: none; }
.cpy__title { margin: 0; font-size: 1.3rem; line-height: 1.2; letter-spacing: -0.01em; }
.cpy__intro { margin: 0.25rem 0 1.1rem; color: var(--cpy-muted); font-size: 0.92rem; }
.cpy__item { margin-bottom: 1rem; }
.cpy__label { margin: 0 0 0.35rem; font-size: 0.85rem; font-weight: 650; }

.cpy__row { display: flex; align-items: stretch; gap: 0.5rem; }
.cpy__code { flex: 1 1 auto; min-width: 0; margin: 0; background: var(--cpy-code-bg); border-radius: 12px; padding: 0.85rem 1rem; overflow-x: auto; color: var(--cpy-code-ink); font-size: 0.88rem; line-height: 1.5; display: flex; align-items: center; }
.cpy__code code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre; }
.cpy__code:focus-visible, .cpy__button:focus-visible { outline: 3px solid var(--cpy-focus); outline-offset: 2px; }

.cpy__button {
  flex: none; display: inline-flex; align-items: center; gap: 0.45rem;
  min-width: 7rem; min-height: 2.75rem; padding: 0 1rem; justify-content: center;
  font: inherit; font-size: 0.88rem; font-weight: 700; cursor: pointer;
  color: var(--cpy-on-accent); background: var(--cpy-accent);
  border: 0; border-radius: 12px;
  transition: background-color 0.15s ease;
}
.cpy__button:hover { filter: brightness(1.12); }
.cpy__button.is-copied { background: var(--cpy-ok); color: #ffffff; }
.cpy__sr { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.cpy__status { min-height: 1.4em; margin: 0.2rem 0 0; color: var(--cpy-muted); font-size: 0.85rem; }

@media (max-width: 30rem) {
  .cpy { padding: 1rem; }
  .cpy__button { min-width: 2.75rem; padding: 0 0.75rem; }
  .cpy__text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
}
@media (prefers-reduced-motion: reduce) { .cpy__button { transition: none; } }
@media (prefers-color-scheme: dark) {
  .cpy {
    --cpy-bg: #1c2033; --cpy-tint: #272c45; --cpy-ink: #eceef8; --cpy-muted: #b3b9d0; --cpy-line: #3d4466;
    --cpy-code-bg: #0e1020; --cpy-accent: #b4b9ff; --cpy-on-accent: #14172a; --cpy-ok: #7fe0a8; --cpy-focus: #ffffff;
    box-shadow: none;
  }
  .cpy__button.is-copied { color: #0c2a1a; }
}

JavaScript

(function () {
  'use strict';

  var root = document.querySelector('.cpy');
  var status = document.getElementById('cpy-status');
  if (!root || !status) return;

  var buttons = Array.prototype.slice.call(root.querySelectorAll('.cpy__button'));
  var timer = 0;

  function resetButtons() {
    buttons.forEach(function (button) {
      button.classList.remove('is-copied');
      button.querySelector('.cpy__text').textContent = 'Copy';
    });
    status.textContent = '';
    status.setAttribute('data-state', 'idle');
  }

  function say(button, state, message) {
    resetButtons();
    if (state === 'copied') {
      button.classList.add('is-copied');
      button.querySelector('.cpy__text').textContent = 'Copied';
    }
    status.textContent = message;
    status.setAttribute('data-state', state);
    clearTimeout(timer);
    timer = setTimeout(resetButtons, 3000);
  }

  // Without the clipboard, the text is selected so the visitor can copy it with the keyboard.
  function selectText(element) {
    var range = document.createRange();
    var selection = window.getSelection();
    range.selectNodeContents(element);
    selection.removeAllRanges();
    selection.addRange(range);
  }

  function copy(button) {
    var source = document.getElementById(button.getAttribute('data-copy'));
    var name = button.getAttribute('data-name');
    function fallback() {
      selectText(source);
      say(button, 'selected', 'Copying is not available here. The ' + name + ' is selected: press Ctrl+C or Cmd+C.');
    }
    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(source.textContent).then(function () {
        say(button, 'copied', 'Copied the ' + name + ' to the clipboard.');
      }, fallback);
    } else {
      fallback();
    }
  }

  // The buttons need this script, so they appear when it runs.
  buttons.forEach(function (button) {
    button.hidden = false;
    button.addEventListener('click', function () { copy(button); });
  });
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • A live region announces the result, so the label change is not the only signal.
  • Each button's name says what it copies, such as Copy build command.
  • The code lines can be scrolled and selected with the keyboard.
Utilities
FreeUses XON

To-do List

A small task list with add, tick, delete, filter and clear-done, with the count announced in a live region and nothing sent anywhere.

  • HTML
  • CSS
  • Uses XON
  • Lists
  • State