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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                main
  .ticket
    h1 Tipping Calculator
    .inputs 
      .line-item.bill
        h2 Bill Amount
        p $
        input#bill(type="number", name="bill-amount")
      .line-item.tip-percent
        h2 Tip %
        p $
        input#tip-percent(
          type="number",
          name="tip-percent",
          min="0",
          max="100"
        )
      button#button Calculate
      button#clear-btn Clear
    .totals
      .line-item.tip
        h2 Tip
        p.total-values $
          span#tip-value 0
      .line-item.total
        h2 Total
        p.total-values $
          span#total-value 0

              
            
!

CSS

              
                body {
  background-color: hsl(0, 0%, 15%);
  font-family: "Montserrat", sans-serif;
}

main {
  display: flex;
  justify-content: center;
  align-content: center;
  margin: 4rem;
}

h1 {
  margin: 2.75rem 0 0;
  font-size: 2.5rem;
  padding-bottom: 1.5rem;
  text-align: center;
  border-bottom: solid 3px hsl(0, 0%, 80%);
}

.ticket {
  background-color: hsl(0, 0%, 100%);
  width: 400px;
  min-height: 500px;
  padding: 1rem 3rem;
  display: flex;
  align-content: flex-start;
  justify-content: center;
  flex-flow: column;
  box-shadow: 6px -6px 8px hsl(0, 0%, 10%);
}

.inputs {
  border-bottom: solid 3px hsl(0, 0%, 80%);
  padding: 1.5rem 1rem;
}

#button {
  text-align: center;
  margin: 0 auto;
  padding: 1rem 2rem;
  background-color: hsl(0, 0%, 0%);
  color: hsl(0, 0%, 100%);
  border: none;
  border-radius: 0.5rem;
  width: 100%;
  font-weight: 600;
  margin: 1rem 0;
  cursor: pointer;
}

#clear-btn {
  text-align: center;
  margin: 0 auto;
  padding: 1rem 2rem;
  border: solid 3px hsl(0, 0%, 0%);
  color: hsl(0, 0%, 0%);
  border: none;
  border-radius: 0.5rem;
  width: 100%;
  font-weight: 600;
  cursor: pointer;
}

.line-item {
  display: grid;
  grid-template-columns: 2fr 0.25fr 1fr;
  gap: 2rem;
  align-items: center;
  width: 100%;
  margin: 1.5rem 0;

  h2 {
    font-size: 1.25rem;
    justify-self: start;
    margin: 0;
  }

  p {
    font-weight: 500;
    font-size: 1.25rem;
  }

  input {
    justify-self: end;
    width: 100px;
    border-radius: 0.5rem;
    border: solid 3px hsl(0, 0%, 0%);
    padding: 0.5rem;
    text-align: center;
    font-size: 1.25rem;
  }
}

.totals {
  padding: 1rem 1rem 1.5rem;

  .line-item {
    gap: 0;
    margin: 1.5rem 0;
    grid-template-columns: repeat(2, 1fr);
  }

  h2 {
    font-size: 1.75rem;
    margin: 0;
  }

  .total-values {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 500;
    border-bottom: solid 3px hsl(0, 0%, 0%);
    border-radius: 0.5rem;
    text-align: center;
    padding: 1rem 1.5rem;
    justify-self: end;
    width: 100%;
    max-width: 100px;
  }

  span {
    margin: 0 0 0 0.5rem;
  }
}

/* Remove arrows from bill amount input */
/* Chrome, Safari, Edge, Opera */
#bill::-webkit-outer-spin-button,
#bill::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
#bill {
  -moz-appearance: textfield;
}

              
            
!

JS

              
                // Get variables
const billAmount = document.getElementById("bill");
const tipPercent = document.getElementById("tip-percent");

const tipValue = document.getElementById("tip-value");
const totalValue = document.getElementById("total-value");

const calculateButton = document.getElementById("button");
const clearButton = document.getElementById("clear-btn");

function calculateTotals() {
  // Convert the tip percentage to a decimal
  let tipDecimal = parseFloat(tipPercent.value) / 100;
  // Calculate the tip bases on the bill amount
  let tipTotal = parseFloat(billAmount.value) * tipDecimal;
  // update the tip total in the DOM
  tipValue.textContent = tipTotal.toFixed(2);
  // add the tip to the bill amount for the final total and update in the DOM
  let totalBill = parseFloat(billAmount.value) + tipTotal;
  totalValue.textContent = totalBill.toFixed(2);
}

function clearForm() {
  tipValue.textContent = "0";
  totalValue.textContent = "0";
  tipPercent.value = "";
  billAmount.value = "";
}

calculateButton.addEventListener("click", calculateTotals);
clearButton.addEventListener("click", clearForm);

              
            
!
999px

Console