Sales Tax Calculator
/* General Styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Arial', sans-serif;
}
body {
background-color: #f7f7f7;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.calculator-container {
background-color: #fff;
border-radius: 10px;
padding: 30px;
width: 100%;
max-width: 500px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #4CAF50;
font-size: 2em;
}
.input-section {
margin-bottom: 20px;
}
label {
font-size: 1.2em;
margin-bottom: 5px;
display: block;
color: #333;
}
input[type="number"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 2px solid #ccc;
border-radius: 5px;
font-size: 1em;
color: #333;
}
button {
width: 100%;
padding: 12px;
background-color: #4CAF50;
color: white;
font-size: 1.2em;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.output-section {
margin-top: 20px;
text-align: center;
}
.output-section p {
font-size: 1.2em;
margin: 10px 0;
}
.output-section span {
font-weight: bold;
color: #4CAF50;
}
/* Responsive Styles */
@media (max-width: 768px) {
.calculator-container {
padding: 20px;
}
}
function calculateTax() {
// Get input values
const price = parseFloat(document.getElementById('price').value);
const taxRate = parseFloat(document.getElementById('tax-rate').value);
// Validate inputs
if (isNaN(price) || isNaN(taxRate) || price <= 0 || taxRate <= 0) {
alert("Please enter valid positive numbers for price and tax rate.");
return;
}
// Calculate the sales tax and total price
const salesTax = (price * taxRate) / 100;
const totalPrice = price + salesTax;
// Display the results
document.getElementById('sales-tax').textContent = `$${salesTax.toFixed(2)}`;
document.getElementById('total-price').textContent = `$${totalPrice.toFixed(2)}`;
}