Simple Calculator: Crafting a Calculator with HTML, CSS, and JavaScript

Hey, I have just completed building a simple calculator using HTML, CSS and JavaScript. Let's dive into this new journey and see what the challenges I faced during building this simple project.

Let's Get Started!

First things first, let's set up our project. Open your favourite text editor (I choose VS Code) and create a new folder for your project. Inside that folder, create three files: index.html, style.css and script.js.

Creating the HTML structure:

In 'index.html' file create a basic structure for our calculator, like displaying screen, some buttons.

<!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 class="calculator"> <input type="text" id="display" readonly>

<!-- Buttons will go here -->

</div> <script src="script.js"></script>

</body>

</html>

Styling with CSS:

Let's make our calculator look nice. In 'style.css' add some basic styling like:

.calculator {
    width: 300px;
    margin: 50px auto;
    border: 2px solid #333;
    border-radius: 5px;
    padding: 10px;
}

#display {
    width: 100%;
    margin-bottom: 10px;
    padding: 10px;
    font-size: 20px;
}

button {
    width: 70px;
    height: 50px;
    margin: 5px;
    font-size: 18px;}

Adding Functionality:

Last but not least, let's make our calculator actually work! In 'script.js', add some JavaScript magic.

document.addEventListener('DOMContentLoaded', function() {
    const display = document.getElementById('display');

    // Add event listeners to buttons and define functionality
    // (I'm leaving this part out for brevity, but you can find the full code in the blog)
});

And that's it, we have built our simple but a functional calculator using HTML, CSS and JavaScript. I have also uploaded it using Vercel.

Here, is the link for my simple calculator:

SimpleCalculator(MyProject)

Happy coding!! ๐Ÿš€โœจ

11
Subscribe to my newsletter

Read articles from Samriddhi Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Samriddhi Sharma
Samriddhi Sharma

I am a fresher in the field of web development and learning many tech related things. I am enthusiast to learn about cybersecurity. I have build some projects in python and have done AI 2.0 python certification from digital Skills India.