.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-form h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.calculator-form p {
margin-bottom: 25px;
line-height: 1.6;
color: #555;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.form-group input[type="number"],
.form-group input[type="text"] {
width: calc(100% – 12px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.calculator-container button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 25px;
padding-top: 20px;
border-top: 1px solid #eee;
}
.calculator-result h3 {
margin-bottom: 10px;
color: #333;
}
#result {
font-size: 1.1em;
font-weight: bold;
color: #28a745;
min-height: 25px; /* To prevent layout shifts */
}
function calculateRatioRate() {
var quantity1 = parseFloat(document.getElementById("quantity1").value);
var units1 = document.getElementById("units1").value.trim();
var quantity2 = parseFloat(document.getElementById("quantity2").value);
var units2 = document.getElementById("units2").value.trim();
var resultDiv = document.getElementById("result");
if (isNaN(quantity1) || isNaN(quantity2)) {
resultDiv.textContent = "Please enter valid numbers for quantities.";
resultDiv.style.color = "red";
return;
}
if (quantity2 === 0) {
resultDiv.textContent = "Quantity 2 cannot be zero.";
resultDiv.style.color = "red";
return;
}
var ratioRate = quantity1 / quantity2;
if (units1 && units2) {
resultDiv.textContent = ratioRate.toFixed(2) + " " + units1 + " per " + units2;
} else if (units1) {
resultDiv.textContent = ratioRate.toFixed(2) + " " + units1;
} else if (units2) {
resultDiv.textContent = ratioRate.toFixed(2) + " per " + units2;
} else {
resultDiv.textContent = ratioRate.toFixed(2);
}
resultDiv.style.color = "#28a745";
}