Quiz App With Timer using HTML, CSS & JavaScript

Quiz App With Timer using HTML, CSS & JavaScript

Quiz App With Timer using HTML, CSS & JavaScript


Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Quiz App with Timer which is basically an application which has a timer and some MCQ questions in which the user has to answer within some seconds and move to the next question. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Quiz App Project.

Quiz App Javascript

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Quiz App Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Quiz App Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Quiz App Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Quiz App

First we’ll start with creating the structure of the Quiz App project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MCQ Quiz</title>
    <!-- Google Font -->
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <div class="start-screen">
      <button id="start-button">Start</button>
    <div id="display-container">
      <div class="header">
        <div class="number-of-count">
          <span class="number-of-question">1 of 3 questions</span>
        <div class="timer-div">
          <img src="https://i.postimg.cc/q7BCXNDy/timer-icon.png" />
          <span class="time-left">10s</span>
      <div id="container">
        <!-- questions and options will be displayed here -->
      <button id="next-button">Next</button>
    <div class="score-container hide">
      <div id="user-score">Demo Score</div>
      <button id="restart">Restart</button>
    <!-- Script -->
    <script src="script.js"></script>

Restaurant Website Using HTML and CSS

CSS Code for Quiz App

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Quiz App project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
body {
  background-color: #0a69ed;
.score-container {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
button {
  border: none;
  outline: none;
  cursor: pointer;
#restart {
  font-size: 1.3em;
  padding: 0.5em 1.8em;
  border-radius: 0.2em;
#restart {
  margin-top: 0.9em;
#display-container {
  background-color: #ffffff;
  padding: 3.1em 1.8em;
  width: 80%;
  max-width: 37.5em;
  margin: 0 auto;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  border-radius: 0.6em;
.header {
  margin-bottom: 1.8em;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 0.6em;
  border-bottom: 0.1em solid #c0bfd2;
.timer-div {
  background-color: #e1f5fe;
  width: 7.5em;
  border-radius: 1.8em;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7em 1.8em;
.question {
  margin-bottom: 1.25em;
  font-weight: 600;
.option-div {
  font-size: 0.9em;
  width: 100%;
  padding: 1em;
  margin: 0.3em 0;
  text-align: left;
  outline: none;
  background: transparent;
  border: 1px solid #c0bfd2;
  border-radius: 0.3em;
.option-div:disabled {
  color: #000000;
  cursor: not-allowed;
#next-button {
  font-size: 1em;
  margin-top: 1.5em;
  background-color: #0a69ed;
  color: #ffffff;
  padding: 0.7em 1.8em;
  border-radius: 0.3em;
  float: right;
.hide {
  display: none;
.incorrect {
  background-color: #ffdde0;
  color: #d32f2f;
  border-color: #d32f2f;
.correct {
  background-color: #e7f6d5;
  color: #689f38;
  border-color: #689f38;
#user-score {
  font-size: 1.5em;
  color: #ffffff;

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

JavaScript Code for Quiz App

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. In this code of JavaScript we have made the question & answer functioning and defined the working of the timer in the Quiz App. Let us see the Final Output of the project Quiz App using HTML, CSS & JavaScript (Source Code).

let timeLeft = document.querySelector(".time-left");
let quizContainer = document.getElementById("container");
let nextBtn = document.getElementById("next-button");
let countOfQuestion = document.querySelector(".number-of-question");
let displayContainer = document.getElementById("display-container");
let scoreContainer = document.querySelector(".score-container");
let restart = document.getElementById("restart");
let userScore = document.getElementById("user-score");
let startScreen = document.querySelector(".start-screen");
let startButton = document.getElementById("start-button");
let questionCount;
let scoreCount = 0;
let count = 11;
let countdown;

//Questions and Options array

const quizArray = [
    id: "0",
    question: "Which is the most widely spoken language in the world?",
    options: ["Spanish", "Mandarin", "English", "German"],
    correct: "Mandarin",
    id: "1",
    question: "Which is the only continent in the world without a desert?",
    options: ["North America", "Asia", "Africa", "Europe"],
    correct: "Europe",
    id: "2",
    question: "Who invented Computer?",
    options: ["Charles Babbage", "Henry Luce", "Henry Babbage", "Charles Luce"],
    correct: "Charles Babbage",

//Restart Quiz
restart.addEventListener("click", () => {

//Next Button
  (displayNext = () => {
    //increment questionCount
    questionCount += 1;
    //if last question
    if (questionCount == quizArray.length) {
      //hide question container and display score
      //user score
      userScore.innerHTML =
        "Your score is " + scoreCount + " out of " + questionCount;
    } else {
      //display questionCount
      countOfQuestion.innerHTML =
        questionCount + 1 + " of " + quizArray.length + " Question";
      //display quiz
      count = 11;

const timerDisplay = () => {
  countdown = setInterval(() => {
    timeLeft.innerHTML = `${count}s`;
    if (count == 0) {
  }, 1000);

//Display quiz
const quizDisplay = (questionCount) => {
  let quizCards = document.querySelectorAll(".container-mid");
  //Hide other cards
  quizCards.forEach((card) => {
  //display current question card

//Quiz Creation
function quizCreator() {
  //randomly sort questions
  quizArray.sort(() => Math.random() - 0.5);
  //generate quiz
  for (let i of quizArray) {
    //randomly sort options
    i.options.sort(() => Math.random() - 0.5);
    //quiz card creation
    let div = document.createElement("div");
    div.classList.add("container-mid", "hide");
    //question number
    countOfQuestion.innerHTML = 1 + " of " + quizArray.length + " Question";
    let question_DIV = document.createElement("p");
    question_DIV.innerHTML = i.question;
    div.innerHTML += `
    <button class="option-div" onclick="checker(this)">${i.options[0]}</button>
     <button class="option-div" onclick="checker(this)">${i.options[1]}</button>
      <button class="option-div" onclick="checker(this)">${i.options[2]}</button>
       <button class="option-div" onclick="checker(this)">${i.options[3]}</button>

//Checker Function to check if option is correct or not
function checker(userOption) {
  let userSolution = userOption.innerText;
  let question =
  let options = question.querySelectorAll(".option-div");

  //if user clicked answer == correct option stored in object
  if (userSolution === quizArray[questionCount].correct) {
  } else {
    //For marking the correct option
    options.forEach((element) => {
      if (element.innerText == quizArray[questionCount].correct) {

  //clear interval(stop timer)
  //disable all options
  options.forEach((element) => {
    element.disabled = true;

//initial setup
function initial() {
  quizContainer.innerHTML = "";
  questionCount = 0;
  scoreCount = 0;
  count = 11;

//when user click on start button
startButton.addEventListener("click", () => {

//hide quiz and display start screen
window.onload = () => {


Live Preview of Quiz App With Timer using HTML, CSS & JavaScript

See the Pen
by Harsh Sawant (@harshh9)
on CodePen.

We have Successfully created our Quiz App with Timer using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply