Copy Button
Copy-to-clipboard button with success feedback.
<button class="copy-btn inline-flex items-center gap-2 px-4 py-2 rounded-xl border border-slate-200 bg-white text-sm font-bold text-slate-700 hover:border-indigo-300 hover:text-indigo-600 transition-colors">
<svg class="w-4 h-4 copy-icon" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/></svg>
<span class="copy-label">Copy link</span>
</button>
<script>
document.querySelectorAll('.copy-btn').forEach(function(btn) {
btn.addEventListener('click', function() {
var label = btn.querySelector('.copy-label');
var original = label.textContent;
navigator.clipboard.writeText(window.location.href).then(function() {
label.textContent = 'Copied!';
btn.classList.add('text-emerald-600', 'border-emerald-300');
setTimeout(function() {
label.textContent = original;
btn.classList.remove('text-emerald-600', 'border-emerald-300');
}, 2000);
});
});
});
</script>Free to use in personal and commercial projects. No attribution required.
Variants
Same component, different looks. Preview each one live and copy the code you like.
Dark
The dark copy button sits well in code blocks and dark toolbars.
Icon only
Go icon-only in tight toolbars where the copy action is obvious.
Large
Size it up for primary share actions on blog posts and docs.
About this copy button
Copy buttons are everywhere because copying is a core web action: share links, promo codes, API keys, addresses. This copy to clipboard button handles the full interaction: click, copy via the Clipboard API, visual confirmation ("Copied!" in emerald), then revert after 2 seconds.
The feedback is what makes it work. Without the label change, users click repeatedly unsure if it worked. The 2-second confirmation is long enough to register, short enough not to annoy.
Use the async Clipboard API (navigator.clipboard.writeText) with a fallback for older browsers. For sensitive content like passwords, consider masking until copy. The button should indicate what will be copied: "Copy link", "Copy code", not just "Copy".
Copying exact strings by manual selection is error-prone: users miss the first character, grab trailing whitespace, or select the label instead of the value. For API keys, promo codes, and share links, that imprecision creates real support tickets. A copy button removes the precision task entirely: one click, the exact string, and confirmation it worked.
The click handler calls navigator.clipboard.writeText with the target string, then swaps the label to Copied! with emerald styling for two seconds before reverting. That feedback loop is the whole interaction: without it, users click repeatedly, unsure anything happened. The button label names what gets copied (Copy link, Copy code), and the overlapping-squares icon is the universally read affordance.
Keep the confirmation visible for about two seconds: enough to be noticed, brief enough to stay out of the way. In code blocks, dock the button in the header's top-right where developers expect it. For long values, make the value itself clickable too, with the button as the visible affordance for those who need the hint.
The silent-failure mistake is calling the Clipboard API with no fallback and showing Copied! regardless, so on insecure contexts the button lies. Chain it properly: try navigator.clipboard first, fall back to the hidden-textarea plus execCommand technique, and show an honest error state if both fail. A copy button that fakes success is worse than none.
Where to use it
- ✓ Share links
- ✓ Promo codes
- ✓ API keys
- ✓ Code snippets
Customization tips
Change what gets copied by editing the writeText argument. For code blocks, place the button in the block header. Add a tooltip confirming the action for extra clarity.
Design notes
When to use
A copy button belongs anywhere users need to grab an exact string: API keys, code snippets, coupon codes, or share links. The success feedback, where the icon flips to a check, closes the loop so users know the copy actually worked instead of pasting to check. Placing it right beside the value it copies removes any doubt about what will land on the clipboard.
What to avoid
Do not use a copy button for long prose, nobody wants a paragraph on their clipboard and selecting text works fine there. Avoid hiding the button until hover on touch devices, where there is no hover and the feature becomes invisible. And do not skip the feedback state, a silent copy leaves users mashing the button wondering if it worked.
Accessibility
Announce the copy result with a live region so screen reader users hear copied without moving focus. Keep the button a real button element so it stays keyboard reachable.
Frequently asked questions
How do I copy text with JavaScript?
Use navigator.clipboard.writeText(text). It returns a promise. For older browsers, fall back to a temporary textarea with document.execCommand('copy').
Why does my copy button need feedback?
Copying is invisible. Without a 'Copied!' confirmation, users click repeatedly unsure if it worked. Always show transient success state.
Can I copy without user click?
No, browsers require user activation for clipboard access (security). The copy must happen in a click handler.
Why does navigator.clipboard need HTTPS?
The Clipboard API is restricted to secure contexts to prevent malicious sites from snooping on clipboard data. It works on HTTPS origins and on localhost for development. On plain HTTP, fall back to the older execCommand("copy") technique or a select-and-copy prompt, wrapped in a try catch.
How do I copy text from a div, not an input?
navigator.clipboard.writeText accepts any string, so read the div's textContent or innerText and pass it directly. No selection or hidden textarea is needed with the modern API. For rich HTML copying you would use navigator.clipboard.write with a ClipboardItem instead.
How do I show a checkmark after copying?
On successful copy, swap the button icon to a check and optionally change the label to "Copied" for 1.5 to 2 seconds, then revert. Announce the success to screen readers with an aria-live region. Handle the catch case with an error state like "Copy failed" so silent failures do not confuse users.