Calculator in HTML CSS JavaScript

Calculator in HTML CSS and JavaScript

Today, we will be making a simple Calculator in HTML CSS JavaScript, where we can use the app to do basic arithmetic calculations. We will be using simple HTML and some basic javascript to get the job done.

We will use JavaScript to calculate and display the result.


  • Code Editor (VS Code Preferred)
  • Chromium Browser (Chrome Preferred)
  • Basic Knowledge of HTML, CSS, Javascript, and Bootstrap


  • Performing simple mathematical calculations
  • Error handling in case of wrong expressions

Folder Structure

We will be using HTML, CSS, BootStrap, and JavaScript for developing a simple Calculator. Check out this image for the folder structure of our project.

The project will include three files, index.html, index.js, and style.css.

folder structure for project

    index.html contains our Html code.

    style.css has been used to style our web page.

    logic.js contains all the logic.

    Coding Calculator in HTML CSS JavaScript

    HTML Code

    <!DOCTYPE html>
    <html lang="en">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="style.css">
        <div class="container">
            <h1>Simple Calculator</h1>
            <div class="calculator">
                <input type="text" name="screen" id="display">
    <script src="index.js"></script>

    JavaScript Code

    To do most of the calculations and detect user input we will take help from this loop, also we will try to catch errors when using the eval function in js, so we used a try-catch block.

    let screen = document.getElementById("display");
    buttons = document.querySelectorAll("button");
    let screenValue = "";
    for (item of buttons) {
      item.addEventListener("click", (e) => {
        buttonText =;
        if (buttonText == "X") {
          buttonText = "*";
          screenValue += buttonText;
          screen.value = screenValue;
        } else if (buttonText == "C") {
          screenValue = "";
          screen.value = screenValue;
        } else if (buttonText == "=") {
          var result;
          try {
            result = eval(screenValue);
          } catch (error) {
            result = "Expression error";
          screen.value = result;
        } else {
          screenValue += buttonText;
          screen.value = screenValue;

    CSS code

        text-align: center;
        margin: auto;
        border-radius: 21px;
        height: 65px;
        width: 260px;
        scroll-behavior: smooth;
        border-radius: 20px;
        font-size: 20px;
        background: #ffffff;
        width: 60px;
        height: 50px;
        margin: 6px;
        background-color: #161616;
        padding: 23px;
        border-radius: 53px;
        display: inline-block;


    output for Calculator in HTML CSS JavaScript


    I hope this was easy to understand Calculator in HTML CSS JavaScript and that you enjoyed building and learning this at the same time. Along the way, we did some basic dom-manipulation to change the text of the UI, and we also learned to handle exceptions in javascript.

    Also Read:


    Author: Ayush Purawr