Exnrt Logo
  • Home
  • Technology
    • Artificial Intelligence
    • WordPress
  • Programming
    ProgrammingShow More
    Mistral AI Model
    Mistral-7B Instruct Fine-Tuning using Transformers LoRa
    19 1
    Hugging Face Website
    Hugging Face Transformers Pipeline, what can they do?
    15 1
    AI generated images using SDXL-Lightning huggingface
    SDXL-Lightning model using hugging face Transformers
    14 1
    Gemma AI Model
    Finetune Gemma Models with Transformers
    11 1
    HTML Quiz App
    Quiz App Using HTML, CSS, and JavaScript
    9 1
  • Business
    • Ads
    • SEO
  • AI Tools
    • AI Chatbot For Education
    • Ask a Question
    • News Title Generator
  • My Feed
    • My Interests
    • My Saves
    • History
Notification
Sign In
ExnrtExnrtExnrt
Font ResizerAa
  • Artificial Intelligence
  • Technology
  • Business
  • Ads
  • SEO
Search
  • Blog
  • Ads
  • Programming
  • Technology
  • Artificial Intelligence
  • WordPress
  • SEO
  • Business
  • Education

Top Stories

Explore the latest updated news!
Fine Tuning Siglip2 a ViT on Image Classification Task.

Fine Tuning Siglip2 on Image Classification Task

6
AI-Generated-Image-using-Flux-1

How to Fine-Tune Flux.1 Using AI Toolkit

8
microsoft/Phi-3-mini-128k-instruct

How to fine-tune Microsoft/Phi-3-mini-128k-instruct

12

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
ProgrammingBlog

Simple Calculator Using Html, CSS, and JavaScript

Ateeq Azam
Last updated: October 6, 2023 6:50 pm
By Ateeq Azam Add a Comment 4
Share
Calculator Using Html, CSS, Javascript
Calculator Using Html, CSS, Javascript
SHARE

A calculator using HTML, CSS, and JavaScript is a web-based application that allows users to perform basic mathematical calculations directly in a web browser. It provides a user interface with buttons for numbers, operators (addition, subtraction, multiplication, division), and other functions (e.g., clear, equals), along with a display area to show the input and the calculated result.

Table of Content
Languages Contribution to develop a CalculatorHow a calculator implemented?Calculate Html, CSS & Javascript Code
Calculator
0

Languages Contribution to develop a Calculator

Here’s a breakdown of how each of these technologies contributes to building a calculator:

  1. HTML (HyperText Markup Language): HTML is used to create the structure of the calculator. It defines the layout, elements, and their positioning on the web page. In the case of a calculator, HTML is used to create the buttons for numbers and operators, as well as the display area to show the results.
  2. CSS (Cascading Style Sheets): CSS is used for styling and designing the calculator. It helps in defining the colors, fonts, spacing, and overall visual appearance of the calculator. In the context of a neumorphic calculator (as previously discussed), CSS can be used to create a visually appealing and user-friendly design.
  3. JavaScript: JavaScript is responsible for the functionality of the calculator. It handles user interactions, such as button clicks, and performs the actual calculations. JavaScript code can capture user input, manipulate it based on the chosen operation, and display the result in the designated area.

How a calculator implemented?

Here’s a high-level overview of how a calculator implemented with HTML, CSS, and JavaScript typically works:

  • Users click on number buttons to enter numeric values.
  • Users click on operator buttons (+, -, *, /) to choose the desired operation.
  • Users can click on additional buttons like “=” to calculate the result or “C” to clear the input.
  • JavaScript code captures the user’s input and performs the calculations accordingly.
  • The result is displayed in the designated display area on the web page.

The combination of these technologies allows developers to create interactive and user-friendly calculators that can be accessed and used directly from a web browser without the need for installing additional software.

Calculate Html, CSS & Javascript Code

HTML + CSS + JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin-top: 100px;
        }
        .calculator {
            width: 300px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2); /* Increase the shadow size */
            background-color: #292626;
            border-radius: 10px; /* Add border-radius for a neumorphic effect */
        }
        .result {
            font-size: 24px;
            margin-bottom: 10px;
            height: 40px;
            text-align: right;
            padding: 10px; /* Add padding for a neumorphic effect */
            background-color: #f5f5f5; /* Use a lighter background color */
            box-shadow: 0px 0px 10px rgba(0, 131, 253, 0.2); /* Add a subtle shadow */
            border-radius: 5px; /* Add border-radius for a neumorphic effect */
        }
        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
        }
        .button {
            padding: 10px;
            font-size: 18px;
            text-align: center;
            cursor: pointer;
            background-color: #f5f5f5; /* Use a lighter background color */
            color: #333; /* Use a darker text color */
            border: 1px solid #ccc; /* Add a subtle border */
            border-radius: 5px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2); /* Add a subtle shadow */
        }
        .button:hover {
            background-color: #eee; /* Slightly change the background color on hover */
        }
    </style>
</head>
<body>
    <div class="calculator">
        <div class="result" id="result">0</div>
        <div class="buttons">
            <button class="button" onclick="clearResult()">C</button>
            <button class="button" onclick="appendToResult('9')">9</button>
            <button class="button" onclick="appendToResult('8')">8</button>
            <button class="button" onclick="appendToResult('7')">7</button>
            <button class="button" onclick="appendToResult('6')">6</button>
            <button class="button" onclick="appendToResult('5')">5</button>
            <button class="button" onclick="appendToResult('4')">4</button>
            <button class="button" onclick="appendToResult('3')">3</button>
            <button class="button" onclick="appendToResult('2')">2</button>
            <button class="button" onclick="appendToResult('1')">1</button>
            <button class="button" onclick="appendToResult('0')">0</button>
            <button class="button" onclick="appendToResult('.')">.</button>
            <button class="button" onclick="appendToResult('+')">+</button>
            <button class="button" onclick="appendToResult('-')">-</button>
            <button class="button" onclick="appendToResult('*')">*</button>
            <button class="button" onclick="appendToResult('/')">/</button>
            <button class="button" onclick="calculateResult('=')">=</button>
        </div>
    </div>

    <script>
        let currentResult = '0';

        function updateResult() {
            document.getElementById('result').textContent = currentResult;
        }

        function appendToResult(value) {
            if (currentResult === '0' && value !== '.') {
                currentResult = value;
            } else {
                currentResult += value;
            }
            updateResult();
        }

        function calculateResult() {
            try {
                currentResult = eval(currentResult).toString();
            } catch (error) {
                currentResult = 'Error';
            }
            updateResult();
        }

        function clearResult() {
            currentResult = '0';
            updateResult();
        }
    </script>
</body>
</html>

Let’s break down the provided code step by step:

  1. HTML Head (Lines 1-52):
    • This code starts with the standard HTML document structure.
    • It sets the document’s character set to UTF-8 and defines the viewport for responsive design.
    • The title of the web page is set to “Calculator.”
  2. CSS Styles (Lines 7-50):
    • CSS rules are defined to style the calculator and its components. These styles create a neumorphic (soft and tactile) design for the calculator.
    • Various CSS properties such as box-shadow, border-radius, and background-color are used to achieve the neumorphic effect.
    • The calculator is centered on the page using margin and width properties.
  3. HTML Content (Lines 53-74):
    • The calculator’s user interface is defined using HTML elements.
    • Inside a <div> element with the class “calculator,” you have two main components:
      • The result display is a <div> with the class “result” and the initial content of “0.” It is right-aligned and styled for the neumorphic effect.
      • The buttons are contained in a <div> with the class “buttons.” It uses CSS Grid (display: grid) to create a grid of buttons (16 buttons in total).
      • Each button is a <button> element with the class “button.” The buttons are labeled with numbers (0-9), operators (+, -, *, /), and special functions (C for clear and = for calculation).
  4. JavaScript (Lines 76-105):
    • JavaScript code is included within a <script> block at the end of the HTML body.
    • A variable currentResult is declared and initialized to ‘0’. This variable stores the current input and result of calculations.
    • Four JavaScript functions are defined:
      • updateResult(): This function updates the content of the result display based on the currentResult variable.
      • appendToResult(value): This function appends the provided value to the currentResult. It handles the user’s input and updates the result display accordingly.
      • calculateResult(): This function evaluates the current input using the eval() function and updates currentResult with the calculated result. It also handles potential errors and displays “Error” if an invalid expression is entered.
      • clearResult(): This function clears the currentResult and sets it back to ‘0’.
    • Event listeners for button clicks are added directly in the HTML buttons using the onclick attribute. When a button is clicked, it invokes the corresponding JavaScript function.

The combined HTML, CSS, and JavaScript code creates a functional neumorphic calculator with a user-friendly interface. Users can input numbers and perform basic calculations, and the results are displayed on the calculator’s screen. The CSS styles provide a visually appealing and tactile design.

TAGGED:Programming
Share This Article
Facebook Twitter Copy Link Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Leave a comment
Subscribe
Login
Notify of
guest

guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

You Might Also Like

Fine Tuning Siglip2 a ViT on Image Classification Task.
Fine Tuning Siglip2 on Image Classification Task
AI-Generated-Image-using-Flux-1
How to Fine-Tune Flux.1 Using AI Toolkit
microsoft/Phi-3-mini-128k-instruct
How to fine-tune Microsoft/Phi-3-mini-128k-instruct
AI Generated: A professional real llama looking like a hacker in a dark lab with light yellow lights
How to Fine-tune Meta Llama-3 8B

Other Posts

Bard AI Image Generation
Google Bard Now Offers Free AI Image Generation
Artificial Intelligence Blog Technology
HTML Quiz App
Quiz App Using HTML, CSS, and JavaScript
Programming Blog
Ezoic for Publishers
Ezoic: Best Ads Network for Small Publishers
Ads Blog
best ecommerce WordPress theme
10 Best eCommerce WordPress Themes Free & Paid
WordPress Blog

Latest Posts

Uncover the Latest stories that related to your interest!
Fine Tuning Siglip2 a ViT on Image Classification Task.
Artificial IntelligenceBlog

Fine Tuning Siglip2 on Image Classification Task

April 14, 2025

At Exnrt.com, we believe in empowering computer science students with the knowledge and skills they need to succeed in their careers. Our goal is to provide accessible and engaging tutorials that help students and professionals develop their skills and advance their careers.

  • Categories:
  • Business
  • Technology
  • Ads
  • SEO

Quick Links

  • Blog
  • Technology
  • Artificial Intelligence
  • Business

About US

  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2024 All Rights Reserved – Exnrt by ateeq.pk

wpDiscuz
Welcome Back!

Sign in to your account

Register Lost your password?