Last updated
Code Screenshots for Social Media
Code screenshots are images of syntax-highlighted code, used for sharing on Twitter/X, LinkedIn, and other platforms that don't render code blocks. Tools like Carbon, Ray.so, and Chalk.ist generate beautiful code images with customizable themes, fonts, and backgrounds.
Generating Code Images with html2canvas
JavaScript
import html2canvas from 'html2canvas';
import Prism from 'prismjs';
async function codeToImage(code, language = 'javascript') {
// Create a styled container
const container = document.createElement('div');
container.style.cssText = `
background: #1e1e2e; padding: 24px; border-radius: 12px;
font-family: 'Fira Code', monospace; font-size: 14px;
line-height: 1.6; width: 600px; box-shadow: 0 20px 60px rgba(0,0,0,0.5);
`;
// Syntax highlight
const highlighted = Prism.highlight(code, Prism.languages[language], language);
container.innerHTML = `<pre style="margin:0" th:inline="none">${highlighted}`;
document.body.appendChild(container);
const canvas = await html2canvas(container, { scale: 2 });
document.body.removeChild(container);
return canvas.toDataURL('image/png');
}
Popular Code Screenshot Tools
| Tool | URL | Features |
|---|---|---|
| Carbon | carbon.now.sh | Many themes, export PNG/SVG |
| Ray.so | ray.so | Gradient backgrounds, clean UI |
| Chalk.ist | chalk.ist | Minimal, fast |
| Codeimg.io | codeimg.io | Social media sizes |