# BMI Calculator using JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. In this project, we will create a BMI calculator which will calculate the BMI(Body Mass Index) which will tell that the weight and height are suitable for a particular weight. In Today’s session, We will use HTML, CSS, and JavaScript to complete BMI Calculator Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment.

ADVERTISEMENT

At last we will use JS (JavaScript) which will add logic to make the project responsive from the user end.

I hope you have got an idea about the project.

### HTML Code for BMI Calculator

```<!DOCTYPE html>
<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>```

First, we’ll start with creating the structure of the BMI Calculator project for that as you can see in the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

Restaurant Website Using HTML And CSS With Source Code

### CSS Code for BMI Calculator

```*,
*: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;
-webkit-appearance: none;
appearance: none;
background-color: #dcdcdc;
border-radius: 3px;
outline: none;
}
input[type="range"]::-webkit-slider-thumb{
-webkit-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;
}```

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the BMI Calculator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

### JavaScript Code for BMI Calculator

```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;
}```

Last stage of the project the JavaScript in which we added the logical and coded as per the requirement. We have also defined a function to make the slider move and make it responsive. Let us see the Final Output of the project Creating a BMI Calculator using HTML, CSS & JavaScript (Source Code).

### Final Output Of BMI Calculator Using JavaScript

We have successfully created our BMI Calculator using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

25+ HTML & CSS Card Design Layout Style

If you find out this Blog helpful, then make sure to search code random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9