Create a Health Tracker using HTML CSS & JavaScript
Last Updated :
25 Jul, 2024
In this article, we will see how we can create a health tracker using HTML, CSS, and JavaScript.
Here, we have provided the user with three inputs i.e. input water intake, exercise duration, and blood sugar levels. The user can input these daily and then the provided data is stored in the localStorage. This data is also shown on the website in tabular form.
Preview Image:

Approach:
- Create the App structure using Html tags, like <div>, <h1> for the heading, <input> tag for taking the input and <table> tag for the output, with corresponding classes and IDs.
- Add the different styling properties to the app using classes and elements that will define the padding, margin, font sizes to text, alignments, colors, etc to the specific elements.
- In JavaScript, first load the previous data from local storage in respective arrays and display that data inside a table in HTML.
- Add event listener to submit button.
- Whenever the submit button gets clicked, take the data from inputs and push that value to respective arrays of data.
- Then display that value in the table and also add the whole array back to local storage.
Example: This example describes the basic implementation of the health tracker app using HTML, CSS, and Javascript.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>
GeeksforGeeks | Create a Health
Tracker using HTML CSS & JavaScript
</title>
<!-- Font Awesome CSS CDN -->
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
integrity=
"sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="/style2.css">
</head>
<body>
<div class="app">
<h1>GeeksforGeeks</h1>
<h3>Health Tracker App</h3>
<div class="inputs">
<div>
<label for="water">
Water Intake (in ml)
</label>
<input id="water" type="number">
</div>
<div>
<label for="exercise">
Exercise Duration (in min)
</label>
<input id="exercise" type="number">
</div>
<div>
<label for="bloodsugerlevel">
Blood Sugar Level (in mg/dL)
</label>
<input id="bloodsugerlevel" type="number">
</div>
</div>
<button id="submit">Submit</button>
<div id="editSection" class="hidden">
<button id="cancelEdit" onclick="cancelEdit()">
Cancel
</button>
<button id="updateEntry" onclick="editRow()">
Update
</button>
</div>
<table>
<thead>
<tr>
<th>Date</th>
<th>Water Intake <br> (in ml)</th>
<th>Exercise Duration <br> (in min)</th>
<th>Blood Sugar Level <br> (in mg/dL)</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody id="output">
</tbody>
</table>
</div>
<!-- Font Awesome JS CDN -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/js/all.min.js"
integrity=
"sha512-GWzVrcGlo0TxTRvz9ttioyYJ+Wwk9Ck0G81D+eO63BaqHaJ3YZX9wuqjwgfcV/MrB2PhaVX9DkYVhbFpStnqpQ=="
crossorigin="anonymous" referrerpolicy="no-referrer">
</script>
<script src="/script2.js"></script>
</body>
</html>
CSS
/* styles.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: sans-serif;
background-color: #0f172a;
color: white;
}
h1 {
background-image: linear-gradient(to right, #0ea5e9, #10b981);
}
h3 {
background-image: linear-gradient(to right, #ec4899, #8b5cf6);
}
h1,
h3 {
color: transparent;
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.app {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 650px;
margin: 1rem auto;
padding: 10px;
gap: 20px;
}
.inputs {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
gap: 20px;
height: fit-content;
}
.inputs>div {
height: 100%;
display: flex;
justify-content: space-between;
flex-direction: column;
}
label {
font-size: 0.9rem;
}
input,
label {
display: block;
}
input {
margin-top: 8px;
margin-bottom: 5px;
padding: 10px;
font-size: large;
background-color: #262626;
color: white;
border: none;
border-radius: 5px;
width: 100%;
}
input:focus-visible {
outline: 2px solid #ec4899;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
button{ display: block; cursor: pointer; border: none; }
#submit {
width: 100%;
padding: 10px;
margin-bottom: 10px;
background-image: linear-gradient(to right, #ec4899, #8b5cf6);
font-size: 1.3rem;
border-radius: 5px;
color: white;
font-weight: bold;
transition: scale 0.3s ease-in-out;
}
#editSection{
width: 100%;
display: flex;
gap: 25px;
justify-content: space-between;
}
#editSection > button {
width: 100%;
padding: 10px;
margin-bottom: 10px;
font-size: 1.3rem;
border-radius: 5px;
color: white;
font-weight: bold;
transition: scale 0.3s ease-in-out;
}
#updateEntry { background-image:
linear-gradient(to right, #ec4899, #8b5cf6); }
#cancelEdit{ background-color: #ef4444; }
#submit:hover, #updateEntry:hover, #cancelEdit:hover { scale: 1.02; }
.hidden{ display: none !important; }
.edit, .delete{
margin: 0 auto;
padding: 5px 10px;
font-size: 1.1rem;
border-radius: 5px;
background-color: transparent;
color: white;
border: 1px solid white;
}
.edit:hover, .delete:hover{
background-color: #0a0a0a;
}
table {
width: 100%;
border-collapse: collapse;
position: relative;
overflow: hidden;
}
th,
td {
text-align: center;
padding: 10px;
border: 0;
}
tr:nth-child(even) {
background-color: #57534e;
}
tr:nth-child(odd) {
background-color: #262626;
}
th {
font-size: 0.9rem;
background-color: #0a0a0a;
}
tbody > tr:hover{
background-color: #737373;
color: black;
}
.delete-animation{
background-color: #ef4444 !important;
animation: deleteAnimate 0.4s linear forwards;
}
@keyframes deleteAnimate{
to{
transform: translateX(100%);
}
}
JavaScript
// script.js
const editIcon = `<i class="fas fa-edit"></i>`
const deleteIcon = `<i class="fas fa-trash"></i>`
function clearInputs() {
wInput.value = ""
eInput.value = ""
bInput.value = ""
}
function addToLocalStorage(){
localStorage.setItem("date", JSON.stringify(date))
localStorage.setItem("water", JSON.stringify(water))
localStorage.setItem("exercise", JSON.stringify(exercise))
localStorage.setItem("bloodsugar", JSON.stringify(bloodsugar))
}
function activateEdit(i){
wInput.value = water[i]
eInput.value = exercise[i]
bInput.value = bloodsugar[i]
editIndex = i
submitButton.classList.add("hidden")
editSection.classList.remove("hidden")
}
function cancelEdit() {
clearInputs()
editIndex = -1
submitButton.classList.remove("hidden")
editSection.classList.add("hidden")
}
function editRow(){
if(editIndex==-1) return
water[editIndex] = wInput.value
exercise[editIndex] = eInput.value
bloodsugar[editIndex] = bInput.value
fillTable()
addToLocalStorage()
cancelEdit()
}
function deleteRow(i){
if(!confirm(
`Confirm that you want to delete the entry:
\n ${date[i]}: ${water[i]}ml, ${exercise[i]}min,
${bloodsugar[i]}mg/dL`))
return
date.splice(i, 1)
water.splice(i, 1)
exercise.splice(i, 1)
bloodsugar.splice(i, 1)
document.querySelector(`#output > tr:nth-child(${i+1})`)
.classList.add("delete-animation")
addToLocalStorage()
setTimeout(fillTable, 500)
}
function fillTable(){
const tbody = document.getElementById("output")
const rows =
Math.max(water.length, exercise.length, bloodsugar.length)
let html = ""
for(let i=0; i<rows; i++){
let w = water[i] || "N/A"
let e = exercise[i] || "N/A"
let b = bloodsugar[i] || "N/A"
let d = date[i] || "N/A"
html+=`<tr>
<td>${d}</td>
<td>${w}</td>
<td>${e}</td>
<td>${b}</td>
<td>
<button onclick="activateEdit(${i})"
class="edit">${editIcon}
</button>
</td>
<td>
<button
onclick="deleteRow(${i})"
class="delete">${deleteIcon}
</button>
</td>
</tr>`
}
tbody.innerHTML = html;
}
let editIndex = -1;
let date =
JSON.parse(localStorage.getItem("date")) || []
let water =
JSON.parse(localStorage.getItem("water")) || []
let exercise =
JSON.parse(localStorage.getItem("exercise")) || []
let bloodsugar =
JSON.parse(localStorage.getItem("bloodsugar")) || []
const wInput = document.getElementById("water")
const eInput = document.getElementById("exercise")
const bInput = document.getElementById("bloodsugerlevel")
const submitButton = document.getElementById("submit")
const editSection = document.getElementById("editSection")
fillTable()
submitButton.addEventListener("click", ()=>{
const w = wInput.value || null;
const e = eInput.value || null;
const b = bInput.value || null;
if(w===null || e===null || b===null) {
alert("Please enter all the fields.")
return
}
const d = new Date().toLocaleDateString()
date = [d, ...date]
water = [w, ...water]
exercise = [e, ...exercise]
bloodsugar = [b, ...bloodsugar]
// date.push(d)
// water.push(w)
// exercise.push(e)
// bloodsugar.push(b)
clearInputs()
fillTable()
addToLocalStorage()
})
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read