HOW TO DEVELOP CALCULATOR USING HTML AND CSS AND JAVASCRIPT
Developer:vinay paariya
CALCULATOR:
CODING PHASE:
Creating a simple calculator using HTML, CSS, and JavaScript is a great way to practice your web development skills. Below is an example of how you can create a basic calculator.
### HTML (index.html)
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<div class="buttons">
<button onclick="clearDisplay()">C</button>
<button onclick="appendToDisplay('/')">/</button>
<button onclick="appendToDisplay('*')">*</button>
<button onclick="appendToDisplay('-')">-</button>
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button onclick="appendToDisplay('+')">+</button>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button onclick="calculate()" class="equals">=</button>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button onclick="appendToDisplay('0')">0</button>
<button onclick="appendToDisplay('.')">.</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
### CSS (styles.css)
css
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
font-family: Arial, sans-serif;
}
.calculator {
background-color: #333;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
#display {
width: 100%;
height: 50px;
background-color: #222;
color: #fff;
border: none;
text-align: right;
padding: 10px;
font-size: 24px;
border-radius: 5px;
margin-bottom: 10px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
background-color: #444;
color: #fff;
border: none;
padding: 20px;
font-size: 18px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #555;
}
.equals {
grid-column: span 2;
background-color: #f39c12;
}
.equals:hover {
background-color: #e67e22;
}
### JavaScript (script.js)
javascript
function appendToDisplay(value) {
document.getElementById('display').value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculate() {
try {
const result = eval(document.getElementById('display').value);
document.getElementById('display').value = result;
} catch (error) {
document.getElementById('display').value = 'Error';
}
}
### Explanation:
1. *HTML*: The structure of the calculator is defined here. It includes an input field for the display and buttons for digits and operations.
2. *CSS*: The styling for the calculator is defined here. It includes the layout, colors, and hover effects for the buttons.
3. *JavaScript*: The logic for the calculator is defined here. It includes functions to append values to the display, clear the display, and calculate the result using the eval() function.
### How to Run:
1. Create three files: index.html, styles.css, and script.js.
2. Copy the respective code into each file.
3. Open index.html in your web browser to see the calculator in action.
This is a basic calculator, and you can expand upon it by adding more features like keyboard support, more complex operations, or a history of calculations.
0 Comments