Note - Double Click to Copy Code Contact Us!

BMI Calculator Using HTML , CSS & JavaScript

Tech Doubility
BMI Calculator Using HTML , CSS & JavaScript



HTML CODE
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>BMI Calculator</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. </head>
  7. <body>
  8. <h1>BMI Calculator</h1>
  9. <div class="container">
  10. <label for="weight">Weight (kg):</label>
  11. <input type="number" id="weight" step="0.01" min="0" required>
  12. <label for="height">Height (cm):</label>
  13. <input type="number" id="height" step="0.01" min="0" required>
  14. <button onclick="calculateBMI()">Calculate</button>
  15. <div id="result"></div>
  16. </div>
  17.  
  18. <script src="script.js"></script>
  19. </body>
  20. </html>
CSS CODE
  1. .container {
  2. width: 300px;
  3. margin: 0 auto;
  4. }
  5.  
  6. label {
  7. display: block;
  8. margin-bottom: 10px;
  9. }
  10.  
  11. input {
  12. width: 100%;
  13. padding: 5px;
  14. margin-bottom: 10px;
  15. }
  16.  
  17. button {
  18. display: block;
  19. width: 100%;
  20. padding: 10px;
  21. background-color: #4CAF50;
  22. color: white;
  23. border: none;
  24. cursor: pointer;
  25. }
  26.  
  27. #result {
  28. margin-top: 20px;
  29. font-weight: bold;
  30. }
Javascript Code
  1. function calculateBMI() {
  2. var weight = parseFloat(document.getElementById("weight").value);
  3. var height = parseFloat(document.getElementById("height").value);
  4.  
  5. if (isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0) {
  6. document.getElementById("result").innerHTML = "Please enter valid values for weight and height.";
  7. return;
  8. }
  9.  
  10. var bmi = weight / ((height/100) * (height/100));
  11. bmi = bmi.toFixed(2);
  12.  
  13. var category = "";
  14. if (bmi < 18.5) {
  15. category = "Underweight";
  16. } else if (bmi >= 18.5 && bmi < 25) {
  17. category = "Normal weight";
  18. } else if (bmi >= 25 && bmi < 30) {
  19. category = "Overweight";
  20. } else {
  21. category = "Obese";
  22. }
  23.  
  24. document.getElementById("result").innerHTML = "BMI: " + bmi + "<br>Category: " + category;
  25. }

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.