How to Build a Simple Calculator Using HTML, CSS, and JavaScript
In today’s tech-driven world, it’s becoming increasingly important to have coding skills. Building a calculator interface is a fantastic way to get started with HTML, CSS, and JavaScript. In fact, it’s a great excuse to put into practice all those math lessons that we were taught in school. In this article, we will discuss how to build a simple calculator using HTML, CSS, and JavaScript.
HTML is the backbone of all web pages. To build a calculator using HTML, you will need to create a simple webpage with a few buttons and display areas. First, we will create a div container to hold the calculator. You can do this by adding the following line of code to your HTML file.
Then, we will add two more divs to this container. The first div will be for the calculator display, where users will see their calculations. We will add the following code for calculator display.
The second div will be for the buttons on the calculator. We will add the following code for the buttons.
Now that we have set up our HTML code let’s move on to the CSS. CSS is responsible for styling the webpage, and in our case, it is for our calculator application. We will add some basic styling to our calculator container. Here is how our CSS code will look.
.calculator-container {
background-color: #202124;
border-radius: 5px;
display: flex;
flex-direction: column;
padding: 10px;
width: 280px;
}
The above CSS code will add a background color, curved edges, and padding to our calculator container.
We will then add some more styles to our calculator display and calculator buttons. The styles are:
.calculator-display {
background-color: #2b2e31;
border-radius: 5px;
color: #fff;
font-size: 24px;
height: 50px;
margin-bottom: 10px;
padding: 10px;
text-align: right;
}
.calculator-buttons {
display: grid;
grid-gap: 5px;
grid-template-columns: repeat(4, 1fr);
}
The styles will change the background color, font size, and layout of the calculator display and buttons making it look more professional.
The last step in building our calculator is to write the script. For this, we will use JavaScript, which is responsible for the calculations done in the calculator. We will add some event listeners to the number and operator buttons, and we will store these values in a variable.
Here is the final code for our JavaScript file.
let display = document.querySelector(‘.calculator-display’);
let inputs = document.querySelectorAll(‘.num, .operator’);
let result;
for (let i = 0; i < inputs.length; i++) {
inputs[i].addEventListener(‘click’, function() {
if (inputs[i].classList.contains(‘num’)){
display.textContent += inputs[i].textContent;
} else if(inputs[i].classList.contains(‘operator’)) {
let operator = inputs[i].textContent;
let firstNum = parseFloat(display.textContent);
display.textContent = ”;
inputs[i].addEventListener(‘click’, function() {
let secondNum = parseFloat(display.textContent);
if(operator === ‘+’) {
result = firstNum + secondNum;
} else if(operator === ‘-‘) {
result = firstNum – secondNum;
} else if(operator === ‘*’) {
result = firstNum * secondNum;
} else if(operator === ‘/’) {
result = firstNum / secondNum;
}
display.textContent = result;
});
}
});
}
The above JavaScript code will handle the click events on the buttons and perform the calculations.
In conclusion, building a simple calculator using HTML, CSS, and JavaScript is a great way to practice your coding skills. With the code we have provided above, you can customize your calculator to your liking. There are many more advanced features you can add, such as decimals, percentages, or even a clear button. Once mastered, building a calculator can be incredibly rewarding and can lead you on to more complex coding projects.