Number Trivia App using HTML, CSS & JavaScript

Number Trivia App using HTML, CSS & JavaScript

Number Trivia App using HTML, CSS & JavaScript


Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Number Trivia App which will an app which consists a trivia and it has random facts & get facts option which tell the facts about the number which has entered by the user. 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 Number Trivia App Project.

Number Trivia 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 Number Trivia 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 Number Trivia App Project.

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

I hope you have got an idea about the project.

HTML Code for Number Trivia App

First we’ll start with creating the structure of the Number Trivia 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>Number Trivia App</title>
    <!-- Google Fonts -->
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <div class="container">
      <div class="search-container">
        <input type="num" placeholder="Number" id="num" />
        <button id="get-fact-btn">Get Fact</button>
      <button id="ran-fact-btn">Get Random Fact</button>
      <div class="fact"></div>
    <!-- Script -->
    <script src="script.js"></script>

How to Create a Wave Background using CSS

CSS Code for Number Trivia App

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Number Trivia 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: #566fff;
.container {
  background-color: #ffffff;
  width: 95vw;
  font-size: 16px;
  max-width: 31.25em;
  padding: 3.5em 2.5em;
  position: absolute;
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  border-radius: 0.6em;
  box-shadow: 0 1.2em 3em rgba(0, 4, 52, 0.2);
.search-container {
  display: flex;
  justify-content: space-between;
.container input,
.container button {
  font-size: 1.25em;
  outline: none;
  border: none;
  border-radius: 0.4em;
.container input {
  width: 40%;
  border: 1px solid #bdbdbf;
  padding: 0.6em;
.container button {
  width: 55%;
  background-color: #566fff;
  color: #ffffff;
  font-weight: 500;
  padding: 0.7em;
  cursor: pointer;
#ran-fact-btn {
  width: 100%;
  margin-top: 1em;
.fact {
  background-color: #ffffff;
  box-shadow: 0 0 1.2em rgba(0, 4, 52, 0.1);
  margin: 3em auto 0 auto;
  padding: 2.8em 1.8em;
  border-radius: 0.6em;
  border: none;
  border-bottom: 0.6em solid #566fff;
  display: none;
.fact h2 {
  font-size: 2.8em;
  color: #181a34;
.fact p {
  font-size: 1.15em;
  font-weight: 400;
  color: #50526b;
  line-height: 1.8em;
  text-align: justify;
  margin-top: 0.8em;
p.msg {
  text-align: center;
  font-weight: 500;
  color: #181a34;

10+ Javascript Projects For Beginners With Source Code

JavaScript Code for Number Trivia 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 defined the buttons which are created in structure in the get element ID and further we have added some functions to make it work on the user side. Let us see the Final Output of the project Number Trivia App using HTML, CSS & JavaScript (Source Code).

let getFactBtn = document.getElementById("get-fact-btn");
let ranFactBtn = document.getElementById("ran-fact-btn");
let fact = document.querySelector(".fact");
let url = "";

let fetchFact = (num) => {
  let finalUrl = url + num;
    .then((resp) => resp.text())
    .then((data) => { = "block";
      fact.innerHTML = `<h2>${num}</h2>
let getFact = () => {
  let num = document.getElementById("num").value;
  //Check if input number is not empty
  //If not empty
  if (num) {
    //Check if number lies between 0 and 300
    //if Yes fetch the fact
    if (num >= 0 && num <= 300) {
    //If number is less than 0 or greater than 300 display error message.
    else { = "block";
      fact.innerHTML = `<p class="msg"> Please enter a number between 0 to 300.</p>`;
  //If input number is empty display error message
  else { = "block";
    fact.innerHTML = `<p class="msg">The input field cannot be empty</p>`;

let getRandomFact = () => {
  //Random number between 0 to 300
  let num = Math.floor(Math.random() * 301);

getFactBtn.addEventListener("click", getFact);
ranFactBtn.addEventListener("click", getRandomFact);
window.addEventListener("load", getRandomFact);



Live Preview of Number Trivia App using HTML, CSS & JavaScript

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

We have Successfully created our Number Trivia App 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