Loading your tools...
Loading your tools...
A super simple guide to building a Sales Tax Calculator. We explain it with food analogies so even a child can understand!
Hi there! 👋
Have you ever bought a toy for $10, but when you got to the register, they asked for $10.80? That extra money is called Sales Tax.
Today, we are going to build a smart machine that figures out that extra number for us.
Think of coding like cooking a pizza:
Let's bake our calculator!
Every meal needs a plate. In code, we call this a "container" or a div. It holds our calculator so it doesn't spill everywhere.
We give it the ID tax-card so we can decorate it later.
<div id="tax-card">
<h2>💰 My Tax Calculator</h2>
<!-- Ingredients go here -->
</div>
First, we need to ask: "How much does the item cost?"
We use an <input> box and tell it to only accept numbers.
<label>Price ($):</label>
<input type="number" id="price-input" placeholder="10.00">
Next, we need to ask: "How much tax do we have to pay?" This is usually a percentage, like 8%.
<label>Tax Rate (%):</label>
<input type="number" id="tax-input" placeholder="8.5">
We need a button to start cooking!
When you click this, it will run a specific set of instructions called calculateTotal.
<button onclick="calculateTotal()">Start Calculating</button>
Where do we show the answer? We need a blank space waiting for the result. We'll make two spots: one for the Tax Amount (the extra cost) and one for the Total (what you pay).
<div id="result-area">
<p>Tax: <span id="tax-amount">$0.00</span></p>
<p>Total: <span id="total-amount">$0.00</span></p>
</div>
Now we teach the computer how to do the math. Here is the recipe:
<script>
function calculateTotal() {
// 1. Get ingredients
var price = parseFloat(document.getElementById('price-input').value);
var rate = parseFloat(document.getElementById('tax-input').value);
// Safety check: Did they type numbers?
if (isNaN(price) || isNaN(rate)) {
alert("Please type real numbers!");
return;
}
// 2. Do the math
// (Rate / 100) changes "8" into "0.08"
var taxMoney = price * (rate / 100);
var finalTotal = price + taxMoney;
// 3. Serve the dish (Show on screen)
// .toFixed(2) makes it look like money (2 decimal places)
document.getElementById('tax-amount').innerText = "$" + taxMoney.toFixed(2);
document.getElementById('total-amount').innerText = "$" + finalTotal.toFixed(2);
}
</script>
Memorizing tax rates is hard. Let's make it easier with a dropdown menu!
In HTML, a dropdown is called a <select>.
Add this above your Tax Input from Step 3:
<label>Choose a State:</label>
<select id="state-select" onchange="updateRate()">
<option value="0">Custom Rate</option>
<option value="7.25">California (7.25%)</option>
<option value="8.875">New York (8.88%)</option>
<option value="6.25">Texas (6.25%)</option>
<option value="6.0">Florida (6.0%)</option>
</select>
Now we need a tiny helper script. When you pick a state from the menu, it should automatically type the number into the Tax Input box for you.
function updateRate() {
var menu = document.getElementById('state-select');
var taxBox = document.getElementById('tax-input');
// If they picked a state, copy the value to the box
if (menu.value !== "0") {
taxBox.value = menu.value;
calculateTotal(); // Auto-calculate!
}
}
Let's organize our plate. We'll center everything, make the text big, and make the button green like money!
<style>
body { font-family: sans-serif; background: #f0f9ff; }
#tax-card {
background: white;
padding: 30px;
border-radius: 15px;
max-width: 350px;
margin: 50px auto;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
input, select, button {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border-radius: 5px;
border: 1px solid #ccc;
}
button {
background-color: #22c55e; /* Money Green */
color: white;
font-weight: bold;
border: none;
cursor: pointer;
}
button:hover { background-color: #16a34a; }
#result-area {
background: #f8fafc;
padding: 15px;
border-radius: 10px;
border: 2px dashed #ddd;
}
</style>
Here is the complete code. You can save this as tax-calculator.html and use it for your next shopping trip!
<!DOCTYPE html>
<html>
<head>
<title>My Tax Calculator</title>
<style>
body { background: #e0f2fe; font-family: 'Segoe UI', sans-serif; display: flex; justify-content: center; min-height: 100vh; align-items: center; margin: 0; }
.card {
background: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
width: 100%;
max-width: 320px;
}
h2 { color: #0369a1; text-align: center; margin-top: 0; }
label { font-weight: 600; font-size: 0.9rem; color: #475569; display: block; margin-bottom: 0.5rem; }
input, select {
width: 100%;
padding: 0.75rem;
border: 2px solid #e2e8f0;
border-radius: 0.5rem;
margin-bottom: 1rem;
box-sizing: border-box;
font-size: 1rem;
}
button {
width: 100%;
padding: 0.75rem;
background: #0ea5e9;
color: white;
border: none;
border-radius: 0.5rem;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
transition: 0.2s;
}
button:hover { background: #0284c7; transform: translateY(-1px); }
.result-box {
margin-top: 1.5rem;
padding: 1rem;
background: #f0f9ff;
border-radius: 0.5rem;
border: 2px dashed #bae6fd;
}
.row { display: flex; justify-content: space-between; margin-bottom: 0.5rem; color: #64748b; }
.total-row { display: flex; justify-content: space-between; font-weight: bold; font-size: 1.25rem; color: #0c4a6e; border-top: 2px solid #bae6fd; padding-top: 0.5rem; margin-top: 0.5rem; }
</style>
</head>
<body>
<div class="card">
<h2>💸 Tax Calculator</h2>
<label>Item Price ($)</label>
<input type="number" id="price" placeholder="0.00">
<label>Select State</label>
<select id="state" onchange="updateRate()">
<option value="0">-- Custom Rate --</option>
<option value="7.25">California (7.25%)</option>
<option value="8.875">New York (8.88%)</option>
<option value="6.25">Texas (6.25%)</option>
<option value="6.0">Florida (6.0%)</option>
<option value="10.0">Easy Math (10%)</option>
</select>
<label>Tax Rate (%)</label>
<input type="number" id="rate" placeholder="0.0">
<button onclick="calculate()">Calculate Total</button>
<div class="result-box">
<div class="row">
<span>Subtotal:</span>
<span id="res-price">$0.00</span>
</div>
<div class="row">
<span>Tax Amount:</span>
<span id="res-tax">$0.00</span>
</div>
<div class="total-row">
<span>TOTAL:</span>
<span id="res-total">$0.00</span>
</div>
</div>
</div>
<script>
function updateRate() {
var menu = document.getElementById('state');
if(menu.value != "0") {
document.getElementById('rate').value = menu.value;
calculate();
}
}
function calculate() {
var price = parseFloat(document.getElementById('price').value) || 0;
var rate = parseFloat(document.getElementById('rate').value) || 0;
var tax = price * (rate / 100);
var total = price + tax;
document.getElementById('res-price').innerText = "$" + price.toFixed(2);
document.getElementById('res-tax').innerText = "$" + tax.toFixed(2);
document.getElementById('res-total').innerText = "$" + total.toFixed(2);
}
// Auto-calculate when typing
document.getElementById('price').addEventListener('input', calculate);
document.getElementById('rate').addEventListener('input', calculate);
</script>
</body>
</html>
Great job! 🍎
You've built your own helper tool. You learned how to take inputs, do math with them, and show the result nicely.
Now, whenever you see a price tag, you can use your app to know exactly how much you'll pay at the register!
Try the Advanced Tax Tool »Developer Tools & Resource Experts
FastTools is dedicated to curating high-quality content and resources that empower developers. With nearly 5 years of hands-on development experience, our team rigorously evaluates every tool and API we recommend, ensuring you get only the most reliable and effective solutions for your projects.