Lädt...


🔧 How to Make a Calculator Using HTML CSS JavaScript – Step-by-Step Guide


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Calculators are an essential tool used in various fields, from simple arithmetic calculations to complex scientific computations. Building a calculator from scratch using HTML, CSS, and JavaScript not only enhances your coding skills but also deepens your understanding of how these technologies work together. This guide will walk you through creating a basic calculator that can perform addition, subtraction, multiplication, and division.

HTML Structure:
The HTML includes a div with the class calculator containing the calculator’s display and buttons.
Each button has data attributes (data-num for numbers and data-op for operations) for easy JavaScript selection.
CSS Styling:
The CSS styles center the calculator on the screen, style the display and buttons, and provide hover effects.
Specific styles are added for the equals button to differentiate it from the others.
JavaScript Functionality:
JavaScript handles the button clicks, updates the display, and performs calculations.
The handleNumber function processes number inputs and decimal points.
The handleOperator function processes operations, including clear, equals, and arithmetic operations.
The calculate function performs the arithmetic based on the selected operator.
The updateDisplay function updates the display with the current input.


<!DOCTYPE html>
<html>

<head>
    <title>iPhone Style Calculator with History and Root Button</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.6.4/math.js"
        integrity="sha512-BbVEDjbqdN3Eow8+empLMrJlxXRj5nEitiCAK5A1pUr66+jLVejo3PmjIaucRnjlB0P9R3rBUs3g5jXc8ti+fQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.6.4/math.min.js"
        integrity="sha512-iphNRh6dPbeuPGIrQbCdbBF/qcqadKWLa35YPVfMZMHBSI6PLJh1om2xCTWhpVpmUyb4IvVS9iYnnYMkleVXLA=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>

    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #fff;
            margin: 0;
            font-family: 'Arial', sans-serif;
        }

        .calculator {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
            padding: 20px;
            border-radius: 20px;
            background-color: #333;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            width: 320px;
        }

        .calculator input[type="text"] {
            grid-column: span 4;
            padding: 20px;
            font-size: 32px;
            border: none;
            border-radius: 10px;
            text-align: right;
            background-color: #000;
            color: #fff;
            box-sizing: border-box;
        }

        .calculator input[type="button"] {
            padding: 20px;
            font-size: 24px;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            color: white;
            box-sizing: border-box;
        }

        .calculator input[type="button"].operator {
            background-color: #ff9500;
        }

        .calculator input[type="button"].operator:hover {
            background-color: #cc7a00;
        }

        .calculator input[type="button"].number {
            background-color: #505050;
        }

        .calculator input[type="button"].number:hover {
            background-color: #6b6b6b;
        }

        .calculator input[type="button"].zero {
            grid-column: span 2;
        }

        .calculator input[type="button"].clear {
            background-color: #d4d4d2;
            color: black;
        }

        .calculator input[type="button"].clear:hover {
            background-color: #bbb; transition: 0.5s;
        } 

        .calculator input[type="button"].root {
            background-color: #f7c600;  
        }

        .calculator input[type="button"].root:hover {
            background-color: #e6b800; transition: 0.5s;
        }
    </style>
</head>

<body>

    <div class="calculator">
        <input type="text" id="display" readonly>
        <input type="button" value="A/C" class="clear" onclick="clr()" />
        <input type="button" value="%" class="operator" onclick="dis('%')" />
        <input type="button" value="/" class="operator" onclick="dis('/')" />
        <input type="button" value="*" class="operator" onclick="dis('*')" />
        <input type="button" value="7" class="number" onclick="dis('7')" />
        <input type="button" value="8" class="number" onclick="dis('8')" />
        <input type="button" value="9" class="number" onclick="dis('9')" />
        <input type="button" value="-" class="operator" onclick="dis('-')" />
        <input type="button" value="4" class="number" onclick="dis('4')" />
        <input type="button" value="5" class="number" onclick="dis('5')" />
        <input type="button" value="6" class="number" onclick="dis('6')" />
        <input type="button" value="+" class="operator" onclick="dis('+')" />
        <input type="button" value="1" class="number" onclick="dis('1')" />
        <input type="button" value="2" class="number" onclick="dis('2')" />
        <input type="button" value="3" class="number" onclick="dis('3')" />

<input type="button" value="=" class="operator" onclick="solve()
" />


        <input type="button" value="0" class="number zero" onclick="dis('0')" />
        <input type="button" value="." class="number" onclick="dis('.')" />
        <input type="button" value="√" class="root" onclick="dis('sqrt(')" />



    </div>

    <script>
        function dis(val) {
            document.getElementById("display").value += val;
        }

        document.addEventListener('keydown', function (event) {
            const key = event.key;
            if ((key >= '0' && key <= '9') || ['+', '-', '*', '/', '%'].includes(key)) {
                dis(key);
            } else if (key === 'Enter') {
                solve();
            } else if (key === 'Escape') {
                clr();
            }
        });

        function solve() {
            try {
                let expression = document.getElementById("display").value;
                // Handle square root calculation
                expression = expression.replace(/sqrt\(([^)]+)\)/g, 'sqrt($1)');
                // Evaluate the expression
                let result = math.evaluate(expression);
                document.getElementById("display").value = result;
            } catch (err) {
                document.getElementById("display").value = "Error";
            }
        }

        function clr() {
            document.getElementById("display").value = "";
        }
    </script>
</body>

</html>

...

🔧 How To Create a Calculator Using HTML CSS & JavaScript | Simple Calculator in JavaScript


📈 58.76 Punkte
🔧 Programmierung

🔧 Age Calculator Using HTML, CSS, and JavaScript: A Beginner’s Guide


📈 43.58 Punkte
🔧 Programmierung

🔧 Introduction of CSS, What is CSS, Why we use CSS and How CSS describe the HTML elements


📈 37.27 Punkte
🔧 Programmierung

🔧 Build a Calculator using HTML and JavaScript: A Hands-On Guide


📈 35.42 Punkte
🔧 Programmierung

🔧 How to make a clock using html , JavaScript and CSS and deploy it using firebase


📈 35.15 Punkte
🔧 Programmierung

🔧 Building a BMI Calculator with HTML, CSS, and JavaScript


📈 33.45 Punkte
🔧 Programmierung

🔧 Building a Calculator with HTML, CSS, and JavaScript


📈 33.45 Punkte
🔧 Programmierung

🔧 Fractions Calculator With HTML, CSS and JavaScript


📈 33.45 Punkte
🔧 Programmierung

🔧 Beginner Calculator Project: html, javaScript & css


📈 33.45 Punkte
🔧 Programmierung

🔧 Build a Tic Tac Toe Game using HTML, CSS, JavaScript, Tailwind CSS and Canvas Confetti


📈 32.02 Punkte
🔧 Programmierung

🔧 Calculator using html css


📈 31.66 Punkte
🔧 Programmierung

🔧 How To Make A Music Player Using HTML CSS And JavaScript


📈 30.5 Punkte
🔧 Programmierung

🔧 How to Make Responsive eSports Website Using HTML CSS JavaScript


📈 30.5 Punkte
🔧 Programmierung

🔧 How to Make a Restaurant Website Using HTML CSS JavaScript


📈 30.5 Punkte
🔧 Programmierung

🔧 Cursor Trail using html css js #virals #js #html #css #work


📈 30.21 Punkte
🔧 Programmierung

🕵️ CVE-2024-8864 | composiohq composio up to 0.5.6 calculator.py Calculator code injection


📈 28.47 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2024-39173 | calculator-boilerplate 1.0 /routes/calculator.js eval input injection


📈 28.47 Punkte
🕵️ Sicherheitslücken

🐧 Microsoft Calculator (UWP). Telemetry in a freaking calculator....


📈 28.47 Punkte
🐧 Linux Tipps

🪟 Is the best calculator on Android a port of the Windows Calculator?


📈 28.47 Punkte
🪟 Windows Tipps

🔧 JavaScript more efficient and readable code for make a calculator with only 20 lines with


📈 27.31 Punkte
🔧 Programmierung

🔧 Name calculator HTML CSS js


📈 27.02 Punkte
🔧 Programmierung

🔧 New Style Love Calculator HTML CSS Js


📈 27.02 Punkte
🔧 Programmierung

🔧 Building a Calculator App with JavaScript: A Beginner's Guide


📈 26.15 Punkte
🔧 Programmierung

🍏 OrgChart JS 8.00.25 - Make neat flowcharts using HTML5, CSS, and JavaScript.


📈 25.88 Punkte
🍏 iOS / Mac OS

🔧 How to Make TikTok Logo By HTML, CSS, JavaScript


📈 25.86 Punkte
🔧 Programmierung

🔧 📫How To Make A Popup | HTML, CSS & JavaScript


📈 25.86 Punkte
🔧 Programmierung

🔧 A practical way to test HTML and CSS in real-time using only CSS.


📈 25.59 Punkte
🔧 Programmierung

🔧 A practical way to test HTML and CSS in real-time using only CSS.


📈 25.59 Punkte
🔧 Programmierung

🔧 Complete Website using HTML And CSS | CSS Project


📈 25.59 Punkte
🔧 Programmierung

🔧 Creating an Information Bot: A Beginner's Guide (HTML/CSS, JavaScript, Gemini API)


📈 24.7 Punkte
🔧 Programmierung

🔧 Choosing the Right CSS Approach: Tailwind CSS vs Bootstrap vs Vanilla CSS


📈 24.49 Punkte
🔧 Programmierung

🔧 A BRIEF REVIEW OF CSS CASCADING, CSS SELECTORS and CSS SPECIFICITY.


📈 24.49 Punkte
🔧 Programmierung

🔧 Stylify CSS: Automagic CSS bundles splitting into CSS layers in Astro.build


📈 24.49 Punkte
🔧 Programmierung

🔧 CSS Grid: Moving From CSS Frameworks To CSS Grid (2018 and beyond)


📈 24.49 Punkte
🔧 Programmierung

🔧 How to make Image rotate on hover using HTML and CSS


📈 24.07 Punkte
🔧 Programmierung

matomo