Home > Web Front-end > CSS Tutorial > BUILDING A SIMPLE WEB-BASED CALCULATOR: Step-by-step Guild with Html CSS And JavaScript

BUILDING A SIMPLE WEB-BASED CALCULATOR: Step-by-step Guild with Html CSS And JavaScript

Patricia Arquette
Release: 2024-11-11 14:20:02
Original
557 people have browsed it

BUILDING A SIMPLE WEB-BASED CALCULATOR: Step-by-step Guild with Html CSS And JavaScript

Creating a calculator web application is a fantastic project for learning HTML, CSS, and JavaScript. Even though calculators are commonplace or rather ordinary , building one from scratch helps beginners understand fundamental concepts of web development—such as structuring content with HTML, styling elements with CSS, and adding interactive functionality with JavaScript.

In this overview, we’ll walk through every part of the code needed to create a fully functional calculator. This guide will not only provide the code but will also explain each line in detail, ensuring you understand how everything fits together. By the end of this project, you’ll have a smooth, interactive calculator that you can personalize or even expand upon with more advanced features.

The Calculator’s Features
This calculator includes basic functionality:

A display area to show the current input and results.

Number buttons (0–9) and an additional "00" button.

Arithmetic operation buttons: addition ( ), subtraction (-), multiplication (*), and division (/).

Special buttons:

  • AC to clear the current input.

  • DEL to delete the last character.

  • /- to toggle between positive and negative numbers.

  • = to evaluate the expression and show the result.

With this project, you’ll learn how to:

  • Create a user interface with HTML.

  • Style elements using CSS to improve the visual appeal.

  • Implement calculator logic using JavaScript to handle button
    interactions and perform calculations.

STEP 1: HTML STRUCTURE - BUILDING THE CALCULATOR'S LAYOUT

The HTML code provides the foundational structure for our calculator. In this part, we define the elements that will make up our calculator, such as buttons and a display area, you can use any editor of your choice for this effect, I personally prefer Visual studio code. Here’s the complete HTML code for the calculator:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div>



<p>Explanation</p>

<p>Document Type and Language Declaration:<br>
</p>

<pre class="brush:php;toolbar:false"><!DOCTYPE html>
Copy after login
Copy after login

Tells the browser that this is an HTML5 document.

<html lang="en">
Copy after login
Copy after login

Begins the HTML document and specifies English as the language. This helps search engines and screen readers understand the language of the document.

Head Section:

<head>
Copy after login
Copy after login

Contains metadata and links significant to the document but not visible to the user.

<meta charset="UTF-8">
Copy after login
Copy after login

Sets the character encoding, ensuring compatibility with most languages.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
Copy after login
Copy after login

Makes the page responsive by adjusting its layout to fit different devices.

<title>Calculator</title>
Copy after login
Copy after login

Sets the title displayed on the browser tab.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div>



<p>Explanation</p>

<p>Document Type and Language Declaration:<br>
</p>

<pre class="brush:php;toolbar:false"><!DOCTYPE html>
Copy after login
Copy after login

Links to the CSS file where styles are defined.

Calculator Layout:

<html lang="en">
Copy after login
Copy after login

Links to the JavaScript file that handles the calculator’s functionality.

STEP 2: CSS STYLING - DESIGNING THE CALCULATOR INTERFACE

Now that we have the structure, let’s move to the styling. This CSS code will make the calculator look more modern, adding colors, rounded buttons, shadows, and responsive layout adjustments.

<head>
Copy after login
Copy after login

Explanation

Basic Reset and Font:

<meta charset="UTF-8">
Copy after login
Copy after login

Removes default padding and margins, sets box-sizing to border-box for consistent sizing, and applies a modern font.

Body Styling:

body: Uses Flexbox to center the calculator container in the middle of the screen and applies a gradient background.

Calculator Container:

.calculator: Adds padding, rounded corners, and a shadow for a neat, card-like appearance.

Display Input:

input: This styling gives the display area a large font size and right alignment, resembling a real calculator display.

Button Styling:

.calculator button: Sets up a circular button with a shadow effect, white text color, and spacing for alignment.

.actionbtn, .clearbtn, and .enter: Styles for specific buttons to make them stand out (e.g., green for operators, red for clear, and orange for equal).

STEP 3: THIS IS WHERE ALL THE JAVASCRIPT LOGIC HAPPENS - MAKING THE CALCULATOR FUNCTIONAL

With structure and style well accomplished, lets add a functionality using JavaScript. This script allows us to handle click on buttons, perform arithmetic, and display results.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
Copy after login
Copy after login

Explanation

Event Listener for Page Load:

<title>Calculator</title>
Copy after login
Copy after login

Ensures the script runs after all HTML content is loaded.

Input and Button Variables:

<link rel="stylesheet" href="style.css">
Copy after login

Selects the display area.

<div>



<p>Wraps the entire calculator interface. We’ll apply styles to this container to make it look like a calculator.<br>
</p>

<pre class="brush:php;toolbar:false"><input type="text" placeholder="0">



<p>This is the display area of the calculator, where we show the current number or result. It is disabled, so users can’t type directly.</p>

<p>Buttons:<br>
</p>

<pre class="brush:php;toolbar:false"><button>



<p>Clears the calculator display and resets the current calculation.<br>
</p>

<pre class="brush:php;toolbar:false"><button>



<p>Deletes the last entered character.<br>
</p>

<pre class="brush:php;toolbar:false"><button>



<p>Toggles between positive and negative values.<br>
</p>

<pre class="brush:php;toolbar:false"><button>



<p>The division operator.</p>

<p>Remaining button elements represent numbers (0–9), operators (+, -, *, /), and a decimal point (.). The = button (class="enter") is used to evaluate the expression.</p>

<p>JavaScript File:<br>
</p>

<pre class="brush:php;toolbar:false"><script src="script.js"></script>
Copy after login

Collects all buttons in an array for easy manipulation.

Button Click Events:

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'poppins', sans-serif;
}
body{
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(45deg, #0a0a0a, #3a4452);
}
.calculator{
    border: 1px solid #a2a2a2;
    padding: 20px;
    border-radius: 20px;
    background: transparent;
    box-shadow: 0px 3px 15px rgba(113, 115, 119, 0.5);
}
input{
    width: 272px;
    min-height: 100px;
    border: none;  
    padding: 5px;
    margin: 10px;
    background: transparent;
    font-size: 40px;
    text-align: right;
    cursor: text;
    outline: none;
    color: #fff;
}
input::placeholder{
    color: #fff;
}
.calculator button{
    width: 50px;
    height: 50px;
    margin: 10px;
    border-radius: 50%;
    border: none;
    box-shadow: -5px 3px 10px rgba(9, 9, 9, 0.5);
    background: transparent;
    color: #fff;
    cursor: pointer;
    outline: none;
}
.calculator .actionbtn{
    color: #1afe17;
}
.calculator .clearbtn{
    background: #f31d1f;
}
.calculator .enter{
    background: #f5881a;
}
Copy after login

Adds a click event to each button. Depending on the button, different actions are performed:

Display Font Size Adjustment: Reduces font size when input length exceeds 10 characters.

Equal Sign (=): Evaluates the expression using eval() and displays the result. If there’s an error (e.g., invalid syntax), it displays “Error.”

Clear (AC): Resets string and clears the display.

Delete (DEL): Removes the last character from string and updates the display.

Number and Operator Buttons: Adds the button’s value to string and updates the display.

Toggle Sign ( /-):

* { padding: 0; margin: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; }
Copy after login

Multiplies the current number by -1 to toggle between positive and negative values.

Conclusively, building a simple yet functional calculator web app using HTML, CSS, and JavaScript is a fantastic project for both beginners and experienced developers. By carefully combining the structural foundation provided by HTML, the stylistic elements brought to life with CSS, and the interactive functionality powered by JavaScript, we can create an intuitive tool that not only serves its primary purpose but also demonstrates core web development concepts.

Moreover, this project opens up a wide range of possibilities for further exploration and enhancement. the lessons learned here provide a comprehensive foundation for more complex projects. Web development is an ongoing learning process, and this project showcases how every line of code contributes to a functional, engaging experience.

As you continue to refine your skills, consider how you can make this calculator even more user-friendly and powerful. Experiment with different layouts, try implementing additional mathematical functions. Every change you make deepens your understanding of coding principles and enhances your development resources.

Happy Coding!

The above is the detailed content of BUILDING A SIMPLE WEB-BASED CALCULATOR: Step-by-step Guild with Html CSS And JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template