Below is a complete, self-contained single-file web app (HTML + CSS + JS) you can drop on your desktop and open in any modern browser.
It gives you:
A “Grams ➜ Calories” calculator.
• Type grams of protein, carbs, and fat → it instantly shows the meal’s calories.
A “Calories ➜ Macro Split” tool.
• Enter a target calorie amount, then use two sliders (protein %, fat %).
• Carbs % is calculated automatically so the three always add up to 100 %.
• The app then shows how many grams of each macro you’ll need.
The calorie math follows the classic rules:
• Protein = 4 kcal/g • Carbs = 4 kcal/g • Fat = 9 kcal/g
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Calorie & Macro Tracker</title>
<!-- Bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background:#f5f7fa; }
.card { box-shadow:0 2px 6px rgba(0,0,0,.08); }
.slider-value { min-width:3rem; display:inline-block; text-align:right;}
.macro-icon { font-size:1.1rem; margin-right:.25rem;}
</style>
</head>
<body>
<div class="container py-5">
<h1 class="mb-4 text-center fw-bold">🥗 Calorie & Macro Tracker</h1>
<!-- SECTION 1 – Grams ➜ Calories -->
<div class="card mb-5">
<div class="card-header bg-success text-white fw-semibold">
1️⃣ Grams ➜ Calories
</div>
<div class="card-body">
<div class="row g-3 align-items-end">
<div class="col-sm-4">
<label class="form-label">Protein (g)</label>
<input id="gProtein" type="number" min="0" step="0.1" class="form-control">
</div>
<div class="col-sm-4">
<label class="form-label">Carbs (g)</label>
<input id="gCarbs" type="number" min="0" step="0.1" class="form-control">
</div>
<div class="col-sm-4">
<label class="form-label">Fat (g)</label>
<input id="gFat" type="number" min="0" step="0.1" class="form-control">
</div>
</div>
<hr class="my-4">
<h4 class="text-center">
Total Calories:
<span id="totalCalsFromGrams" class="badge bg-success fs-5">0</span>
kcal
</h4>
</div>
</div>
<!-- SECTION 2 – Calories ➜ Macro Split -->
<div class="card">
<div class="card-header bg-primary text-white fw-semibold">
2️⃣ Calories ➜ Macro Split
</div>
<div class="card-body">
<!-- Calories input -->
<div class="mb-4">
<label class="form-label">Target Calories (kcal)</label>
<input id="targetCals" type="number" min="0" step="1" class="form-control" placeholder="e.g. 2200">
</div>
<!-- Sliders -->
<div class="mb-4">
<label class="form-label">Protein % <span class="slider-value" id="protPctLbl">30</span>%</label>
<input id="protPct" type="range" class="form-range" min="0" max="100" value="30">
<label class="form-label">Fat % <span class="slider-value" id="fatPctLbl">25</span>%</label>
<input id="fatPct" type="range" class="form-range" min="0" max="100" value="25">
<!-- Carbs is derived -->
<div class="form-text">
Carbs % is automatically set to <b id="carbPctLbl">45</b> % so all macros add to 100 %.
</div>
</div>
<hr>
<!-- Output table -->
<div class="table-responsive">
<table class="table table-sm text-center align-middle">
<thead class="table-light">
<tr>
<th></th>
<th>Protein</th>
<th>Carbs</th>
<th>Fat</th>
</tr>
</thead>
<tbody>
<tr>
<th>Calories</th>
<td id="protCals">0</td>
<td id="carbCals">0</td>
<td id="fatCals">0</td>
</tr>
<tr>
<th>Grams</th>
<td id="protGrams">0</td>
<td id="carbGrams">0</td>
<td id="fatGrams">0</td>
</tr>
</tbody>
</table>
</div>
<div id="warning" class="text-danger fw-semibold d-none">
⚠️ Protein % + Fat % cannot exceed 100 %.
</div>
</div>
</div>
<p class="mt-4 small text-muted text-center">Calorie values: protein 4 kcal/g, carbs 4 kcal/g, fat 9 kcal/g.
This tool is for informational purposes only, not medical advice.</p>
</div>
<script>
/* ------------ CONSTANTS ------------- */
const KCAL_P_G_PROT = 4;
const KCAL_P_G_CARB = 4;
const KCAL_P_G_FAT = 9;
/* ------------ SECTION 1 LOGIC: grams ➜ calories ------------- */
const gProtein = document.getElementById('gProtein');
const gCarbs = document.getElementById('gCarbs');
const gFat = document.getElementById('gFat');
const totalCalsFromGrams = document.getElementById('totalCalsFromGrams');
[gProtein, gCarbs, gFat].forEach(el => {
el.addEventListener('input', updateCaloriesFromGrams);
});
function updateCaloriesFromGrams(){
const p = parseFloat(gProtein.value) || 0;
const c = parseFloat(gCarbs.value) || 0;
const f = parseFloat(gFat.value) || 0;
const kcal = p*KCAL_P_G_PROT + c*KCAL_P_G_CARB + f*KCAL_P_G_FAT;
totalCalsFromGrams.textContent = kcal.toFixed(0);
}
/* ------------ SECTION 2 LOGIC: calories ➜ macro split ------------- */
const targetCals = document.getElementById('targetCals');
const protPct = document.getElementById('protPct');
const fatPct = document.getElementById('fatPct');
const protPctLbl = document.getElementById('protPctLbl');
const fatPctLbl = document.getElementById('fatPctLbl');
const carbPctLbl = document.getElementById('carbPctLbl');
const protCalsEl = document.getElementById('protCals');
const carbCalsEl = document.getElementById('carbCals');
const fatCalsEl = document.getElementById('fatCals');
const protGramsEl = document.getElementById('protGrams');
const carbGramsEl = document.getElementById('carbGrams');
const fatGramsEl = document.getElementById('fatGrams');
const warningBox = document.getElementById('warning');
// Attach listeners
[targetCals, protPct, fatPct].forEach(el => {
el.addEventListener('input', updateSplit);
});
// Init default display
updateSplit();
function updateSplit(){
const kcalTarget = parseFloat(targetCals.value) || 0;
let pPct = parseFloat(protPct.value);
let fPct = parseFloat(fatPct.value);
// Prevent sum > 100
if (pPct + fPct > 100) {
warningBox.classList.remove('d-none');
// Scale both percentages to fit 100
const scale = 100 / (pPct + fPct);
pPct = Math.round(pPct * scale);
fPct = 100 - pPct; // keep total 100
protPct.value = pPct;
fatPct.value = fPct;
} else {
warningBox.classList.add('d-none');
}
const cPct = 100 - pPct - fPct;
// Update labels
protPctLbl.textContent = pPct;
fatPctLbl.textContent = fPct;
carbPctLbl.textContent = cPct;
// Calories per macro
const pCals = kcalTarget * (pPct/100);
const fCals = kcalTarget * (fPct/100);
const cCals = kcalTarget * (cPct/100);
// Grams
const pGr = pCals / KCAL_P_G_PROT;
const fGr = fCals / KCAL_P_G_FAT;
const cGr = cCals / KCAL_P_G_CARB;
// Update UI
protCalsEl.textContent = pCals.toFixed(0);
fatCalsEl .textContent = fCals.toFixed(0);
carbCalsEl.textContent = cCals.toFixed(0);
protGramsEl.textContent = pGr.toFixed(1);
fatGramsEl .textContent = fGr.toFixed(1);
carbGramsEl.textContent = cGr.toFixed(1);
}
</script>
</body>
</html>
How it works
Grams ➜ Calories
• Type any value; JS instantly multiplies by 4/4/9 to sum total kcal.
Calories ➜ Macro Split
• Enter a calorie target.
• Move the protein% and fat% sliders; carbs% auto-fills to keep the pie at 100 %.
• When protein% + fat% would exceed 100, the app scales them down and shows a warning.
• The program converts each macro’s calories to grams using the same 4/4/9 factors.
Feel free to tweak the styling or extend with persistence (localStorage, a backend, etc.) as you see fit. Happy tracking!