Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

PHP

How to Make a Multiple Choice Quiz using HTML, CSS, and JavaScript

By Admin
March 6, 2023 3 Min Read
Comments Off on How to Make a Multiple Choice Quiz using HTML, CSS, and JavaScript

Are you looking to create an interactive quiz that engages your audience? A multiple-choice quiz is a great way to do just that! With HTML, CSS, and JavaScript, you can create an engaging quiz that your audience will love. In this tutorial, we will walk you through the process of creating a multiple-choice quiz using HTML, CSS, and JavaScript.

Follow this video for complete guidance :

Step 1: Create the HTML structure

The first step in creating a multiple-choice quiz is to create the HTML structure. We will use the <form> element to create the quiz.

<div class="quiz-container">
  <h1>Multiple Choice Quiz</h1>
  <hr>
  <form id="quiz-form">
    <div class="d-flex">
      <div class="question">
        <p>1. What is the capital of France?</p>
        <div class="answers">
          <label>
            <input type="radio" name="q1" value="a">
            Paris
          </label>
          <label>
            <input type="radio" name="q1" value="b">
            Madrid
          </label>
          <label>
            <input type="radio" name="q1" value="c">
            London
          </label>
          <label>
            <input type="radio" name="q1" value="d">
            Rome
          </label>
        </div>
      </div>
      
      <div class="question">
        <p>2. What is the largest planet in our solar system?</p>
        <div class="answers">
          <label>
            <input type="radio" name="q2" value="a">
            Venus
          </label>
          <label>
            <input type="radio" name="q2" value="b">
            Mars
          </label>
          <label>
            <input type="radio" name="q2" value="c">
            Jupiter
          </label>
          <label>
            <input type="radio" name="q2" value="d">
            Saturn
          </label>
        </div>
      </div>
    </div>
    <button type="submit" class="submit-btn">Submit</button>
  </form>
  
  <div class="result" id="result"></div>
</div>

Step 2: Style the quiz with CSS

Once you have created the HTML structure, you can style the quiz with CSS. In this example, we will use CSS to style the questions, answer choices, and submit button.

body {
  font-family: Arial, sans-serif;
  font-size: 16px;
  background-color: aliceblue;
}
form{
  margin-top:20px;
}
.quiz-container {
  width: 80%;
  margin: 0 auto;
}

h1 {
  text-align: center;
}

.question {
  width:45%;
  margin-bottom: 20px;
  background-color: #fff;
  padding:20px;
}
.question:nth-child(odd){
  margin-right:5%;
}
.question p{
  font-weight: bold;
}

.answers {
  margin-bottom: 20px;
}
.answers label:not(:first-child){
  border-top:1px solid #e0e0e0;
}

label {
  padding:10px 0;
  display: block;
  margin-bottom: 10px;
}

input[type="radio"] {
  margin-right: 10px;
}

.submit-btn {
  display: block;
  margin: 20px auto;
  padding: 10px 20px;
  background-color: #4CAF50;
  color: #fff;
  border: none;
  border-radius: 5px;
  font-size: 18px;
  cursor: pointer;
}

.result {
  font-weight: bold;
  font-size: 22px;
  margin-top: 20px;
  padding: 10px;
  background-color: #f2f2f2;
  border-radius: 5px;
  text-align: center;
}
.d-flex{
  display: flex;
}

Step 3: Add JavaScript functionality

Finally, we will add JavaScript functionality to our quiz. We will use JavaScript to check the user’s answers and display their score.

const quizForm = document.getElementById('quiz-form');
const resultDiv = document.getElementById('result');
const correctAnswers = ['a', 'c'];

quizForm.addEventListener('submit', e => {
  e.preventDefault();
  
  let score = 0;
  const userAnswers = [quizForm.q1.value,quizForm.q2.value];
  userAnswers.forEach((answer, index) => {
    if (answer === correctAnswers[index]) {
      score += 1;
    }
  });

  resultDiv.innerHTML = `Your score is ${score}/${correctAnswers.length}`;
});

Complete source code is as follows :

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Multiple Choice Quiz</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      font-size: 16px;
      background-color: aliceblue;
    }
    form{
      margin-top:20px;
    }
    .quiz-container {
      width: 80%;
      margin: 0 auto;
    }
    
    h1 {
      text-align: center;
    }
    
    .question {
      width:45%;
      margin-bottom: 20px;
      background-color: #fff;
      padding:20px;
    }
    .question:nth-child(odd){
      margin-right:5%;
    }
    .question p{
      font-weight: bold;
    }
    
    .answers {
      margin-bottom: 20px;
    }
    .answers label:not(:first-child){
      border-top:1px solid #e0e0e0;
    }
    
    label {
      padding:10px 0;
      display: block;
      margin-bottom: 10px;
    }
    
    input[type="radio"] {
      margin-right: 10px;
    }
    
    .submit-btn {
      display: block;
      margin: 20px auto;
      padding: 10px 20px;
      background-color: #4CAF50;
      color: #fff;
      border: none;
      border-radius: 5px;
      font-size: 18px;
      cursor: pointer;
    }
    
    .result {
      font-weight: bold;
      font-size: 22px;
      margin-top: 20px;
      padding: 10px;
      background-color: #f2f2f2;
      border-radius: 5px;
      text-align: center;
    }
    .d-flex{
      display: flex;
    }
  </style>
</head>
<body>
  <div class="quiz-container">
    <h1>Multiple Choice Quiz</h1>
    <hr>
    <form id="quiz-form">
      <div class="d-flex">
        <div class="question">
          <p>1. What is the capital of France?</p>
          <div class="answers">
            <label>
              <input type="radio" name="q1" value="a">
              Paris
            </label>
            <label>
              <input type="radio" name="q1" value="b">
              Madrid
            </label>
            <label>
              <input type="radio" name="q1" value="c">
              London
            </label>
            <label>
              <input type="radio" name="q1" value="d">
              Rome
            </label>
          </div>
        </div>
        
        <div class="question">
          <p>2. What is the largest planet in our solar system?</p>
          <div class="answers">
            <label>
              <input type="radio" name="q2" value="a">
              Venus
            </label>
            <label>
              <input type="radio" name="q2" value="b">
              Mars
            </label>
            <label>
              <input type="radio" name="q2" value="c">
              Jupiter
            </label>
            <label>
              <input type="radio" name="q2" value="d">
              Saturn
            </label>
          </div>
        </div>
      </div>
      <button type="submit" class="submit-btn">Submit</button>
    </form>
    
    <div class="result" id="result"></div>
  </div>
  
  <script>
    const quizForm = document.getElementById('quiz-form');
    const resultDiv = document.getElementById('result');
    const correctAnswers = ['a', 'c'];
    
    quizForm.addEventListener('submit', e => {
      e.preventDefault();
      
      let score = 0;
      const userAnswers = [quizForm.q1.value,quizForm.q2.value];
      userAnswers.forEach((answer, index) => {
        if (answer === correctAnswers[index]) {
          score += 1;
        }
      });
    
      resultDiv.innerHTML = `Your score is ${score}/${correctAnswers.length}`;
    });
</script>

 

Tags:

quiz
Author

Admin

Follow Me
Other Articles
Previous

PHP Debugging and Troubleshooting: Tips and Tools for Developers

Next

PHP script for creating a Social Media Sharing System

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme