Back to templates
Interactive multiple-choice quiz
A self-contained HTML quiz with instant answer scoring and replay support for classrooms.
Source code
1 lines · 1.7 KB<style>*{box-sizing:border-box}body{margin:0;font-family:Inter,system-ui,sans-serif;background:#f6f7fb;color:#172033}button,a{font:inherit}.wrap{width:min(960px,calc(100% - 32px));margin:auto}</style><style>main{min-height:100vh;display:grid;place-items:center;padding:24px}.quiz{width:min(520px,100%);background:#fff;border:1px solid #e2e8f0;border-radius:18px;padding:28px}.opt{display:block;width:100%;text-align:left;padding:12px 14px;margin:10px 0;border:1px solid #cbd5e1;border-radius:10px;background:#fff;cursor:pointer}.opt:hover{border-color:#4f46e5}.msg{margin-top:14px;font-weight:600}</style><main><div class="quiz"><p id="step" style="color:#64748b;margin:0 0 8px"></p><h1 id="q" style="font-size:22px;margin:0 0 14px"></h1><div id="opts"></div><p id="msg" class="msg"></p></div></main><script>const data=[{q:"Which HTML attribute isolates an iframe from parent storage?",opts:["sandbox without allow-same-origin","loading=lazy","referrerpolicy=no-referrer"],ans:0},{q:"Which CSS unit scales relative to the root font size?",opts:["px","rem","vh"],ans:1}];let idx=0,score=0;function show(){const item=data[idx];document.getElementById("step").textContent="Question "+(idx+1)+" of "+data.length;document.getElementById("q").textContent=item.q;document.getElementById("msg").textContent="";const box=document.getElementById("opts");box.innerHTML="";item.opts.forEach((o,n)=>{const b=document.createElement("button");b.className="opt";b.textContent=o;b.onclick=()=>{if(n===item.ans)score++;idx++;if(idx<data.length)show();else{box.innerHTML="";document.getElementById("q").textContent="Completed!";document.getElementById("msg").textContent="Score: "+score+" / "+data.length}};box.appendChild(b)})}show()</script>When this template fits
A self-contained multiple-choice quiz ideal for classroom checks, onboarding exercises, and interactive blog tutorials.
How the layout works
Displays one question at a time with full-width option buttons, a question counter, and an end-of-quiz summary score.
What to customize
Expand the data array with explanations per answer, add a Restart button on completion, and avoid exposing answer keys where formal grading security is required.
Review before publishing
Test full keyboard navigation through every option button and verify long question titles wrap cleanly on phones.
Keep building with context
Compare a related pattern or read a practical guide before adapting this example in the Playground.