Create a Calculator using HTML, CSS, and JavaScript

A calculator is an essential tool for performing quick calculations and solving complex mathematical equations. In this era of technology, we are surrounded by digital devices that offer calculators as a built-in feature. However, there is something satisfying about create a calculator using HTML, CSS, and JavaScript.

In this article, we will guide you through the process of creating a simple calculator using HTML, CSS, and JavaScript. By the end, you will have a fully functional calculator that you can use for your own calculations. Recently I created a How To Create a Todo List

Simple Steps for Creating a Calculator in HTML CSS & JavaScript

Create a Folder: Start by creating a folder for your project. You can name it anything you like. Inside this folder, you’ll need to create the following files:

  • index.html (for the structure of your calculator)
  • style.css (for styling the calculator)
  • script.js (for adding functionality with JavaScript)

Create the HTML File:

  • Name your HTML file index.html.

Create the CSS File:

  • Name your CSS file style.css.

Create the JavaScript File:

  • Name your JavaScript file script.js.

First, copy the following code snippets and paste them into your code editor

				
					<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Calculator</title>
  <style id="wpr-lazyload-bg-container"></style><style id="wpr-lazyload-bg-exclusion"></style>
<noscript>
<style id="wpr-lazyload-bg-nostyle">.ultp-block-wrapper .slick-loading .slick-list{--wpr-bg-0b5b422c-8abe-448a-ba0a-f8c92bbaf8dc: url('https://notarena.com/wp-content/plugins/ultimate-post/assets/img/loading.gif');}.rll-youtube-player .play{--wpr-bg-e1490727-9a56-4e3b-aabc-0d2a4c4775ce: url('https://notarena.com/wp-content/plugins/wp-rocket/assets/img/youtube.png');}</style>
</noscript>
<script type="application/javascript">const rocket_pairs = [{"selector":".ultp-block-wrapper .slick-loading .slick-list","style":".ultp-block-wrapper .slick-loading .slick-list{--wpr-bg-0b5b422c-8abe-448a-ba0a-f8c92bbaf8dc: url('https:\/\/notarena.com\/wp-content\/plugins\/ultimate-post\/assets\/img\/loading.gif');}","hash":"0b5b422c-8abe-448a-ba0a-f8c92bbaf8dc","url":"https:\/\/notarena.com\/wp-content\/plugins\/ultimate-post\/assets\/img\/loading.gif"},{"selector":".rll-youtube-player .play","style":".rll-youtube-player .play{--wpr-bg-e1490727-9a56-4e3b-aabc-0d2a4c4775ce: url('https:\/\/notarena.com\/wp-content\/plugins\/wp-rocket\/assets\/img\/youtube.png');}","hash":"e1490727-9a56-4e3b-aabc-0d2a4c4775ce","url":"https:\/\/notarena.com\/wp-content\/plugins\/wp-rocket\/assets\/img\/youtube.png"}]; const rocket_excluded_pairs = [];</script></head>
  
  <body>
    <div class="container">
      <div class="calculator">
        <input type="text" id="inputBox" placeholder="0" />
        <div>
          <button class="button operator">AC</button>
          <button class="button operator">DEL</button>
          <button class="button operator">%</button>
          <button class="button operator">/</button>
        </div>
        <div>
          <button class="button">7</button>
          <button class="button">8</button>
          <button class="button">9</button>
          <button class="button operator">*</button>
        </div>
        <div>
          <button class="button">4</button>
          <button class="button">5</button>
          <button class="button">6</button>
          <button class="button operator">-</button>
        </div>
        <div>
          <button class="button">1</button>
          <button class="button">2</button>
          <button class="button">3</button>
          <button class="button operator">+</button>
        </div>

        <div>
          <button class="button">00</button>
          <button class="button">0</button>
          <button class="button">.</button>
          <button class="button equalBtn">=</button>
        </div>
      </div>
    </div>

  </body>
</html>
				
			

HTML Structure in this structure It includes:

  • Result Display: A div (<div id=”result-display”></div>) at the top of the calculator to show the calculation history, such as 2 + 4 = 6.

Second, copy the following code and paste it into your style.css file

				
					<style>
    @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap');

*{
    margin: 1;
    padding: 1;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

body{
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(45deg, #000000, #000000);
}

.calculator{
    border: 1px solid #717377;
    padding: 20px;
    border-radius: 16px;
    background: transparent;
    box-shadow: 0px 3px 15px rgba(29, 116, 255, 0.5);

}

input{
    width: 320px;
    border: none;
    padding: 24px;
    margin: 10px;
    background: transparent;
    box-shadow: 0px 3px 15px rgbs(84, 84, 84, 0.1);
    font-size: 40px;
    text-align: right;
    cursor: pointer;
    color: #ffffff;
}

input::placeholder{
    color: #ffffff;
}

button{
    border: none;
    width: 60px;
    height: 60px;
    margin: 10px;
    border-radius: 10%;
    background: transparent;
    color: #ffffff;
    font-size: 20px;
    box-shadow: -8px -8px 15px rgba(0, 115, 255, 0.237);
    cursor: pointer;
}

.equalBtn{
    background-color: #066cf1;
}

.operator{
    color: #0a74ee;
}
  </style>
				
			

Third, copy the following code and paste it into your script.js file

				
					<script>
        let input = document.getElementById('inputBox');
let buttons = document.querySelectorAll('button');

let string = "";
let arr = Array.from(buttons);
arr.forEach(button => {
    button.addEventListener('click', (e) =>{
        if(e.target.innerHTML == '='){
            string = eval(string);
            input.value = string;
        }

        else if(e.target.innerHTML == 'AC'){
            string = "";
            input.value = string;
        }
        else if(e.target.innerHTML == 'DEL'){
            string = string.substring(0, string.length-1);
            input.value = string;
        }
        else{
            string += e.target.innerHTML;
            input.value = string;
        }
        
    })
})
    </script>
				
			

Javascripts Functionality The JavaScript handles all the logic for the calculator, including input handling, operations, and displaying results.

In addition to these basic operations, you can add advanced features like square roots, exponents, and logarithms to make your calculator more versatile. You can also design it to have a dark or light mode, depending on your preference.

Creating a calculator using HTML, CSS, and JavaScript is not only a fun and educational project, but it also helps improve your web development skills. It allows you to practice your coding and problem-solving abilities while creating a practical and useful tool.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link
Powered by Social Snap