Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <form id="calorie-calculator">
  <h2 class="form-title">Daily Calories Calculator</h2>
  <label for="age">Age</label>
  <input type="number" name="age" id="age" placeholder="Enter your age in years" required>
  <span></span>
  <div class="radio-group">
    <label for="sex">Sex</label>
    <input type="radio" name="sex" value="male" id="male" checked="checked"> <label for="male">Male</label>
    <input type="radio" name="sex" value="female" id="female"> <label for="female">Female</label>
  </div>
  <label>Weight (lbs)</label>
  <input type="number" name="weight" id="weight" placeholder="Enter your weight in pounds" required>
  <span></span>
  <label>Height (inches)</label>
  <input type="number" name="inches" id="inches" placeholder="Enter your height in inches" required>
  <span></span>
  <label>Activity Level</label>
  <select name="activity_level" id="activity_level" required>
    <option value="">Select an Activity Level</option>
    <option value="1.2">Little to no exercise</option>
    <option value="1.375">Light exercise (1−3 days per week)</option>
    <option value="1.55">Moderate exercise (3−5 days per week)</option>
    <option value="1.725">Heavy exercise (6−7 days per week)</option>
    <option value="1.9">Very heavy exercise (twice per day, extra heavy workouts)</option>
  </select>
  <span></span>
  <label>Choose Goal:</label>
  <select name="gain_loss_amount" id="gain_loss_amount" required>
    <option value="">Select a Goal</option>
    <option value="-1000">Lose 2 Pounds per Week</option>
    <option value="-750">Lose 1.5 Pounds per Week</option>
    <option value="-500">Lose 1 Pounds per Week</option>
    <option value="-250">Lose 0.5 Pounds per Week</option>
    <option value="0">Stay the Same Weight</option>
    <option value="250">Gain 0.5 Pound per Week</option>
    <option value="500">Gain 1 Pound per Week</option>
    <option value="750">Gain 1.5 Pounds per Week</option>
    <option value="1000">Gain 2 Pounds per Week</option>
  </select>
  <span></span>
  <button type="reset">Reset</button>
  <button type="submit">Calculate</button>
  <div id="results"></div>
</form>

<form id="macro-calculator">
  <label>Protein (g)</label>
  <input type="number" name="weight" id="protein" placeholder="Protein in grams" required>
  <span></span>
  <label>Fat (g)</label>
  <input type="number" name="weight" id="fat" placeholder="Fat in grams" required>
  <span></span>
  <label>Carbohydrates (g)</label>
  <input type="number" name="weight" id="carbs" placeholder="Carbs in grams" required>
  <span></span>
  
  <button type="reset">Reset</button>
  <button type="submit">Calculate</button>

  <div id="m-results"></div>
</form>
              
            
!

CSS

              
                * {
  margin: 0;
  box-sizing: border-box;
}

html {
  font-family: sans-serif;
}

form {
  padding: 1rem;
  max-width: 500px;
  margin: auto;
  position: relative;
}

.form-title {
  margin-bottom: 1rem;
}

button,
input,
select {
  font-size: 1rem;
  margin: 0 0 1.2rem;
}

label {
  text-transform: uppercase;
  display: block;
  font-weight: bold;
}

input[type="radio"] + label {
  display: inline-block;
  text-transform: initial;
  font-weight: normal;
  padding: .3rem;
}

input[type="radio"] {
  transform: translateY(-2px);
}

input:-webkit-autofill,
input:-webkit-autofill:hover, 
input:-webkit-autofill:focus, 
input:-webkit-autofill:active {
  background-color: white;
  -webkit-box-shadow: 0 0 0 30px white inset;
}

input[type="text"],
input[type="number"],
select {
  display: block;
  width: 100%;
  height: 2.5rem;
  border: 1px solid #ccc;
  padding: .5rem;
  border-radius: 5px;
  cursor: pointer;
  outline: none;
  font-size: 1rem;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}

button {
  border: 1px solid rgba(0,0,0,.35);
  border-bottom-width: 3px;
  background-color: transparent;
  font-weight: bold;
  padding: .5rem 1rem;
  border-radius: 5px;
  outline: none;
  font-size: 1rem;
}

button[type="reset"] {
  background-color: #e41a23;
  color: white;
}

button[type="submit"] {
  background-color: #5cb85c;
  color: white;
}

input:focus,
select:focus {
  border-color: #e41a23;
}

input:focus + span::before,
select:focus + span::before {
  content: '▶';
  position: absolute;
  left: -.25rem;
  transform: translateY(-3rem);
  color: #e41a23;
}

.radio-group:focus-within::after {
  content: '▶';
  position: absolute;
  left: -.25rem;
  transform: translateY(.25rem);
  color: #e41a23;  
}
 
              
            
!

JS

              
                $('#calorie-calculator').submit(function(e){
  e.preventDefault();
  calcDailyCals();
});

$('#macro-calculator').submit(function(e){
  e.preventDefault();
  calcCalsFromMacros();
});

$('button[type="reset"]').click(function(){
  $('#results').fadeOut('fast',function(){
    $(this).html('');
  });
});

function calcCalsFromMacros(){
  let carbs = parseInt($('#carbs').val()) * 4;
  let protein = parseInt($('#protein').val()) * 4;
  let fat = parseInt($('#fat').val()) * 9;

  let result = carbs+protein+fat;
  
  $('#m-results').fadeOut('fast',function(){
    $(this).html('<h3>Estimated Daily Calories: ' + result + '</h3>').fadeIn('fast');
  });
  
}


function calcDailyCals(){
  let age = parseInt($('#age').val());
  let sex = $('input[name="sex"]:checked').val();
  let weight = parseFloat($('#weight').val()) * 0.453592;
  let height = parseFloat($("#inches").val()) * 2.54;
  let activity = parseFloat($('#activity_level').val());
  let goal = parseInt($('#gain_loss_amount').val());
  
  let result;
  
	if (sex === 'male') {
    result = (88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age)) * activity;
  } else {
    result = (447.593 + (9.247 * weight) + (3.098 * height) - (4.33 * age)) * activity;
  }
  
	result = Math.round(result + goal);
  
  calcDailyMacros(result);
  
  $('#results').fadeOut('fast',function(){
    $(this).html('<h3>Estimated Daily Calories: ' + result + '</h3>').fadeIn('fast');
  });    
  
  function calcDailyMacros(result){
    let carbs = (result * .4) /4; 
    let protein = (result * .3) /4; 
    let fat = (result * .3) / 9;
    
    $('#carbs').val(Math.round(carbs));
    $('#protein').val(Math.round(protein));
    $('#fat').val(Math.round(fat));
  }
  
  
}


              
            
!
999px

Console