Code Block with Copy
A dark code block with a header and a working copy button.
Requires Bootstrap 5. Add the CDN tags below to your page (styles in
<head>, scripts before </body>). The downloaded file already includes them.<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script><div class="card border-0 shadow" style="max-width:400px;background:#0d1117"><div class="card-body p-0">
<div class="d-flex justify-content-between align-items-center px-3 py-2" style="background:#161b22"><span class="small" style="color:#8b949e">app.js</span><button class="btn btn-sm py-0 px-2" id="cbCopy" style="color:#8b949e;border:1px solid #30363d">Copy</button></div>
<pre class="mb-0 p-3" id="cbCode" style="color:#c9d1d9;font-size:.82rem">const sum = (a, b) => a + b;
console.log(sum(2, 3));</pre>
</div></div>document.getElementById('cbCopy').onclick=function(){var b=this;navigator.clipboard&&navigator.clipboard.writeText(document.getElementById('cbCode').innerText);b.textContent='Copied!';setTimeout(function(){b.textContent='Copy'},1400)};Free to use.This component uses Bootstrap 5 (linked above). Copy any file, or download a ready-to-open .html page with the CDN links already included.