Building Tabbed Code Snippet Cards with Syntax Highlighting & 1-Click Copy Feedback
A tabbed code snippet card groups multiple language views (HTML, CSS, JS, Tailwind) within an ARIA tablist (`role="tablist"`). Switching tabs toggles visible `
` code blocks, while a copy button calls `navigator.clipboard.writeText()` and provides instant visual feedback.
Step 1: Structure the Tabbed Code Card Markup
Build the header with language tab buttons, copy action button, and code panels.
<div class="code-snippet-card" id="codeCard">
<div class="code-header">
<div class="code-tabs" role="tablist" aria-label="Code languages">
<button class="code-tab active" data-tab="css" role="tab" aria-selected="true">CSS</button>
<button class="code-tab" data-tab="html" role="tab" aria-selected="false">HTML</button>
<button class="code-tab" data-tab="tailwind" role="tab" aria-selected="false">Tailwind</button>
</div>
<button class="code-copy-btn" id="btnCopySnippet">
<span>Copy</span>
</button>
</div>
<div class="code-body">
<pre class="code-panel active" data-panel="css"><code>.glass-card {
background: rgba(255, 255, 255, 0.08);
backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 16px;
}</code></pre>
<pre class="code-panel" data-panel="html"><code><div class="glass-card">
<h3>Frosted Glass</h3>
</div></code></pre>
</div>
</div>
Step 2: Style Tab Transitions & Monospace Code Blocks
Apply dark developer themes, rounded card borders, and line spacing.
.code-snippet-card {
background: #090d16;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 12px;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
.code-header {
display: flex;
justify-content: space-between;
align-items: center;
background: #0f172a;
padding: 8px 16px;
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.code-tab {
background: none;
border: none;
color: #94a3b8;
padding: 6px 12px;
font-size: 0.85rem;
font-weight: 600;
cursor: pointer;
border-radius: 6px;
}
.code-tab.active {
color: #fff;
background: rgba(255, 255, 255, 0.1);
}
.code-panel {
display: none;
margin: 0;
padding: 18px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.9rem;
line-height: 1.5;
color: #38bdf8;
overflow-x: auto;
}
.code-panel.active {
display: block;
}
Step 3: Wire Tab Switching and Clipboard API
Handle tab selection and copy active panel code with visual checkmark feedback.
const card = document.getElementById('codeCard');
const tabs = card.querySelectorAll('.code-tab');
const panels = card.querySelectorAll('.code-panel');
const copyBtn = document.getElementById('btnCopySnippet');
tabs.forEach(tab => {
tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('active'));
panels.forEach(p => p.classList.remove('active'));
tab.classList.add('active');
const target = card.querySelector(`[data-panel="${tab.dataset.tab}"]`);
if (target) target.classList.add('active');
});
});
copyBtn.addEventListener('click', async () => {
const activeCode = card.querySelector('.code-panel.active code').textContent;
await navigator.clipboard.writeText(activeCode);
copyBtn.innerHTML = '<span>✓ Copied!</span>';
setTimeout(() => {
copyBtn.innerHTML = '<span>Copy</span>';
}, 2000);
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why use CSS counters for line numbers instead of inserting numbers in markup?
When users select text in a code block with markup line numbers, the numbers get copied into their clipboard. CSS counters (`counter-increment: line; content: counter(line);`) are rendered via pseudo-elements and are ignored by clipboard copy selections.
What is the browser compatibility of navigator.clipboard.writeText?
The Clipboard API is supported in all modern browsers (Chrome 66+, Safari 13.1+, Firefox 63+, Edge). It requires an active user gesture (like a button click) and HTTPS protocol.
How do you handle horizontal scrolling for wide code lines on mobile?
Set `overflow-x: auto; -webkit-overflow-scrolling: touch;` on `
` elements to allow smooth horizontal finger swipes without breaking page layout.
Can syntax highlighting be achieved without large 50KB Prism.js bundles?
Yes, for static snippets, pre-render highlighted HTML spans server-side or during build time, resulting in zero client-side JavaScript overhead.
How do you structure code tabs for screen reader accessibility?
Follow the W3C Tabs Pattern: attach `role="tablist"` to the container, `role="tab"` to each button, `aria-selected="true/false"`, and `role="tabpanel"` to code containers.