This lightweight JavaScript adds a Copy button to code blocks. When someone clicks the button, the script copies the code text to their clipboard and briefly changes the button label to confirm it worked.
Code examples are easier to use when visitors can copy them with one click. This is helpful for script blogs, documentation pages, webmaster tools, setup guides, and small developer resources.
Wrap your code in a container with the class copy-code-box. The script will find the code block and add a copy button automatically.
<div class="copy-code-box">
<pre><code><p>Hello world</p></code></pre>
</div>
Add this script near the bottom of your page before the closing </body> tag.
<script>
document.addEventListener('DOMContentLoaded', function () {
const boxes = document.querySelectorAll('.copy-code-box');
boxes.forEach(function (box) {
const code = box.querySelector('code');
if (!code) {
return;
}
const button = document.createElement('button');
button.type = 'button';
button.className = 'copy-code-btn';
button.textContent = 'Copy';
button.addEventListener('click', async function () {
try {
await navigator.clipboard.writeText(code.innerText);
button.textContent = 'Copied';
setTimeout(function () {
button.textContent = 'Copy';
}, 1400);
} catch (error) {
button.textContent = 'Failed';
setTimeout(function () {
button.textContent = 'Copy';
}, 1400);
}
});
box.insertBefore(button, box.firstChild);
});
});
</script>
This CSS keeps the copy button simple. You can adjust the colors, borders, spacing, and font to match your own site.
<style>
.copy-code-box{
position:relative;
margin:16px 0;
}
.copy-code-box pre{
margin:0;
padding:14px;
overflow:auto;
border:2px solid #111;
border-radius:12px;
background:#fbfbff;
}
.copy-code-btn{
display:inline-flex;
align-items:center;
justify-content:center;
min-height:34px;
margin:0 0 8px;
padding:0 12px;
border:2px solid #111;
border-radius:10px;
background:#fff;
color:#111;
cursor:pointer;
font:inherit;
font-weight:600;
}
</style>
Here is a complete copy-paste example with the HTML, CSS, and JavaScript together.
<div class="copy-code-box">
<pre><code><button type="button">Click me</button></code></pre>
</div>
<style>
.copy-code-box{
position:relative;
margin:16px 0;
}
.copy-code-box pre{
margin:0;
padding:14px;
overflow:auto;
border:2px solid #111;
border-radius:12px;
background:#fbfbff;
}
.copy-code-btn{
display:inline-flex;
align-items:center;
justify-content:center;
min-height:34px;
margin:0 0 8px;
padding:0 12px;
border:2px solid #111;
border-radius:10px;
background:#fff;
color:#111;
cursor:pointer;
font:inherit;
font-weight:600;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
const boxes = document.querySelectorAll('.copy-code-box');
boxes.forEach(function (box) {
const code = box.querySelector('code');
if (!code) {
return;
}
const button = document.createElement('button');
button.type = 'button';
button.className = 'copy-code-btn';
button.textContent = 'Copy';
button.addEventListener('click', async function () {
try {
await navigator.clipboard.writeText(code.innerText);
button.textContent = 'Copied';
setTimeout(function () {
button.textContent = 'Copy';
}, 1400);
} catch (error) {
button.textContent = 'Failed';
setTimeout(function () {
button.textContent = 'Copy';
}, 1400);
}
});
box.insertBefore(button, box.firstChild);
});
});
</script>
Script directories, code snippet pages, documentation pages, setup guides, tutorial posts, developer notes, lightweight tools, and any page where visitors may want to copy example code quickly.
Forgetting to wrap the code block, placing the script before the code exists on the page, styling the button too close to the code text, or assuming clipboard access will always work in every browser context.
Use this script when your page includes reusable snippets, setup commands, HTML examples, CSS examples, JavaScript examples, PHP examples, or any short block of text visitors may want to copy.
Skip it on pages with no code examples, very short inline snippets, or pages where copying text could create confusion because the snippet needs heavy editing before use.
For more practical examples, browse the Scripts, WebDev Database, or Webmaster Tools.