Skip to content
Code Playground — Unique Foundation
🚀

Code Playground

Write HTML & CSS in real time and see your creations come to life instantly. Perfect for learning, experimenting, and building!

⚡ Live PreviewSee changes instantly
📚 SnippetsReady-to-use examples
🎯 ChallengesPractice with tasks
📋 ConsoleDebug your code
HTML
CSS
Ready
0 chars
Ln 1, Col 1 HTML5 · UTF-8
`;const defaultCSS = `/* Your styles here */ body { font-family: sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #f8f9ff, #ede9ff); margin: 0; }h1 { color: #6c63ff; font-size: 48px; margin-bottom: 16px; }p { color: #666; font-size: 18px; }`;htmlEditor.value = defaultHTML; cssEditor.value = defaultCSS;/* ── GUTTER ─────────────────────────────────────── */ function updateGutter(editorId, gutterId) { const ed = document.getElementById(editorId); const g = document.getElementById(gutterId); const lines = ed.value.split('\n').length; let html = ''; for (let i = 1; i <= lines; i++) html += `${i}`; g.innerHTML = html; }function syncScroll(editorId, gutterId) { const ed = document.getElementById(editorId); const g = document.getElementById(gutterId); ed.addEventListener('scroll', () => { g.scrollTop = ed.scrollTop; }); }syncScroll('editor-html', 'gutter-html'); syncScroll('editor-css', 'gutter-css');/* ── TABS ─────────────────────────────────────────── */ function switchTab(lang) { ['html','css'].forEach(l => { document.getElementById(`tab-${l}`).classList.toggle('active', l === lang); document.getElementById(`area-${l}`).classList.toggle('active', l === lang); }); updateGutter(`editor-${lang}`, `gutter-${lang}`); }/* ── CURSOR / CHAR COUNT ────────────────────────── */ function updateStatus(ed) { const val = ed.value; const pos = ed.selectionStart; const lines = val.substr(0, pos).split('\n'); document.getElementById('cursorPos').textContent = `Ln ${lines.length}, Col ${lines[lines.length-1].length+1}`; const active = document.querySelector('.editor-area.active textarea'); document.getElementById('charCount').textContent = `${active.value.length} chars`; }[htmlEditor, cssEditor].forEach(ed => { ed.addEventListener('input', () => { updateGutter(ed.id, 'gutter-' + ed.id.replace('editor-','')); updateStatus(ed); runCode(); }); ed.addEventListener('keyup', () => updateStatus(ed)); ed.addEventListener('click', () => updateStatus(ed)); ed.addEventListener('keydown', e => { if (e.key === 'Tab') { e.preventDefault(); const s = ed.selectionStart, end = ed.selectionEnd; ed.value = ed.value.substr(0,s) + ' ' + ed.value.substr(end); ed.selectionStart = ed.selectionEnd = s+2; updateGutter(ed.id, 'gutter-' + ed.id.replace('editor-','')); } }); });/* ── RUN / PREVIEW ─────────────────────────────── */ let debounceTimer; function runCode() { clearTimeout(debounceTimer); debounceTimer = setTimeout(doRun, 350); }function doRun() { const html = htmlEditor.value; const css = cssEditor.value; const doc = `${extractBody(html)}`; try { // Update the currently visible preview if (isDesktop) { const iframe = document.getElementById('previewDesktop'); iframe.srcdoc = doc; } else { const iframe = document.getElementById('preview'); iframe.srcdoc = doc; } setStatus(true, 'Ready'); logConsole('✓ Preview updated', 'ok'); } catch(e) { setStatus(false, 'Error'); logConsole('✗ ' + e.message, 'err'); } }function extractBody(html) { const m = html.match(/]*>([\s\S]*?)<\/body>/i); return m ? m[1] : html; }function resetPreview() { doRun(); }function openFull() { const html = htmlEditor.value; const css = cssEditor.value; const doc = `${extractBody(html)}`; const w = window.open('', '_blank'); w.document.write(doc); w.document.close(); }/* ── STATUS ─────────────────────────────────────── */ function setStatus(ok, text) { const pill = document.getElementById('statusPill'); pill.className = 'status-pill ' + (ok ? 'ok' : 'err'); document.getElementById('statusText').textContent = text; }/* ── CONSOLE ─────────────────────────────────────── */ let logCount = 0; function logConsole(msg, type='') { if (logCount > 40) consoleEl.innerHTML = '
Console
'; const d = document.createElement('div'); d.className = 'console-line ' + type; d.textContent = msg; consoleEl.appendChild(d); consoleEl.scrollTop = consoleEl.scrollHeight; logCount++; }/* ── SNIPPETS ─────────────────────────────────────── */ function loadSnippet(name) { const s = snippets[name]; if (!s) return; htmlEditor.value = s.html; cssEditor.value = s.css; updateGutter('editor-html', 'gutter-html'); updateGutter('editor-css', 'gutter-css'); doRun(); showToast('Snippet loaded: ' + name); logConsole('→ Snippet "' + name + '" loaded', 'ok'); }/* ── CLEAR ─────────────────────────────────────── */ function clearAll() { htmlEditor.value = ''; cssEditor.value = ''; updateGutter('editor-html', 'gutter-html'); updateGutter('editor-css', 'gutter-css'); doRun(); showToast('Editors cleared'); }/* ── COPY CODE ─────────────────────────────────── */ function copyCode() { const combined = `\n${htmlEditor.value}\n\n/* CSS */\n${cssEditor.value}`; navigator.clipboard.writeText(combined).then(() => showToast('Code copied to clipboard!')); }/* ── MODE ─────────────────────────────────────── */ function setMode(mode) { // Update desktop header buttons ['html-css','html-only','css-only'].forEach(m => { const btn = document.getElementById('mode-'+m); if (btn) btn.classList.toggle('active', m===mode); // Also update mobile sidebar buttons const mobBtn = document.getElementById('mob-mode-'+m); if (mobBtn) mobBtn.classList.toggle('active', m===mode); }); if (mode === 'html-only') { switchTab('html'); document.getElementById('tab-css').style.display='none'; } else if (mode === 'css-only') { switchTab('css'); document.getElementById('tab-html').style.display='none'; } else { document.getElementById('tab-html').style.display=''; document.getElementById('tab-css').style.display=''; } showToast('Mode: ' + mode.replace('-',' ')); }/* ── TOAST ─────────────────────────────────────── */ function showToast(msg) { const t = document.getElementById('toast'); t.textContent = msg; t.classList.add('show'); setTimeout(() => t.classList.remove('show'), 2200); }/* ── WELCOME ─────────────────────────────────────── */ function closeWelcome() { document.getElementById('welcome').style.animation = 'fadeIn 0.2s ease reverse'; setTimeout(() => document.getElementById('welcome').remove(), 200); }/* ── DESKTOP RESIZE ─────────────────────────────── */ const handle = document.getElementById('resizeHandle'); const editorPanel = document.getElementById('editorPanel'); const previewPanelDesktop = document.getElementById('previewPanelDesktop'); let isResizing = false;handle.addEventListener('mousedown', e => { if (!isDesktop) return; isResizing = true; handle.classList.add('dragging'); document.body.style.cursor = 'col-resize'; document.body.style.userSelect = 'none'; });document.addEventListener('mousemove', e => { if (!isResizing) return; const workspace = document.querySelector('.workspace'); const rect = workspace.getBoundingClientRect(); const sidebar = 220; const x = e.clientX - rect.left - sidebar; const total = rect.width - sidebar - 4; const pct = Math.min(Math.max(x / total * 100, 25), 75); editorPanel.style.flex = 'none'; editorPanel.style.width = pct + '%'; previewPanelDesktop.style.flex = 'none'; previewPanelDesktop.style.width = (100 - pct) + '%'; });document.addEventListener('mouseup', () => { if (!isResizing) return; isResizing = false; handle.classList.remove('dragging'); document.body.style.cursor = ''; document.body.style.userSelect = ''; });/* ── INIT ─────────────────────────────────────── */ window.addEventListener('resize', checkLayout); checkLayout(); updateGutter('editor-html','gutter-html'); updateGutter('editor-css','gutter-css'); doRun();

N.B.- This page is now under testing, Please let us know if any trouble occurs…

Log in