Hello, fellow developers! Today, I’m excited to share a project that I recently worked on: a BMI (Body Mass Index) Calculator. This simple web application helps users calculate their BMI based on their height and weight, offering a straightforward way to assess their health status. Let's dive into the details of the project and how it was built!
The BMI Calculator is a user-friendly tool that allows users to input their height (in centimeters) and weight (in kilograms). With just a click of a button, the application computes the BMI and displays the corresponding health category, such as Underweight, Normal weight, Overweight, or Obesity.
Here’s a quick overview of the project structure:
BMI-Calculator/ ├── index.html ├── style.css └── script.js
Let’s take a closer look at the code that powers the BMI Calculator.
The HTML provides the basic structure of the calculator, including input fields for height and weight, a button to trigger the calculation, and a section to display the result.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>BMI Calculator</title> <link rel="stylesheet" href="./style.css"> <script src="./script.js" defer></script> </head> <body> <div class="header"> <h1>BMI Calculator</h1> </div> <div class="container"> <h1 class="heading">Body Mass Index (BMI)</h1> Your Height (cm): <input type="number" class="input" id="height" value="180" placeholder="Enter your height in cm"> Your Weight (kg): <input type="number" class="input" id="weight" value="80" placeholder="Enter your weight in kg"> <button class="btn" id="btn">Compute BMI</button> <input disabled type="text" class="input" id="bmi-result"> <h4 class="info-text">Weight Condition: <span id="weight-condition"></span></h4> </div> <div class="footer"> <p>Made with ❤️ by Abhishek Gurjar</p> </div> </body> </html>
The CSS styles the page to make it visually appealing and easy to use. It features a modern, clean design with responsive elements.
body { margin: 0; background: linear-gradient(to left bottom, lightgreen, lightblue); display: flex; flex-direction: column; min-height: 100vh; justify-content: center; align-items: center; font-family: 'Courier New', Courier, monospace; } .container { background: rgba(255, 255, 255, 0.3); padding: 20px; display: flex; flex-direction: column; border-radius: 5px; box-shadow: 0 10px 10px rgba(0, 0, 0, 0.3); margin: 5px; } .heading { font-size: 30px; } .input { padding: 10px 20px; font-size: 18px; background: rgba(255, 255, 255, 0.4); border-color: rgba(255, 255, 255, 0.5); margin: 10px; } .btn { background-color: lightgreen; border: none; padding: 10px 20px; border-radius: 5px; margin: 10px; font-size: 20px; box-shadow: 0 0 4px rgba(0, 0, 0, 0.3); cursor: pointer; } .btn:hover { box-shadow: 0 0 8px rgba(0, 0, 0, 0.3); transition: all 300ms ease; } .info-text { font-size: 20px; font-weight: 500; } .header { margin: 30px; text-align: center; } .footer { margin: 20px; text-align: center; }
The JavaScript handles the core functionality of the application—calculating the BMI based on the user’s input and updating the webpage with the results.
const btnE1 = document.getElementById("btn"); const resultE1 = document.getElementById("bmi-result"); const weightConditionE1 = document.getElementById("weight-condition"); const heightE1 = document.getElementById("height"); const weightE1 = document.getElementById("weight"); function calculateBMI() { const height = heightE1.value / 100; const weight = weightE1.value; const bmiValue = weight / (height * height); resultE1.value = bmiValue.toFixed(2); if (bmiValue < 18.5) { weightConditionE1.innerText = "Under Weight"; } else if (bmiValue >= 18.5 && bmiValue <= 24.9) { weightConditionE1.innerText = "Normal Weight"; } else if (bmiValue >= 25 && bmiValue <= 29.9) { weightConditionE1.innerText = "Over Weight"; } else if (bmiValue > 30) { weightConditionE1.innerText = "Obesity"; } } btnE1.addEventListener("click", calculateBMI);
You can try out the BMI Calculator live here.
Building this BMI Calculator was a rewarding experience that enhanced my skills in HTML, CSS, and JavaScript. The project showcases how a simple idea can be turned into a practical tool that users can benefit from. Whether you're looking to build a similar project or just exploring web development, I hope this tutorial helps you on your journey. Happy coding!
The above is the detailed content of Building a BMI Calculator with HTML, CSS, and JavaScript. For more information, please follow other related articles on the PHP Chinese website!