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

              
                    <div class="tip__calc">
      <div class="tip">
        <div class="calc__form--label">Tip Amount</div>
        <div class="calc__form--value">
          <sup>$</sup><span class="tip__total--amount value">12.00</span>
        </div>
      </div>
      <div class="tip">
        <div class="calc__form--label">Total per Person</div>
        <div class="calc__form--value">
          <sup>$</sup><span class="tip__pers--amount value">4.00</span>
        </div>
      </div>
      <form id="calc__form" class="calc__form" method="" action="">
        <div class="row calc__form--input">
          <div class="input">
            <input
              class=""
              name="total"
              type="number"
              placeholder=""
              min="0"
              step="0.01"
              value="60.00"
              size="8"
            />
            <label class="calc__form--label" for="total">Bill Amount</label>
          </div>
          <div class="input">
            <input
              class=""
              name="nb-people"
              type="number"
              placeholder=""
              min="1"
              step="1"
              value="3"
              size="4"
            />
            <label class="calc__form--label" for="total"
              >Number of People</label
            >
          </div>
        </div>
        <div class="row calc__form--radio">
          <div class="radio">
            <input type="radio" name="tip" id="5p" value="0.05" />
            <label class="form-control" for="5p">5%</label>
          </div>
          <div class="radio">
            <input type="radio" name="tip" id="10p" value="0.1" />
            <label class="form-control" for="10p">10%</label>
          </div>
          <div class="radio">
            <input type="radio" name="tip" id="15p" value="0.15" />
            <label class="form-control" for="15p">15%</label>
          </div>
          <div class="radio">
            <input type="radio" name="tip" id="20p" value="0.2" checked="checked" />
            <label class="form-control" for="20p">20%</label>
          </div>
        </div>
        <div class="row calc__form--submit">
          <button class="" name="submit" type="submit">Calculate</button>
        </div>
      </form>
    </div>

              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Inter:wght@700&family=Roboto+Mono:wght@700&display=swap");

:root {
  --background-color: #f6f6f3;
  --form-background-color: #f7f7f7;
  --tips-background-color: #eeeeee;
  --border-color: #dedede;
  --discount-color: #60c1b6;
  --button-color: #ed7861;
  --text-color: #333;
}

body {
  background-color: var(--background-color);
  font-family: "Inter", sans-serif;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  color: var(--text-color);
}

.tip__calc {
  background: #ffffff;
  box-shadow: 0px 0px 48px rgba(148, 146, 120, 0.23);
  border-radius: 22px;

  display: flex;
  flex-direction: column;
  align-items: center;

  padding-top: 1rem;
  max-width: 50rem;
}

.tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  width: 100%;
  padding: 1.5rem 0;
}

.tip + .tip {
  border-top: 1px solid var(--border-color);
}

.calc__form {
  background-color: var(--form-background-color);
  display: flex;
  flex-direction: column;
  border-radius: 1.5rem;
}

.calc__form--label {
  font-family: "Roboto Mono", monospace;
  font-size: 1rem;
  line-height: 1.25;
  padding: 1rem 2rem;
}

.calc__form--value {
  font-size: 5.5rem;
}

.calc__form--value sup {
  font-size: 3.25rem;
}

.calc__form--input {
  display: flex;
  flex-direction: column;
}

.calc__form--input .input {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 65px 45px 45px;
}

.calc__form--input .input:not(:last-child) {
  border-right: 1px solid var(--border-color);
}

input[type="number"] {
  border: none;
  border-bottom: 3.2px dotted var(--border-color);
  text-align: center;
  background-color: transparent;
  font-size: 4rem;
  line-height: 1;
  width: 100%;
}

[name="total"] {
  background: url("https://codepen.davidroessli.com/img/dollar.svg") left 15px no-repeat;
  padding-left: 40px;
}

[name="nb-people"] {
  background: url("https://codepen.davidroessli.com/img/people.svg") left 20px no-repeat;
  padding-left: 40px;
}

.calc__form--radio {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
  background-color: var(--tips-background-color);
  padding: 1.5rem 2rem;

  font-family: "Roboto Mono", monospace;
  font-size: 24px;
  font-weight: 700;
  line-height: 2rem;
  color: var(--discount-color);

  border: 1px solid var(--border-color);
  border-width: 1px 0;
}

.radio {
  display: grid;
}

.radio input {
  grid-area: 1 / 1 / 4 / 4;
}

.radio label {
  grid-area: 2 / 2 / 2 / 2;
  z-index: 2;
}

input[type="radio"] {
  -webkit-appearance: none;
  appearance: none;
  background-color: #fff;
  margin: 0;
  font: inherit;
  color: currentColor;
  width: 150px;
  height: 75px;
  border-radius: 18px;
  transform: translateY(-0.075em);

  display: grid;
  place-content: center;
}

input[type="radio"]::before {
  content: "";
  width: 150px;
  height: 75px;
  border-radius: 18px;
  transform: scale(0);
  transition: 120ms transform ease-in-out;
  box-shadow: inset 1em 1em var(--form-control-color);
}

input[type="radio"]:checked + label {
  color: white;
}

input[type="radio"]:checked::before {
  transform: scale(1);
  background-color: var(--discount-color);
}

input[type="radio"]:focus {
  outline: max(2px, 0.15em) solid currentColor;
  outline-offset: max(2px, 0.15em);
}

.calc__form--submit {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  padding: 2rem;
}

[name="submit"] {
  font-family: "Roboto Mono", monospace;
  font-size: 24px;
  font-weight: 700;
  line-height: 2rem;
  text-align: center;
  letter-spacing: 0.15em;
  color: #ffffff;

  background-color: var(--button-color);
  padding: 18px 75px;
  box-shadow: 0px 1.0717px 8.57359px rgba(0, 0, 0, 0.05);
  border-radius: 18px;
}

[name="submit"]:focus,
[name="submit"]:hover {
  background-color: var(--discount-color);
}

@media screen and (min-width: 600px) {
  .tip,
  .calc__form--input {
    flex-direction: row;
  }
  .calc__form--input .input {
    width: 60%;
  }
  .calc__form--input .input + .input {
    width: 40%;
  }
}

              
            
!

JS

              
                      const form = document.getElementById('calc__form');
      var tips = document.getElementsByName('tip');
      var total = document.querySelector('input[name="total"]');
      var nb = document.querySelector('input[name="nb-people"]');

      function calcTip(e) {

        for (i = 0; i < tips.length; i++) {
          if (tips[i].checked) {
            var tip = tips[i];
          }
        }

        var totalTip = document.querySelector('.tip__total--amount');
        var totalPerson = document.querySelector('.tip__pers--amount');

        totalTip.textContent = (total.value * tip.value).toFixed(2);
        totalPerson.textContent = (
          (total.value * tip.value) /
          nb.value
        ).toFixed(2);

        event.preventDefault();
      }

      form.addEventListener('submit', calcTip);
      total.addEventListener('change', calcTip);
      nb.addEventListener('change', calcTip);
      tips.forEach(radio => radio.addEventListener('change', calcTip));

              
            
!
999px

Console