Creating an online calculator involves using HTML, CSS, and JavaScript. Here's a simple example to get you started:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Calculator</title>
<style>
body {
font-family: 'Arial', sans-serif;
text-align: center;
background-color: #f4f4f4;
}
#calculator {
width: 300px;
margin: 100px auto;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
font-size: 16px;
}
button {
width: 48%;
padding: 10px;
font-size: 16px;
cursor: pointer;
}
button.operator {
background-color: #f36f48;
color: #fff;
}
button.equal {
background-color: #4caf50;
color: #fff;
}
</style>
</head>
<body>
<div id="calculator">
<input type="text" id="display" readonly>
<br>
<button onclick="appendValue('1')">1</button>
<button onclick="appendValue('2')">2</button>
<button onclick="appendValue('3')">3</button>
<button class="operator" onclick="appendValue('+')">+</button>
<br>
<button onclick="appendValue('4')">4</button>
<button onclick="appendValue('5')">5</button>
<button onclick="appendValue('6')">6</button>
<button class="operator" onclick="appendValue('-')">-</button>
<br>
<button onclick="appendValue('7')">7</button>
<button onclick="appendValue('8')">8</button>
<button onclick="appendValue('9')">9</button>
<button class="operator" onclick="appendValue('*')">*</button>
<br>
<button onclick="appendValue('0')">0</button>
<button onclick="clearDisplay()">C</button>
<button class="equal" onclick="calculate()">=</button>
<button class="operator" onclick="appendValue('/')">/</button>
</div>
<script>
function appendValue(value) {
document.getElementById('display').value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculate() {
try {
document.getElementById('display').value = eval(document.getElementById('display').value);
} catch (error) {
document.getElementById('display').value = 'Error';
}
}
</script>
</body>
</html>
Copy and paste this code into an HTML file, and you'll have a simple calculator with basic functionality. Note that using eval() for calculation can have security implications, and in a real-world application, you should implement a more secure approach to evaluate expressions.