Pen Settings



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


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


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.


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.


                <div class="container">
    <h1>Weight Converter</h1>
    <div id="input">
      <select name="weightSelect" id="weightType">
        <option>Choose weight type</option>
        <option value="1">Kilograms</option>
        <option value="2">Pounds</option>
        <option value="3">Grams</option>
        <option value="4">Ounces</option>
      <input type="number" id="weightValue" value="0">
    <div id="output">
      <div class="card kilos" id="lbCard">
        <h4 id="toPounds"></h4>
      <div class="card pounds" id="kgCard">
        <h4 id="toKilos"></h4>
      <div class="card grams" id="gmCard">
        <h4 id="toGrams"></h4>
      <div class="card ounces" id="ozCard">
        <h4 id="toOunces"></h4>


                @import url('');

body {
  background: #333;
  color: #FFF;
  font-family: 'Oswald', sans-serif;
.container {
  width: 50%;
  margin: 3em auto;
  padding: 2em;
header {
  margin-bottom: 30px;
header h1 {
  font-size: 2em;
  text-align: center;
  text-transform: uppercase;
section {
  display: flex;
  flex-direction: column;
  align-items: center;
select, input {
  width: 250px;
  outline: none;
  border: 0;
  height: 30px;
  border-radius: 5px;
  font-size: 16px;
  margin-bottom: 10px;
  text-indent: 10px;
option {
  text-indent: 10px;
#input {
  margin-bottom: 20px;
#output {
  width: 70%;
.card {
  margin-bottom: 10px;
  padding: 20px;
  text-transform: uppercase;
  border-radius: 10px;
  overflow: auto;
.card h3 {
  font-size: 1.6em;
  float: left;
.card h4 {
  font-size: 1.5em;
  color: #FFF !important;
  float: right;

.kilos {
  color: #FF9900;
  border: 3px solid #FF9900;
.pounds {
  color: #FF0077;
  border: 3px solid #FF0077;
.grams {
  color: #14BAFC;
  border: 3px solid #14BAFC;
.ounces {
  color: #2CCA31;
  border: 3px solid #2CCA31;


                // Reference all DOM elements
const weightType = document.getElementById('weightType');
const weightValue = document.getElementById('weightValue');
const ozCard = document.getElementById('ozCard');
const kgCard = document.getElementById('kgCard');
const toPounds = document.getElementById('toPounds');
const toKilos = document.getElementById('toKilos');
const toGrams = document.getElementById('toGrams');
const toOunces = document.getElementById('toOunces');
const output = document.getElementById('output');

// Get a list of all cards
const cardArray = ['lbCard', 'kgCard', 'gmCard', 'ozCard'];
// Keep track of select box option
let wt = 0;

// Hide input box and cards on startup = 'hidden'; = 'hidden';

// Listen for change in select box
weightType.addEventListener('change', (e) => {
  // Show input box and cards = 'visible'; = 'visible';
  // Get current selected option value
  let val =;
  // Hide selected card and display the rest
  if(val === '1') {
    wt = 1;
  if(val === '2') {
    wt = 2;
  if(val === '3') {
    wt = 3;
  if(val === '4') {
    wt = 4;

// Updates converted values on input
weightValue.addEventListener('input', (e) => {
  let val =;
  convert(wt, val);

// FUNCTION: Hides currently selected card and displays the rest
function hideCard(card) {
  for(var i=0; i < cardArray.length; i++) {
    if(cardArray[i] === card) {
      let match = cardArray[i];
      document.getElementById(match).style.display = 'none';
    } else {
      document.getElementById(cardArray[i]).style.display = 'block';

// FUNCTION: Used only with hideCard function
// Updates cards converted values when selected weight type has changed
function updateValues() {
  let val = weightValue.value;
  convert(wt, val);

// FUNCTION: Handles all conversion ratios
function convert(wt, val) {
  switch(wt) {
    case 1: // Kilos to...
      toPounds.innerHTML = (val*2.2046).toFixed(2);
      toGrams.innerHTML = val*1000;
      toOunces.innerHTML = (val*35.274).toFixed(2);
    case 2: // Pounds to...
      toKilos.innerHTML = (val/2.2046).toFixed(2);
      toGrams.innerHTML = (val/0.0022046).toFixed(2);
      toOunces.innerHTML = val*16;
    case 3: // Grams to...
      toKilos.innerHTML = val/1000;
      toPounds.innerHTML = val/500;
      toOunces.innerHTML = (val*0.035274).toFixed(4);
    case 4: // Ounces to...
      toKilos.innerHTML = (val/35.274).toFixed(3);
      toPounds.innerHTML = (val*0.062500).toFixed(3);
      toGrams.innerHTML = (val/0.035274).toFixed(4);