Your blog Post #121-147 all have pre code blocks. User must manually select copy - bad UX. Copy button = 1 click copy = time on page up, users love. Must add to all code boxes auto.
In Post #148, add copy button + syntax highlight.
Step 1: Add Auto Copy Button to All Pre Tags
- Blogger → Theme → Edit HTML → Find
]]></b:skin>→ Above it paste:
/* Copy Code Button Post 148 */
pre{position:relative;background:#0f172a;color:#e2e8f0;border-radius:12px;padding:18px 14px 14px;margin:16px 0;overflow-x:auto;font-size:13px;line-height:1.5;}
pre code{color:#e2e8f0;}
.copy-btn{position:absolute;top:8px;right:8px;background:#fff;color:#000;border:none;padding:5px 10px;border-radius:20px;font-size:11px;font-weight:700;cursor:pointer;box-shadow:0 2px 6px rgba(0,0,0,0.2);}
.copy-btn.copied{background:#10b981;color:#fff;}
- Find
</body>→ Above paste:
<script>
// Copy Code Button Auto - Post 148
document.addEventListener('DOMContentLoaded', function(){
document.querySelectorAll('pre').forEach(function(pre){
if(pre.querySelector('.copy-btn')) return;
var btn = document.createElement('button');
btn.className='copy-btn';
btn.textContent='Copy';
btn.onclick = function(){
var code = pre.innerText.replace(/^Copy/,'').trim();
navigator.clipboard.writeText(code).then(function(){
btn.textContent='Copied ✓';
btn.classList.add('copied');
setTimeout(function(){ btn.textContent='Copy'; btn.classList.remove('copied'); },2000);
});
};
pre.appendChild(btn);
});
});
</script>
Now every pre from Post #121-147 gets copy button auto.
Step 2: Add Language Label on Code Box (HTML, CSS, JS)
<div class="code-wrap" data-lang="HTML">
<pre><code>...your code...</code></pre>
</div>
<style>
.code-wrap{position:relative;}
.code-wrap::before{content:attr(data-lang);position:absolute;top:-10px;left:14px;background:#f59e0b;color:#000;font-size:10px;font-weight:700;padding:2px 8px;border-radius:10px;z-index:1;}
</style>
Use data-lang="CSS", "JS", "HTML" to show badge.
Step 3: Syntax Highlight - Lightweight (No External Library)
- If you want colors, add prism.js lightweight - but our method below no external JS for speed:
<!-- Optional Prism - only if you want colors, adds 30kb --> <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-css.min.js"></script>
My advice: Don't add Prism, keep dark theme CSS from Step 1 - faster like Post #131-132 optimization.
Step 4: Add Copy Count Tracking for GA4
btn.onclick = function(){
navigator.clipboard.writeText(code);
if(typeof gtag!=='undefined') gtag('event','code_copy', {code_lang: pre.closest('.code-wrap')?.dataset.lang || 'unknown'});
}
Check Analytics → which code copied most = which post most useful → create more like that.
Step 5: Test All Old Posts
- Open Post #121 → See copy button top right of each code
- Click → Copied ✓ → Paste in notepad → Should work
- Mobile check - button visible? If not, add:
@media(max-width:600px){.copy-btn{top:6px;right:6px;}}
Post #148 Complete - Copy UX done.
Next Post #149: How to Add Related Posts with Images - advanced grid.
0 Comments