html code with css for calculator



copy this html code


Calculator
<!DOCTYPE html>
<html>
<head>
  <title>Calculator</title>
  <style>
    body {
      font-family: Arial, sans-serif;
    }

    .calculator {
      width: 200px;
      background-color: #f2f2f2;
      padding: 10px;
      border-radius: 5px;
      margin: 0 auto;
    }

    .calculator input[type="text"] {
      width: 100%;
      margin-bottom: 10px;
      padding: 5px;
      font-size: 18px;
    }

    .calculator .buttons {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-gap: 5px;
    }

    .calculator button {
      padding: 10px;
      font-size: 16px;
      background-color: #e0e0e0;
      border: none;
      cursor: pointer;
    }

    .calculator button:hover {
      background-color: #d0d0d0;
    }

    .calculator .equal-button {
      grid-column: span 2;
    }

    .calculator .clear-button {
      grid-column: span 3;
    }
  </style>
</head>
<body>
  <div class="calculator">
    <input type="text" id="result" readonly>
    <div class="buttons">
      <button onclick="addToScreen('7')">7</button>
      <button onclick="addToScreen('8')">8</button>
      <button onclick="addToScreen('9')">9</button>
      <button onclick="addToScreen('/')">/</button>
      <button onclick="addToScreen('4')">4</button>
      <button onclick="addToScreen('5')">5</button>
      <button onclick="addToScreen('6')">6</button>
      <button onclick="addToScreen('*')">*</button>
      <button onclick="addToScreen('1')">1</button>
      <button onclick="addToScreen('2')">2</button>
      <button onclick="addToScreen('3')">3</button>
      <button onclick="addToScreen('-')">-</button>
      <button onclick="addToScreen('0')">0</button>
      <button onclick="addToScreen('.')">.</button>
      <button onclick="calculate()">=</button>
      <button onclick="addToScreen('+')">+</button>
      <button onclick="clearScreen()" class="clear-button">C</button>
    </div>
  </div>

  <script>
    function addToScreen(value) {
      document.getElementById('result').value += value;
    }

    function calculate() {
      var result = eval(document.getElementById('result').value);
      document.getElementById('result').value = result;
    }

    function clearScreen() {
      document.getElementById('result').value = '';
    }
  </script>
</body>
</html>