Margin Calculator
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
background-color: #f0f4f8;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
text-align: center;
}
h1 {
color: #333;
margin-bottom: 20px;
}
.calculator {
display: flex;
flex-direction: column;
gap: 15px;
}
input {
padding: 10px;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
width: 100%;
transition: border-color 0.3s ease;
}
input:focus {
border-color: #5fba7d;
outline: none;
}
button {
background-color: #5fba7d;
color: white;
padding: 12px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #4a9e65;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: #f9f9f9;
border-radius: 6px;
display: none;
}
#marginResult, #marginPercentage {
font-size: 18px;
color: #333;
}
#marginResult {
font-weight: 500;
}
#marginPercentage {
font-weight: bold;
color: #5fba7d;
}
document.getElementById("calculateButton").addEventListener("click", function() {
let costPrice = parseFloat(document.getElementById("costPrice").value);
let sellingPrice = parseFloat(document.getElementById("sellingPrice").value);
if (isNaN(costPrice) || isNaN(sellingPrice) || costPrice <= 0 || sellingPrice <= 0) {
alert("Please enter valid positive numbers for both cost price and selling price.");
return;
}
// Calculate margin
let margin = sellingPrice - costPrice;
let marginPercentage = (margin / sellingPrice) * 100;
// Display results
document.getElementById("marginResult").textContent = `Margin: $${margin.toFixed(2)}`;
document.getElementById("marginPercentage").textContent = `Margin Percentage: ${marginPercentage.toFixed(2)}%`;
// Show result section
document.querySelector(".result").style.display = "block";
});