How To Make Calculator using HTML, CSS, and JavaScript

Create a simple calculator in using HTML, CSS, and JavaScript

To create a calculator using HTML, CSS, and JavaScript, follow these steps:

Set up your project directory:

  1. Create a new directory for your project.
  2. Inside the project directory, create an HTML file (e.g., `calculator.html`), a CSS file (e.g., `styles.css`), and a JavaScript file (e.g., `script.js`).

Open your HTML file (`calculator.html`) in a text editor.

Add the basic structure and elements to your HTML file:


html
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" id="result" readonly>
<div class="buttons">
<!-- Add buttons for numbers and operators here -->
</div>
<button id="equal">=</button>
</div>

<script src="script.js"></script>
</body>
</html>

Open your CSS file (`styles.css`) in a text editor.

Add styles to your CSS file to customize the appearance of your calculator. For example:

css
.calculator {
max-width: 300px;
margin: 0 auto;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}

input[type=”text”] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
font-size: 20px;
}

.buttons button {
width: 50px;
height: 50px;
margin: 5px;
font-size: 20px;
}

#equal {
width: 100%;
}

Open your JavaScript file (`script.js`) in a text editor.

Add JavaScript code to handle the calculator functionality. For example:
javascript
// Get the calculator display element
const display = document.getElementById('result');

// Function to update the display
function updateDisplay(value) {
display.value += value;
}

// Add event listeners to the number buttons
const buttons = document.querySelectorAll('.buttons button:not(#equal)');
buttons.forEach(button => {
button.addEventListener('click', function() {
updateDisplay(button.textContent);

});
});

// Add event listener to the equal button
const equalButton = document.getElementById('equal');
equalButton.addEventListener('click', function() {
try {
display.value = eval(display.value);
} catch (error) {
display.value = 'Error';
}
});

Save your HTML, CSS, and JavaScript files.

Open the HTML file (`calculator.html`) in a web browser to see your calculator. You can enter numbers and perform calculations by clicking the number buttons and operators (+, -, *, /). The result will be displayed in the input field.

Customize your calculator further by adding more functionality, such as clear button, decimal point, parentheses, and more. You can enhance the styles and layout by modifying the CSS rules in `styles.css`.

Remember to validate and sanitize user input to ensure the calculator functions correctly and securely.

This is a basic example to get you started with creating a calculator using HTML, CSS, and JavaScript. As you progress, you can explore more advanced concepts, such as using frameworks like React or Vue.js, to build more sophisticated calculators or calculator-like applications.

Leave a comment