Land area calculator
Area calculator using html css and javascript:-
Html code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Land Area Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<label for="length">Length (meters): </label>
<input type="number" id="length" required>
<label for="width">Width (meters): </label>
<input type="number" id="width" required>
<button onclick="calculateArea()">Calculate Area</button>
<p id="result">Result: </p>
</div>
<script src="scripts.js"></script>
</body>
Css code:-
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
}
.calculator {
text-align: center;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
label {
display: block;
margin: 10px 0;
}
input {
width: 100%;
padding: 8px;
margin: 6px 0;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
p#result {
margin-top: 20px;
font-weight: bold;
}
Javascript code:-
function calculateArea() {
// Get input values
var length = parseFloat(document.getElementById("length").value);
var width = parseFloat(document.getElementById("width").value);
// Check if inputs are valid numbers
if (isNaN(length) || isNaN(width)) {
alert("Please enter valid numbers for length and width.");
return;
}
// Calculate area
var area = length * width;
// Display the result
document.getElementById("result").innerHTML = "Result: " + area.toFixed(2) + " square meters";
}
Comments
Post a Comment