Body Mass Index (BMI) Calculator
body { font-family: Arial, sans-serif; background: #f9f9f9; }
.container { max-width: 420px; margin: 30px auto; background: #fff; border-radius: 8px; box-shadow: 0 0 10px #ccc; padding: 24px; }
.tabs { display: flex; margin-bottom: 18px; }
.tab { flex: 1; text-align: center; padding: 10px 0; border-radius: 4px 4px 0 0; background: #e9e9e9; cursor: pointer; }
.tab.active { background: #417d3a; color: #fff; font-weight: bold; }
.panel { display: none; }
.panel.active { display: block; }
.form-row { margin-bottom: 14px; display: flex; align-items: center; }
.form-row label { width: 80px; }
.form-row input[type="text"], .form-row input[type="number"] { width: 110px; padding: 4px 6px; }
.form-row input[type="radio"] { margin-left: 12px; }
.btns { text-align: right; margin-top: 8px; }
.btn { background: #417d3a; color: #fff; border: none; padding: 7px 18px; border-radius: 4px; margin-left: 6px; cursor: pointer; }
.btn.clear { background: #bbb; color: #222; }
.result { margin-top: 24px; background: #f2ffe0; border-radius: 6px; padding: 18px; }
.bmi-value { font-size: 1.5em; font-weight: bold; }
.bmi-category { font-size: 1.1em; margin-left: 8px; }
.bmi-gauge { display: block; margin: 20px auto 10px auto; }
.bmi-details { font-size: 0.98em; margin-top: 12px; }
.label-small { font-size: 0.9em; color: #555; margin-left: 5px; }
.h2result { background: #6a953e; color: #fff; padding: 7px 12px; border-radius: 5px 5px 0 0; font-size: 1.2em; margin: 0 0 10px 0;}
.saveicon { float:right; padding-top:3px; cursor:pointer;}
ages: 2 - 120
ft
in
lbs
ages: 2 - 120
cm
kg
function switchTab(idx) {
var tabs = document.querySelectorAll('.tab');
var panels = document.querySelectorAll('.panel');
tabs.forEach((t,i) => t.classList.toggle('active', i===idx));
panels.forEach((p,i) => p.classList.toggle('active', i===idx));
clearResult();
}
function clearResult() {
document.getElementById('bmiValue').textContent = 'BMI = -- kg/m²';
document.getElementById('bmiCategory').textContent = '';
document.getElementById('bmiDetails').innerHTML = '';
drawBMIGauge(null);
}
function calculateBMI(unit) {
let age, gender, height, weight, heightM, weightKG;
if (unit === 'us') {
age = +document.getElementById('us-age').value;
gender = document.querySelector('input[name="us-gender"]:checked').value;
let ft = +document.getElementById('us-height-ft').value;
let inch = +document.getElementById('us-height-in').value;
height = ft * 12 + inch; // in inches
weight = +document.getElementById('us-weight').value; // lbs
heightM = height * 0.0254;
weightKG = weight * 0.453592;
} else {
age = +document.getElementById('metric-age').value;
gender = document.querySelector('input[name="metric-gender"]:checked').value;
height = +document.getElementById('metric-height').value; // cm
weight = +document.getElementById('metric-weight').value; // kg
heightM = height / 100;
weightKG = weight;
}
if (heightM <= 0 || weightKG <= 0) {
alert('Please enter valid height and weight.');
return;
}
let bmi = weightKG / (heightM * heightM);
bmi = Math.round(bmi * 10) / 10;
let category = '', color = '#008137';
if (bmi < 16) { category = 'Severe Thinness'; color = '#bc2020'; }
else if (bmi < 17) { category = 'Moderate Thinness'; color = '#d38888'; }
else if (bmi < 18.5) { category = 'Mild Thinness'; color = '#ffe400'; }
else if (bmi < 25) { category = 'Normal'; color = '#008137'; }
else if (bmi < 30) { category = 'Overweight'; color = '#ffe400'; }
else if (bmi < 35) { category = 'Obese Class I'; color = '#d38888'; }
else if (bmi 25 ? (Math.round((weightKG - 25 * heightM * heightM) * 10) / 10) : 0;
let bmiPrime = Math.round((bmi / 25) * 100) / 100;
let ponderal = Math.round((weightKG / Math.pow(heightM, 3)) * 10) / 10;
let details = `
- Healthy BMI range: 18.5 kg/m² - 25 kg/m²
- Healthy weight for the height: ${healthyMin} kg - ${healthyMax} kg
${loseWeight > 0 ? `- Lose ${loseWeight} kg to reach a BMI of 25 kg/m².
` : ''}
- BMI Prime: ${bmiPrime}
- Ponderal Index: ${ponderal} kg/m³
`;
document.getElementById('bmiDetails').innerHTML = details;
drawBMIGauge(bmi);
}
function drawBMIGauge(bmi) {
// If bmi is null, show gauge with no needle
let minBMI = 16, maxBMI = 40;
let angle = bmi ? ((bmi - minBMI) / (maxBMI - minBMI)) * 144 - 72 : 0;
angle = bmi ? Math.max(-72, Math.min(angle, 72)) : 0;
let rad = (angle - 90) * Math.PI / 180;
let cx = 140, cy = 140, r = 75;
let x2 = cx + r * Math.cos(rad);
let y2 = cy + r * Math.sin(rad);
let bmiLabel = bmi ? bmi : '--';
let svg = `
16
17
18.5
25
30
35
40
Underweight
Normal
Overweight
Obesity
${bmi !== null ? `` : ''}
BMI = ${bmiLabel}
`;
document.getElementById('gauge-container').innerHTML = svg;
}
// Show gauge always at init
drawBMIGauge(null);