Loading...
We are Using 3rd party Ads Service on our Site. Please bear with us if you face any difficulties

How to Build Stylish Calculator using Html CSS and JavaScript

Today we will learn how to create your very own Calculator using Html CSS and JavaScript to help you get better at it.
Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

A calculator is a small computer that can be carried around and used to do math calculations. Some calculators even allow for simple text programming and editing. When programming, you should use code that is simple and calculated. Learn how to create your very own Calculator using Html CSS and JavaScript.

    Why you Should Build this Calculator

    Creating & Developing the Projects are the great way to learn JavaScript. You need to get started on projects as soon as possible if you want to become a good web developer. Start with simple projects like a stopwatch, digital clock, or calculator for beginners.

    You Can Also Check the Demo of this Project from below.

    Demo Click here

    Whether you are just starting out with coding or have been away from it for some time, developing projects can help you get better at it. Even developing straightforward fully functional apps can boost your confidence.

    You can make a straightforward Calculator using Html CSS and JavaScript and more. This mini-computer can perform essential numerical activities like Addition, Subtraction, Multiplication, and division. You can perform basic math calculations without leaving the computer with the help of calculator programs.

    Features of HTML Calculator

    1. It will perform essential number-crunching tasks like Addition, Subtraction, Multiplication, and division.
    2. It will deal with decimal numbers.
    3. If you attempt to divide any number by zero, the calculator will show "Infinity".
    4. In the event of an invalid expression, it will not display any results. ig: 5++9 will not display anything.
    5. You can use the Clear Screen feature to clear the screen whenever you want.

    The components of the calculator

    The components of the calculator are as follows:

    • It Consists Mathematical Operators i.e, (+, -, *, /)
    • It has Also Display and Decimal Button i.e, (0 to 9)
    • It has a clear Button to clear previous Records
    • And a Equal or Calculate Button (=)

    We are aware that a calculator is a device that makes it easier for us to perform mathematical operations. The majority of calculators, for instance, can add, subtract, multiply, and divide. Some also perform square roots, and more advanced calculators can assist with calculus and function graphing. Adding machines are found all over. You can even construct a calculator that is more advanced and modern, but we will keep this Calculator using Html CSS and JavaScript as simple as it could be.

    The Project Calculator using Html CSS and JavaScript which we are going to build in this Article will look like the following:

    calculator-using-html-css-and-javascript

    How to Build Calculator using Html CSS and JavaScript

      You can simply copy and paste the Calculator using Html CSS and JavaScript with complete source code into your own project in this blog post. Enjoy your explorations and learning! I hope you have an idea of what the project is about.

      Note:
      You can use this Calculator in blogger, just paste the entire code into a new page section and your HTML Calculator is ready to rock.

    1. HTML Section

      Before we can use HTML to set up the structure of the Calculator project, we need to use all of the necessary elements and attributes. Our first step to creating the Calculator Section will be this HTML Part. Later we will know how to code the CSS to incorporate styling and adjust the labels. The HTML code is provided below; copy it and paste it where you want to use it.
    2. <div class="calc">
        <div class="calc-title">
          <span class="calc-title-span">
            HTML Calculator
          </span>
          <div class="calc-title-hr"></div>
        </div>
        <div class="calc-display">
          <div class="calc-display-span secondary-display" id="secondary-display">
          </div>
          <div class="calc-display-span primary-display" id="display">
          </div>
        </div>
        <div class="calc-display-hr"></div>
        <div class="calc-btn" id="btn">
          <button class="calc-btn-primary" id="seven">7</button>
          <button class="calc-btn-primary" id="eight">8</button>
          <button class="calc-btn-primary" id="nine">9</button>
          <button class="calc-btn-primary btn-bg" id="divide">/</button>
          <button class="calc-btn-primary" id="four">4</button>
          <button class="calc-btn-primary" id="five">5</button>
          <button class="calc-btn-primary" id="six">6</button>
          <button class="calc-btn-primary btn-bg" id="multiply">*</button>
          <button class="calc-btn-primary" id="one">1</button>
          <button class="calc-btn-primary" id="two">2</button>
          <button class="calc-btn-primary" id="three">3</button>
          <button class="calc-btn-primary btn-bg" id="add">+</button>
          <button class="calc-btn-primary btn-bg" id="decimal">.</button>
          <button class="calc-btn-primary" id="zero">0</button>
          <button class="calc-btn-primary btn-bg-equal" id="equals">=</button>
          <button class="calc-btn-primary btn-bg" id="subtract">-</button>
          <button class="calc-btn-clear" id="clear">clear</button>
        </div>
      </div>
    3. CSS Section

      Second, we have the CSS code that we have styled to fit the structure of the Calculator using Html CSS and JavaScript project. In addition, the CSS code has been aligned and positioned in such a way that it does not become cluttered with appropriate CSS elements. Now, let's make the CSS component responsive by programming it. Copy and Paste the Code where you want to use it.
    4. <style>
        /* HTML Calculator by Tech & Fun Zone */
        @import url('https://fonts.googleapis.com/css?family=Montserrat:400,600,700,900');
      .calc {
        display: flex;
        flex-direction : column;
        width: 330px;
        height: 600px;
        margin : 50px;
        background-color: #080636;
        background-image: linear-gradient(315deg, #080636 0%, #414141 104%);
        border-radius: 20px;
        box-shadow : 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
      }
      
      .calc-title {
        display: flex;
        flex-direction: column;
        justify-content: center;
        height: 40px;
        align-items: center;
      }
      
      .calc-title-span {
        color: #17e82e;
        font-size: 15px;
        letter-spacing: 1px;
        font-weight: 700;
        line-height: 20px;
      }
      
      .calc-title-hr {
        width: 200px;
        height: 10px;
        background-color: #17e82e;
        opacity: 0.8;
      }
      
      .calc-display {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        width: 100%;
        height: 100px;
        color: #fff; 
      }
      
      
      .secondary-display{
        font-size:20px;
        opacity : 0.8;
        overflow-x : hidden;
      }
      /* ScrollBar Style*/
      .secondary-display::-webkit-scrollbar-track {
          -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
          background-color: #F5F5F5;
      }
      .secondary-display::-webkit-scrollbar {
        height : 4px;
      	background-color: #bdbdbd;
        opacity: 0.7;
      }
      .secondary-display::-webkit-scrollbar-thumb {
          border-radius: 10px;
          background-color: #000000;
      	  border: 3px solid #555555;
      }
      
      .primary-display::-webkit-scrollbar-track {
          -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
          background-color: #F5F5F5;
      }
      .primary-display::-webkit-scrollbar {
        height : 4px;
      	background-color: #bdbdbd;
        opacity: 0.7;
      }
      .primary-display::-webkit-scrollbar-thumb {
          border-radius: 10px;
          background-color: #000000;
      	  border: 3px solid #555555;
      }
      
      .calc-display-hr {
        width: 250px;
        height: 2px;
        margin-top: 10px;
        align-self: center;
        background-color: #bdbdbd;
        opacity: 0.4;
      }
      
      .calc-btn {
        display: flex;
        align-items: center;
        justify-content: space-around;
        flex-wrap: wrap;
        margin: 20px;
      }
      .calc-btn-primary {
        cursor: pointer;
        font-family: 'Montserrat', sans-serif;
        margin: 10px 5px;
        outline: none;
        border: none;
        background-color: transparent;
        color: #fff;
        font-size: 30px;
        width: 60px;
        height: 60px;
        border: 2px solid #616161;
        border-radius: 100%;
        cursor: pointer;
        box-shadow: 2px 4px 11px #141414, -2px -4px 11px #000;
      }
      .calc-btn-primary:hover {
      box-shadow: inset 3px 5px 10px #141414, inset -3px -5px 10px #000;
      border: 2px solid rgba(255, 255, 255, 0.2);
      font-size: 32px;
      }
      
      .btn-bg{
        background-color:#424242 !important;
        opacity : 0.9;
        border : none !important;
      }
      .btn-bg-equal{
        background-color:#ff7555 !important;
        border : none !important;
      }
      .calc-btn-primary:focus {
        outline:none
      }
      .calc-btn-primary:active {
        transform: scale(0.9);
      }
      
      .calc-btn-clear {
        margin: 20px 5px;
        outline: none;
        border: none;
        background-color: transparent;
        color: #fff;
        font-size: 30px;
        padding: 10px 90px;
        border: 3px solid #17e82e;
        border-radius: 11px;
        cursor: pointer;
        box-shadow: 2px 4px 11px #141414, -2px -4px 11px #000;
      }
      .calc-btn-clear:hover {
      box-shadow: inset 3px 5px 10px #17e82e, inset -3px -5px 10px #000;
      border: 2px solid rgba(255, 255, 255, 0.2);
      font-size: 32px;
      }
      .calc-btn-clear:active {
        transform: scale(0.9);
        }
        @media screen and (min-width:641px){.calc{max-width:97%}</style>
    5. JavaScript Section

      The project's final phase is JavaScript, which we added the logic and coded in accordance with the requirements of the calculator project, with a few conditions. In addition, we have developed functions that store the responses and display them when the user does something mathematical. Let's take a look at the final step of Calculator using Html CSS and JavaScript.
    6. <script>
       console.clear();
      const btns = document.getElementById("btn");
      const primaryDisplay = document.getElementById("display");
      const secondaryDisplay = document.getElementById("secondary-display");
      let currentString = '';
      let resultString = '';
      let evalResult;
      let lastOperator = false;
      const primaryRender = (value) => {
        primaryDisplay.innerText = value;
      }
      
      const secondaryRender = (value) => {
        secondaryDisplay.innerText = value;
      }
      secondaryRender('0');
      primaryRender('0');
      
      const evaluate = (e) => {
        let width = secondaryDisplay.scrollWidth;
        if(width > 310){
          secondaryDisplay.style.overflowX = 'scroll';
          secondaryDisplay.scrollLeft = width;
        }
        else{
          secondaryDisplay.style.overflowX = 'hidden';
        }
        
        let value = e.target.innerText;
        if (value >= '0' && value <= '9') {
          let len = currentString.length;
          let lastOp = isOp(currentString[len - 1]);
          if (lastOperator) {
            currentString = currentString.concat(value);
            resultString = "".concat(value);
            secondaryRender(currentString);
            primaryRender(resultString);
            lastOperator = false;
          } else {
            currentString = currentString.concat(value);
            resultString = resultString.concat(value);
            secondaryRender(currentString);
            primaryRender(resultString);
          }
      
        } else if (isOp(value)) {
          if (currentString.length == 0 && (value == '/' || value == '*'));
          else {
            resultString = "";
            primaryRender(value);
            lastOperator = true;
            let len = currentString.length;
            let lastOp = isOp(currentString[len - 1]);
            if (lastOp) {
              currentString = currentString.substr(0, len - 1) + value;
              console.log(currentString);
              secondaryRender(currentString);
              decimal = false;
            } else {
              currentString = currentString.concat(value);
              secondaryRender(currentString);
            }
          }
      
        } else if (value == '.') {
          if (resultString.indexOf('.') < 0) {
            if (resultString.length == 0) {
              currentString = currentString.concat('0.');
              resultString = resultString.concat('0.');
              secondaryRender(currentString);
              primaryRender(resultString);
            } else {
              currentString = currentString.concat(value);
              resultString = resultString.concat(value);
              secondaryRender(currentString);
              primaryRender(resultString);
            }
          }
        } else if (value == '=') {
          secondaryDisplay.style.overflowX = 'hidden';
          
          if (currentString.length == 0);
          else {
            let result = eval(currentString);
            resultString = '';
            currentString = '';
            secondaryRender('0');
            primaryRender(result);
          }
        } else if (value == 'clear') {
          currentString = '';
          resultString = '';
          secondaryRender('0');
          primaryRender('0');
        }
          let width1 = primaryDisplay.scrollWidth;
          if(width1 > 310){
            primaryDisplay.style.overflowX = 'scroll';
          }
          else{
            primaryDisplay.style.overflowX = 'hidden';
          }
      }
      
      
      //Adding Event Listener to all buttons
      for (let elem of btns.children) {
        elem.addEventListener('click', evaluate);
      }
      
      function isOp(value) {
        return (/\+|\-|\*|\//).test(value);
      }
      </script>

    That's All, Now you have successfully created the Calculator using html css and javascript. If you want more project like this feel free tell us by commenting down below.

    Term's of use !
    The templates or Scripts are for a personal use only. How personal can it get? Well, you are very much encouraged to download the template or Script of your choice and use it. But Personal means that you can’t make business out of our templates or Scripts. You are not allowed to sub-license, transfer, resell or republish any of the templates even for free.

    Conclusion

    Therefore, this is the How to Build a Calculator using Html CSS and JavaScript tutorial. I hope you will find this script useful. This script will work with All Browser. You can contact us or post a comment in the comment section if you encounter a problem or error.


    © Tech & Fun Zone

    About the Author

    Student | Blogger | Developer

    Post a Comment

    Cookie Consent
    We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
    Oops!
    It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.