How to Create a Stylish and Interactive BMI Calculator with JavaScript
Body Mass Index (BMI) is a simple and popular tool used to determine whether a person is underweight, healthy, overweight, or obese. With the growing importance of health and wellness, adding an interactive BMI calculator to your website can enhance user engagement and provide a helpful tool for your visitors. fsiblog, we’ll walk you through creating a stylish and interactive BMI calculator using JavaScript, HTML, and CSS.
Step 1: Set Up the Basic HTML Structure
The first step in building a BMI calculator is to create a basic HTML structure. This will include input fields for weight and height, a button to calculate BMI technology, and a section to display the result.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BMI Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="bmi-calculator">
<h1>BMI Calculator</h1>
<div class="input-group">
<label for="weight">Weight (kg):</label>
<input type="number" id="weight" placeholder="Enter your weight">
</div>
<div class="input-group">
<label for="height">Height (cm):</label>
<input type="number" id="height" placeholder="Enter your height">
</div>
<button id="calculate-btn">Calculate BMI</button>
<div id="result" class="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Step 2: Style the Calculator with CSS
To make your BMI calculator visually appealing, use CSS to style the layout. Here’s a simple example:
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.bmi-calculator {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
width: 300px;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 15px;
text-align: left;
}
label {
display: block;
font-size: 14px;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background: #007bff;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #0056b3;
}
.result {
margin-top: 20px;
font-size: 18px;
}
Step 3: Add Interactivity with JavaScript
Now let’s write the JavaScript to calculate BMI based on user input and display the result dynamically.
// script.js
document.getElementById('calculate-btn').addEventListener('click', function() {
const weight = parseFloat(document.getElementById('weight').value);
const height = parseFloat(document.getElementById('height').value) / 100; // Convert cm to meters
if (isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0) {
document.getElementById('result').textContent = 'Please enter valid values for weight and height.';
return;
}
const bmi = (weight / (height * height)).toFixed(2);
let category = '';
if (bmi < 18.5) {
category = 'Underweight';
} else if (bmi >= 18.5 && bmi < 24.9) {
category = 'Normal weight';
} else if (bmi >= 25 && bmi < 29.9) {
category = 'Overweight';
} else {
category = 'Obese';
}
document.getElementById('result').textContent = `Your BMI is ${bmi} (${category}).`;
});
Step 4: Test and Improve
With the basic setup complete, test your BMI calculator to ensure it works as expected. You can enhance it further by:
- Adding responsiveness to the CSS for better usability on mobile devices.
- Including a reset button to clear inputs and results.
- Displaying a BMI chart for more information.
Final Thoughts
Creating a stylish and interactive BMI calculator is a straightforward and fun project to improve your JavaScript, HTML, and CSS skills. It’s also a practical tool that users can benefit from. With further customization, you can make your BMI calculator a standout feature on your websites business.