Simple Calculator Code in HTML and JavaScript

Simple Calculator Code In HTML and JavaScript
Code Snippet:Calculator
Author: rose
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 1,302
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a basic calculator interface using HTML and JavaScript. It allows you to perform addition, subtraction, multiplication, and division calculations. The calculator’s main functionality is to display input and calculate results, providing a user-friendly tool for simple arithmetic operations.

You can integrate this calculator widget into your web project wherever basic calculations are required, offering users a convenient tool for performing arithmetic. Furthermore, you can customize the calculator interface with additional CSS according to your web/app theme.

How to Create Simple Calculator Code In HTML and JavaScript

1. First, insert the following HTML code into your project’s HTML file. This code provides the structure and user interface for the calculator.

<p class="m">Calculator</p>
  <div class="calculator">
    <div class="screen">0</div>
    <br />
    <div class="buttons">
      <button id="equals">=</button>
    <br />
    <button id="clear">CLEAR</button>

2. Copy the following CSS code into your project’s CSS file or include it in the HTML file using <style> tags. This CSS code ensures the calculator looks visually appealing.

@import url('');
@import url(';200;300;400;500;700&display=swap');
@import url(' Script&family=Source+Code+Pro');
@import url(';300;500;600;700&display=swap');
@import url('');

body {
    background: rgba(0, 0, 0, 0.955);
    background-attachment: fixed;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;

.calculator {
    background-color: rgb(0, 0, 0);
    border: solid rgb(44, 44, 44) 1px;
    padding: 20px;
    border-radius: 10px;

.screen {
    width: 224px;
    background-color: rgba(103, 103, 103, 0.294);
    border: solid rgb(44, 44, 44) 1px;
    font-size: 40px;
    text-align: end;
    padding: 4px;
    color: white;

.buttons {
    width: 232px;
    display: flex;
    flex-wrap: wrap;

button {
    font-family: "Black Ops One";
    background-color: rgb(44, 44, 44);
    border: none;
    color: rgb(255, 255, 255);
    font-size: 40px;
    width: 50px;
    height: 50px;
    border-radius: 20px;
    margin: 4px;

button:hover {
    background-color: rgb(111, 110, 110);

button:active {
    background-color: rgb(192, 190, 190);
    box-shadow: rgba(50, 50, 93, 0.25) 0px 30px 60px -12px inset, rgba(0, 0, 0, 0.3) 0px 18px 36px -18px inset;

#equals {
    background-color: rgb(252, 20, 27);

#equals:hover {
    background-color: rgba(198, 1, 1, 0.953);

#clear {
    width: 100%;
    background-color: rgb(44, 44, 44);
    font-size: 30px;

#clear:hover {
    background-color: rgb(255, 213, 61);
    color: black;

.m {
    font-family: "Black Ops One";
    margin-top: -550px;
    text-align: center;
    align-items: center;
    color: white;
    font-size: 50px;
    position: absolute;

3. Finally, add the following JavaScript code to your project. This code handles the calculator’s functionality, including button clicks and calculation logic.

const screenDisplay = document.querySelector('.screen')
const buttons = document.querySelectorAll('button')

let calculation = []
let accumlativeCalculation

function calculate(button) {
    const value = button.textContent

    if (value == "CLEAR") {
        calculation = []
        screenDisplay.textContent = '0'
    else if(value === "="){
        screenDisplay.textContent = eval(accumlativeCalculation)
    else {
        accumlativeCalculation = calculation.join('')
        screenDisplay.textContent = accumlativeCalculation


buttons.forEach(button => button.addEventListener('click', () => calculate(button)))

That’s all! You’ve successfully added a simple calculator widget to your web project. Users can now utilize this tool for basic arithmetic calculations directly on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link