<html lang="en">
<head>
<title>BMI Calculator</title>
<!--Google Font-->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500;700&display=swap" rel="stylesheet">
<!--Stylesheet-->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="row">
<input type="range" min="20" max="200" value="20" id="weight" oninput="calculate()">
<span id="weight-val">20 kg</span>
</div>
<div class="row">
<input type="range" min="100" max="250" value="100" id="height" oninput="calculate()">
<span id="height-val">100 cm</span>
</div>
<p id="result">20.0</p>
<p id="category">Normal weight</p>
</div>
<!--Script-->
<script src="script.js"></script>
</body>
</html>
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
height: 100vh;
background: linear-gradient(
135deg,
#61d954,
#2ebf75
);
}
.container{
background-color: #ffffff;
padding: 40px 30px;
width: 50%;
min-width: 400px;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
border-radius: 5px;
font-family: 'Poppins',sans-serif;
box-shadow: 25px 25px 30px rgba(0,0,0,0.15);
}
.row{
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 40px;
}
.row span{
font-weight: 500;
}
input[type="range"]{
width: 70%;
height: 3.5px;
appearance: none;
appearance: none;
background-color: #dcdcdc;
border-radius: 3px;
outline: none;
}
input[type="range"]::slider-thumb{
appearance: none;
appearance: none;
height: 15px;
width: 15px;
background-color: #1c1c1c;
border-radius: 50%;
cursor: pointer;
}
#result{
font-size: 30px;
font-weight: 700;
letter-spacing: 1px;
text-align: center;
color: #0be881;
}
#category{
font-size: 18px;
text-align: center;
letter-spacing: 1px;
}
function calculate(){
var bmi;
var result = document.getElementById("result");
var weight = parseInt(document.getElementById("weight").value);
document.getElementById("weight-val").textContent = weight + " kg";
var height = parseInt(document.getElementById("height").value);
document.getElementById("height-val").textContent = height + " cm";
bmi = (weight / Math.pow( (height/100), 2 )).toFixed(1);
result.textContent = bmi;
if(bmi < 18.5){
category = "Underweight";
result.style.color = "#ffc44d";
}
else if( bmi >= 18.5 && bmi <= 24.9 ){
category = "Normal Weight";
result.style.color = "#0be881";
}
else if( bmi >= 25 && bmi <= 29.9 ){
category = "Overweight";
result.style.color = "#ff884d";
}
else{
category = "Obese";
result.style.color = "#ff5e57";
}
document.getElementById("category").textContent = category;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.