simple calculator html code example

Example 1: calculator in html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Calculator </title>
  </head>

  <!--CSS File-->

  	* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #fffbd5; /* fallback for old browsers */
  background: -webkit-linear-gradient(
    to right,
    #b20a2c,
    #fffbd5
  ); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(
    to right,
    #b20a2c,
    #fffbd5
  ); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
.container-first {
  position: relative;
  background: #0b8000;
  border-radius: 6px;
  overflow: hidden;
  z-index: 10;
}
.container-first .calculator {
  position: relative;
  display: grid;
}
.container-first .calculator .value {
  grid-column: span 4;
  height: 140px;
  width: 300px;

  border: none;
  padding-left: 1em;

  font-size: 2.5em;

  color: gold;
}
.container-first .calculator span {
  display: grid;
  height: 4.5em;
  font-weight: 900;
  color: rgb(255, 255, 255);
  text-align: center;
  align-items: center;

  font-size: 20px;
  user-select: none;
  border-bottom: 4px solid rgba(255, 255, 255, 0.07);
  border-right: 4px solid rgba(255, 255, 255, 0.07);
}
input[type="text"] {
  color: rgb(255, 136, 0);
  background-color: rgb(156, 40, 40);
}
  <body>
    <div class="container-first">
      <form name="val" class="calculator">
        <input type="text" class="value" name="ans" />
        <span onclick="document.val.ans.value+='4'">4</span>
        <span onclick="document.val.ans.value+='5'">5</span>
        <span onclick="document.val.ans.value+='6'">6</span>


        <span onclick="document.val.ans.value =eval(document.val.ans.value)">
          =
        </span>


        <span onclick="document.val.ans.value+='7'">7</span>
        <span onclick="document.val.ans.value+='8'">8</span>
        <span onclick="document.val.ans.value+='9'">9</span>
        <span onclick="document.val.ans.value+='-'">-</span>

        <span onclick="document.val.ans.value+='1'">1</span>
        <span onclick="document.val.ans.value+='2'">2</span>
        <span onclick="document.val.ans.value+='3'">3</span>

        <span onclick="document.val.ans.value+='/'">/</span>

        <span onclick="document.val.ans.value+='0'">0</span>
        <span onclick="document.val.ans.value=''">C</span>
        <span onclick="document.val.ans.value+='*'">*</span>

        <span onclick="document.val.ans.value+='00'">00</span>

        <span onclick="document.val.ans.value+='+'">+</span>
      </form>
    </div>
  </body>
</html>

Example 2: how to create a simple calculator in html

<!DOCTYPE html>
<html lang="en">
<head>
<style>
.contain{
display: flex;
}
#display{
  width: 98%;
  
}
.mathButtons{
  width: 100px;
  height: 40px;
  background-color: rgb(0,0,0,0.3);
  color: white;
}
.mathButtons:hover{
  background-color: DodgerBlue;
  
  }
  #clearButton:hover{
    background-color: orangered;
  }
  #container{
  
    width: 430px;
    height: 300px;

    }
    
    
    .buttondigits{
      background-color:  black;
      color: white;
      height: 40px;
      width: 100px;
    }
    #clearButton{
      width: 100px;
      height: 40px;
      background-color: rgb(0,0,0,0.3);
      font-weight: bold;
    }
	.buttondigits:hover {
background-color: gray;
}

</style>
<title> standard calculator </title>
<meta charset="utf-8">
<link rel="stylesheet" href="cal.css"/>
</head>
<body>
  
     <div class="container">
	<fieldset id="container">
	  <form name="calculator">
<input type="text" id="display" name="display" readonly>
<br>
<br>
<br>
<div class="contain">
<input class="buttondigits" type="button" value="7" onclick="calculator.display.value += '7'">
<input class="buttondigits" type="button" value="8" onclick="calculator.display.value += '8'">
<input class="buttondigits" type="button" value="9" onclick="calculator.display.value += '9'">
<input class="mathButtons" type="button" value="+" onclick="calculator.display.value += '+'">
</div>

<div class="contain">
<input class="buttondigits" type="button" value="4" onclick="calculator.display.value += '4'">
<input class="buttondigits" type="button" value="5" onclick="calculator.display.value += '5'">
<input class="buttondigits" type="button" value="6" onclick="calculator.display.value += '6'">
<input class="mathButtons" type="button" value="-" onclick="calculator.display.value += '-'">
</div>

<div class="contain">
<input class="buttondigits" type="button" value="1" onclick="calculator.display.value += '1'">
<input class="buttondigits" type="button" value="2" onclick="calculator.display.value += '2'">
<input class="buttondigits" type="button" value="3" onclick="calculator.display.value += '3'">
<input class="mathButtons" type="button" value="*" onclick="calculator.display.value += '*'">
</div>

<div class="contain">
<input id="clearButton" class="button" type="button" value="C" onclick="calculator.display.value = ''">
<input class="buttondigits" type="button" value="0" onclick="calculator.display.value += '0'">
<input class= "mathButtons" type="button" value="=" onclick="calculator.display.value = eval(calculator.display.value)">
<input class="mathButtons" type="button" value="/" onclick="calculator.display.value += '/'">
</div>
</form> 
</fieldset>
</div>
</body>
</html>

Example 3: What is the code for a calculator

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Calculator Made by Sanjith </title>
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
<style>
    * {
        font-family: 'Inconsolata', monospace;
        color: rgb(0, 0, 0);
    }
    body {
        background: linear-gradient(to right, rgb(24, 184, 168), rgb(118, 190, 50));;
    }
    .container {
        width: 320px;
        background: linear-gradient(to right, rgb(173, 58, 173), rgb(184, 63, 26));;
        margin: 120px auto;
    }
    table {
        width: 100%;
        border-color: #09646407;
        background: linear-gradient(to right, rgb(184, 24, 37), rgb(78, 13, 230));;
    }
    td {
        width: 25%;
        border-color: rgba(0, 0, 0, 0.363);
    }
    button {
        width: 100%;
        height: 70px;
        font-size: 24px;
        background: linear-gradient(to right, rgb(151, 11, 151), rgb(190, 94, 50));;      
        border: none;
    }
    #inputLabel {
        height: 120px;
        font-size: 40px;
        vertical-align: bottom;
        text-align: right;
        padding-right: 16px;
        background: linear-gradient(to right, rgb(4, 65, 88), rgb(226, 68, 47));;
    }
</style>
</head>
<body>
<div class="container">
    <table border="1" cellspacing="0">
        <tr>
            <td colspan="4" id="inputLabel">0</td>
        </tr>
        <tr>
            <td colspan="3"><button onclick="pushBtn(this);">AC</button></td>
            <td><button onclick="pushBtn(this);">/</button></td>
        </tr>
        <tr>
            <td><button onclick="pushBtn(this);">7</button></td>
            <td><button onclick="pushBtn(this);">8</button></td>
            <td><button onclick="pushBtn(this);">9</button></td>
            <td><button onclick="pushBtn(this);">*</button></td>
        </tr>
        <tr>
            <td><button onclick="pushBtn(this);">4</button></td>
            <td><button onclick="pushBtn(this);">5</button></td>
            <td><button onclick="pushBtn(this);">6</button></td>
            <td><button onclick="pushBtn(this);">-</button></td>
        </tr>
        <tr>
            <td><button onclick="pushBtn(this);">1</button></td>
            <td><button onclick="pushBtn(this);">2</button></td>
            <td><button onclick="pushBtn(this);">3</button></td>
            <td><button onclick="pushBtn(this);">+</button></td>
        </tr>
        <tr>
            <td colspan="2"><button onclick="pushBtn(this);">0</button></td>
            <td><button onclick="pushBtn(this);">.</button></td>
            <td><button onclick="pushBtn(this);">=</button></td>
        </tr>
    </table>
</div>
 
<script>
    var inputLabel = document.getElementById('inputLabel');
     
    function pushBtn(obj) {
         
        var pushed = obj.innerHTML;
         
        if (pushed == '=') {
            // Calculate
            inputLabel.innerHTML = eval(inputLabel.innerHTML);
             
        } else if (pushed == 'AC') {
            // All Clear
            inputLabel.innerHTML = '0';
             
        } else {
            if (inputLabel.innerHTML == '0') {
                inputLabel.innerHTML = pushed;
                 
            } else {
                inputLabel.innerHTML += pushed;   
            }
        }
    }
</script>
</body>
</html>

Example 4: how to make a calculator website HTML

<body>
  
   <center>
    <h1 class="text-danger">Calculator</h1> 
    <p class="q">Type in the numbers to be added, separate them with commas (,). If 'Invalid' shows, you have not used a number, please clear the box and type numbers</p>
   </center>

    <center>
    <h3 style="font-family: Lucida Sans; color: lightseagreen">Add</h3>
    <input class="addinput" type="text">
   <button class="add" style="font-family: Lucida Sans; background-color: lightsalmon">ADD</button>
   <p class="p" style="background-color:lightpink">Sum is <span class="result"></span></p>
   </center>
 
<center>
    <h3 style="font-family: Lucida Sans; color: lightseagreen">Multiply</h3>
  <input class="multiplyinput" type="text">
  <button class="multiply" style="font-family: Lucida Sans; background-color: lightsalmon">MULTIPLY</button>
   <p class="p" style="background-color:lightpink"></p>>Product is <span class="product"></span></p>
</center>

<center>
        <h3 style="font-family: Lucida Sans; color: lightseagreen">Subtract</h3>
    <input class="subtractinput" type="text">
    <button class="subtract" style="font-family: Lucida Sans; background-color: lightsalmon">SUBTRACT</button>
     <p class="p" style="background-color:lightpink"></p>>Difference is <span class="difference"></span></p>
  </center>

  <center>
        <h3 style="font-family: Lucida Sans; color: lightseagreen">Divide</h3>
    <input class="divideinput" type="text">
    <button class="divide" style="font-family: Lucida Sans; background-color: lightsalmon">DIVIDE</button>
     <p class="p" style="background-color:lightpink"></p>>Quotient is <span class="quotient"></span></p>
  </center>
  
   <script src="calculator.js"></script>

</body>

Tags:

Misc Example