calculator
2 years ago in HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300&display=swap" rel="stylesheet">
<title>Calculator</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
font-family: "Montserrat", sans-serif;
height: 100vh;
background: rgb(255, 255, 255);
background: radial-gradient(
circle,
rgba(255, 255, 255, 1) 0%,
rgba(255, 214, 137, 1) 100%
);
}
h1 {
padding: 15px;
text-align: center;
}
.calculatorBody {
background-color: #f1a661;
display: block;
margin: 0 auto;
padding: 20px 0;
width: 300px;
border-radius: 35px;
}
.row {
display: flex;
justify-content: center;
align-items: center;
}
.row input {
background-color: #f8d5ab;
padding: 40px 10px;
margin-top: 20px;
margin-bottom: 10px;
width: 70%;
font-size: 2rem;
outline: none;
border: 2px solid #f1a661;
border-radius: 30px;
text-align: right;
}
.btn {
padding: 8px;
margin: 7px;
width: 45px;
font-size: 1.5rem;
border-radius: 25px;
border: none;
background-color: #f2d388;
cursor: pointer;
}
.btn:hover {
background-color: #f3cf7c;
}
@media screen and (max-width: 600px) {
.calculatorBody {
margin: 100px auto;
}
}
</style>
</head>
<body>
<h1>Calculator</h1>
<div class="calculatorBody">
<div class="row">
<input type="text" readonly>
</div>
<div class="row">
<button class="btn">7</button>
<button class="btn">8</button>
<button class="btn">9</button>
<button class="btn">+</button>
</div>
<div class="row">
<button class="btn">4</button>
<button class="btn">5</button>
<button class="btn">6</button>
<button class="btn">-</button>
</div>
<div class="row">
<button class="btn">1</button>
<button class="btn">2</button>
<button class="btn">3</button>
<button class="btn">*</button>
</div>
<div class="row">
<button class="btn">7</button>
<button class="btn">8</button>
<button class="btn">9</button>
<button class="btn">/</button>
</div>
<div class="row">
<button class="btn">.</button>
<button class="btn">0</button>
<button class="btn">C</button>
<button class="btn">=</button>
</div>
</div>
</body>
<script>
let string = "";
let buttons = document.querySelectorAll(".btn");
Array.from(buttons).forEach((button) => {
button.addEventListener("click", (e) => {
if (e.target.innerHTML == "=") {
document.querySelector("input").value = string;
string = eval(string);
} else if (e.target.innerHTML == "C") {
string = "";
document.querySelector("input").value = string;
} else {
console.log(e.target);
string = string + e.target.innerHTML;
document.querySelector("input").value = string;
}
});
});
</script>
</html>