How to Make Calculator in HTML CSS and JavaScript

The best way for learning JavaScript is by creating projects. If you want to be a skilled web developer, you should start working on projects right away. Begin by developing simple applications like a calculator, digital clock, or timer.

This simple calculator may be built using only basic web technology such as HTML, CSS, and JavaScript. This calculator can do basic mathematical operations such as adding, subtracting, multiplying, and dividing numbers. So, without any more delay, learn how to make calculator in HTML, CSS, and JavaScript.

Features of the Calculator

This project will have the following features:

  1. Basic Mathematical Operations: Users can use the calculator to conduct basic arithmetic operations such as addition, subtraction, multiplication, and division. It calculates the result exactly based on the input provided.
  2. User-friendly Interface: The calculator provides a user-friendly interface with buttons for each numerical digit and operation. Users can easily input numbers and perform calculations using the intuitive layout.
  3. Clear and Delete Functionality: The calculator offers a clear button that allows users to erase the entire input and start fresh. Additionally, a delete button enables users to remove the last entered digit or operation if they make a mistake.
  4. Decimal Point: The calculator includes a decimal point button, allowing users to input decimal numbers for precise calculations.
  5. Responsive Design: The calculator is made responsive, adapting to different screen sizes and devices. It ensures a consistent and accessible experience across various platforms.

These features collectively provide a practical and user-friendly calculator, allowing individuals to perform basic mathematical calculations efficiently using JavaScript.

The code used in this project is available in our GitHub repository freely, and if you want to set an eye on a live version of this project so visit this demo also.

Components of the Calculator

The calculator created using JavaScript typically consists of several components:

Build a Simple Calculator Using HTML CSS and JavaScript

  1. Number Buttons: These buttons represent numerical digits from 0 to 9. When clicked, they add the corresponding digit to the input or perform other operations, such as concatenating numbers for multi-digit input.
  2. Operator Buttons: These buttons represent mathematical operators such as addition (+), subtraction (-), multiplication (*), and division (/). When clicked, they perform the corresponding operation using the current input values.
  3. Equal Button: This button triggers the calculation and displays the result of the operation based on the current input values and selected operator.
  4. Clear Button: When clicked, this button clears the entire input, allowing the user to start fresh with a new calculation.
  5. Delete Button: This button removes the last entered digit or operation, allowing the user to correct mistakes in the input.
  6. Decimal Button: This button adds a decimal point to the current input, allowing the user to input decimal numbers for more precise calculations.
  7. Display Area: This area shows the current input and the result of the calculations. It dynamically updates as the user inputs numbers and performs operations.

All of these components combine to build a functioning and interactive JavaScript calculator that allows users to perform simple mathematical calculations.

Calculator Project Folder Structure

Make a root folder with the HTML, CSS, and JavaScript files. You can call the files whatever you wish. The calculator is the name of the root folder in this case and our HTML, CSS, and JavaScript files are titled index.html, styles.css, and script.js  respectively.

HTML Code

Open the index.html file and paste the following HTML code for the calculator:

// How to Make Calculator in HTML //
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8" />
    <title>Calculator using HTML, CSS and JavaScript</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <table class="calculator">
      <tr>
        <td colspan="3">
          <input class="display-box" type="text" id="result" disabled />
        </td>
        <!-- clearScreen() function clears all the values -->
        <td>
          <input type="button" value="C" onclick="clearScreen()" id="btn" />
        </td>
      </tr>
      <tr>
        <!-- display() function displays the value of clicked button -->
        <td><input type="button" value="7" onclick="display('7')" /></td>
        <td><input type="button" value="8" onclick="display('8')" /></td>
        <td><input type="button" value="9" onclick="display('9')" /></td>
        <td><input type="button" value="+" onclick="display('+')" /></td>
      </tr>
      <tr>
        <td><input type="button" value="4" onclick="display('4')" /></td>
        <td><input type="button" value="5" onclick="display('5')" /></td>
        <td><input type="button" value="6" onclick="display('6')" /></td>
        <td><input type="button" value="-" onclick="display('-')" /></td>
      </tr>
      <tr>
        <td><input type="button" value="1" onclick="display('1')" /></td>
        <td><input type="button" value="2" onclick="display('2')" /></td>
        <td><input type="button" value="3" onclick="display('3')" /></td>
        <td><input type="button" value="/" onclick="display('/')" /></td>
      </tr>
      <tr>
        <td><input type="button" value="0" onclick="display('0')" /></td>
        <td><input type="button" value="." onclick="display('.')" /></td>
        <!-- calculate() function evaluates the mathematical expression -->
        <td>
          <input type="button" value="=" onclick="calculate()" id="btn" />
        </td>
        <td><input type="button" value="*" onclick="display('*')" /></td>
      </tr>
    </table>
    <script src="script.js"></script>
  </body>
</html>
The overall structure of the calculator is created with the <table> tag in this project. The <table> tag has five rows that represent the calculator’s five horizontal portions. Each row is represented by a <tr> tag. Each <tr> element contains <td> tags that contain the calculator’s display screen and buttons.

CSS Code

Open the style.css file and paste the following CSS code for the calculator:

// How to Make Calculator in HTML //
@import url("https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@300&display=swap");
.calculator {
  height: 500px;
  width: 400px;
  padding: 20px;
  border-radius: 10px;
  margin: 100px auto;
  box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;
  background-color: #201d19;
}
.display-box {
  font-family: "Roboto Slab", sans-serif;
  background-color: #fff;
  border: solid black 0.5px;
  color: black;
  border-radius: 5px;
  width: 100%;
  height: 65%;
  font-size: 25px;
}
#btn {
  background-color: #ff9800;
}
input[type="button"] {
  border: 0;
  outline: 0;
  width: 80%;
  height: 70%;
  border-radius: 5px;
  box-shadow: -8px -8px 15px rgba(255, 255, 255, 0.1),5px 5px 15px rgba(0, 0, 0, 0.2);
  background: #ff9800;
  font-size: 30px;
  color: #fff;
  cursor: pointer;
  margin: 10px;
}
input:active[type="button"] {
  background: #201d19;
  -webkit-box-shadow: inset 0px 0px 5px #c1c1c1;
  -moz-box-shadow: inset 0px 0px 5px #c1c1c1;
  box-shadow: inset 0px 0px 5px #c1c1c1;
}

The calculator is styled using the CSS shown above. In CSS, the .class selector selects elements that have a specified class attribute. The .calculator and .display-box class selectors style the calculator’s table structure and display screen, respectively. @import uses Google Fonts to import the Roboto Slab font family.

JavaScript Code

Open the script.js file and add functionality to the simple calculator using the following JavaScript code:

// How to Make Calculator in HTML //
// This function clear all the values
function clearScreen() {
  document.getElementById("result").value = "";
}
// This function display values
function display(value) {
  document.getElementById("result").value += value;
}
// This function evaluates the expression and returns result
function calculate() {
  var p = document.getElementById("result").value;
  var q = eval(p);
  document.getElementById("result").value = q;
}

Understanding the JavaScript Code

The clearScreen(), display(), and calculate() functions add functionality to the Calculator.

Clearing Values: The clear screen () function accesses the DOM using the resultID and clears its value by passing it an empty string. You can use DOM selectors to target multiple elements on a page.

function clearScreen() {
  document.getElementById("result").value = "";
}

Displaying Values: The display() function gets the result’s id to access the DOM and appends the value of the clicked button to the result.

function display(value) {
  document.getElementById("result").value += value;
}

Evaluating Expression: The calculate() function uses the result’s id to access the DOM and evaluates the expression using the eval() function. The evaluated value of the expression is assigned to the result once more. The eval() function in JavaScript evaluates an expression that you supply to it. It returns the expression’s final result.

function calculate() {
  var p = document.getElementById("result").value;
  var q = eval(p);
  document.getElementById("result").value = q;
}

So, this is how we build a Simple Calculator Using HTML CSS, and JavaScript.

Create Interesting Programming Projects

You can improve your coding skills by creating projects, whether you’re a new freshman or returning to coding after a break. Creating completely functional apps, even if they are simple, may improve your confidence.

You can experiment with us variety of simple projects ranging from games (chess, tic-tac-toe, Rock Paper Scissors) to utilities (to-do list, weight conversion, countdown clock) in upcoming tutorials.

So bookmark our website to learn upcoming beginner to advanced-level projects to improve your skills as a developer.

FAQ on How to Make Calculator in HTML

[WPSM_AC id=6041]

When creating a calculator or any other online application, remember to check out current resources and tutorials for thorough instructions for use and best practices.

Leave a Reply

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

error: Content is protected !!