Log in Register Dashboard Temp Share Shortlinks Frames API

HTMLify

hello.html
Views: 66 | Author: dakshbadal1379
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Calculator</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background-color: black;
            font-family: "Poppins", sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 10px;
        }

        /* Main Calculator Box */
        .main-calc-container {
            width: 90%;
            max-width: 300px;
            background-color: white;
            border: 3px solid black;
            border-radius: 20px;
            padding: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.4);
        }

        /* Display Area */
        .display-part-res {
            background-color: rgb(231, 221, 221);
            height: 50px;
            width: 100%;
            border-radius: 12px;
            box-shadow: inset 2px 2px 5px rgba(0,0,0,0.1);
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            justify-content: flex-end;
            padding-right: 15px;
            overflow-x: auto;
            word-wrap: break-word;
        }

        /* Buttons Section */
        .button-sec-part {
            margin-top: 15px;
            background-color: antiquewhite;
            border-radius: 15px;
            padding: 8px 0;
        }

        /* Common button layout */
        .button-sec-part div {
            display: flex;
            justify-content: space-around;
            align-items: center;
            margin: 6px 0;
            flex-wrap: wrap;
        }

        .button-sec-part button {
            width: 45px;
            height: 45px;
            background-color: white;
            border: none;
            border-radius: 50%;
            font-weight: 900;
            font-size: 1rem;
            transition: 0.2s;
        }

        .button-sec-part button:hover {
            background-color: rgb(240, 240, 240);
            transform: scale(1.05);
        }

        /* Tablets */
        @media (max-width: 768px) {
            .main-calc-container {
                max-width: 260px;
            }

            .button-sec-part button {
                width: 42px;
                height: 42px;
                font-size: 0.95rem;
            }
        }

        /* Mobiles */
        @media (max-width: 480px) {
            body {
                align-items: flex-start;
                padding-top: 30px;
            }

            .main-calc-container {
                width: 95%;
                max-width: 240px;
            }

            .display-part-res {
                height: 45px;
                font-size: 1.2rem;
            }

            .button-sec-part button {
                width: 40px;
                height: 40px;
                font-size: 0.9rem;
            }
        }
    </style>
</head>
<body>
    <div class="main-calc-container">
        <div class="display-part-res" id="display"></div>
        <div class="button-sec-part">

            <div class="first-column">
                <button>AC</button>
                <button>DEL</button>
                <button>%</button>
                <button>/</button>
            </div>

            <div class="second-column">
                <button>7</button>
                <button>8</button>
                <button>9</button>
                <button>*</button>
            </div>

            <div class="third-column">
                <button>4</button>
                <button>5</button>
                <button>6</button>
                <button>-</button>
            </div>

            <div class="fourth-column">
                <button>1</button>
                <button>2</button>
                <button>3</button>
                <button>+</button>
            </div>

            <div class="fifth-column">
                <button>+/-</button>
                <button>0</button>
                <button>.</button>
                <button>=</button>
            </div>
        </div>
    </div>

    <!-- ✅ JavaScript Section -->
    <script>
        const display = document.getElementById('display');
        const buttons = document.querySelectorAll('button');
        let currentInput = '';

        buttons.forEach(button => {
            button.addEventListener('click', () => {
                const value = button.textContent;

                if (value === 'AC') {
                    currentInput = '';
                } 
                else if (value === 'DEL') {
                    currentInput = currentInput.slice(0, -1);
                } 
                else if (value === '=') {
                    try {
                        currentInput = eval(currentInput.replace('%', '/100')).toString();
                    } catch {
                        currentInput = 'Error';
                    }
                } 
                else if (value === '+/-') {
                    if (currentInput) {
                        if (currentInput.startsWith('-')) {
                            currentInput = currentInput.slice(1);
                        } else {
                            currentInput = '-' + currentInput;
                        }
                    }
                }
                else {
                    currentInput += value;
                }

                display.textContent = currentInput || '0';
            });
        });
    </script>
</body>
</html>

Comments