JSFiddle - React, Tailwind, and code Playground
by toddmotto
HTML
<form name="bmi">
<h1>BMI Calculator</h1>
<label>
<input type="text" name="weight" placeholder="Weight (kg)">
</label>
<label>
<input type="text" name="height" placeholder="Height (cm)">
</label>
<button type="submit">
Calculate BMI
</button>
<div class="calculation">
<div>
BMI calculation: <span class="result"></span>
</div>
<div>
This means you are: <span class="health"></span>
</div>
</div>
</form>
SCSS
* {
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
font: 300 14px/1.4 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
body {
background: #596778;
margin: 0;
padding: 0;
}
input[type=text] {
outline: 0;
width: 100%;
height: 50px;
margin-bottom: 25px;
padding: 0 15px 2px;
font-size: 17px;
background: #fff;
border: 2px solid #ebebeb;
border-radius: 4px;
-webkit-box-shadow: inset 0 -2px #ebebeb;
box-shadow: inset 0 -2px #ebebeb;
&:focus {
border-color: #62c2e4;
outline: none;
-webkit-box-shadow: inset 0 -2px #62c2e4;
box-shadow: inset 0 -2px #62c2e4;
}
}
button[type=submit] {
width: 100%;
height: 54px;
padding: 0;
font-size: 22px;
color: white;
text-align: center;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
background: #f0776c;
border: 0;
border-bottom: 2px solid #d76b60;
border-radius: 5px;
cursor: pointer;
-webkit-box-shadow: inset 0 -2px #d76b60;
box-shadow: inset 0 -2px #d76b60;
}
form {
overflow: hidden;
border-radius: 5px;
padding: 35px;
margin: 50px;
position: relative;
background: #fff;
h1 {
margin: -35px -35px 25px;
padding: 32px 0 25px;
font-size: 26px;
font-weight: 300;
color: #aaa;
text-align: center;
text-shadow: 0 1px rgba(255, 255, 255, 0.75);
background: #f7f7f7;
}
&:before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 8px;
background: #c4e17f;
border-radius: 4px 4px 0 0;
background-image: -webkit-linear-gradient(left, #c4e17f, #c4e17f 12.5%, #f7fdca 12.5%, #f7fdca 25%, #fecf71 25%, #fecf71 37.5%, #f0776c 37.5%, #f0776c 50%, #db9dbe 50%, #db9dbe 62.5%, #c49cde 62.5%, #c49cde 75%, #669ae1 75%, #669ae1 87.5%, #62c2e4 87.5%, #62c2e4);
background-image: -moz-linear-gradient(left, #c4e17f, #c4e17f 12.5%, #f7fdca 12.5%, #f7fdca 25%, #fecf71 25%, #fecf71 37.5%, #f0776c 37.5%, #f0776c 50%, #db9dbe 50%, #db9dbe 62.5%, #c49cde...
JavaScript
(() => {
const form = document.querySelector('form[name=bmi]');
const getHealthMessage = unit => {
let healthMessage;
if (unit < 18.5) {
healthMessage = 'considered underweight';
} else if (unit > 18.5 && unit < 25) {
healthMessage = 'a healthy weight';
} else if (unit > 25) {
healthMessage = 'considered overweight';
}
return healthMessage;
};
const getBMI = (weight, height) => {
let newWeight = parseInt(weight, 10);
let newHeight = parseInt(height, 10);
return (newWeight / (newHeight /100 * newHeight / 100)).toFixed(1);
};
const onSubmit = event => {
event.preventDefault();
const result = form.querySelector('.result');
const health = form.querySelector('.health');
const weight = form.querySelector('input[name=weight]').value;
const height = form.querySelector('input[name=height]').value;
const bmi = getBMI(weight, height);
result.innerHTML = bmi;
health.innerHTML = getHealthMessage(bmi);
};
form.addEventListener('submit', onSubmit, false);
})();