AI CSS Animations

Monthly Yearly Pricing Toggle

The pricing page switch: Monthly and Yearly on either side of a track that stays colored, since both choices are valid, and a Save 20% badge that pops and tilts when yearly is picked.

Also known as: pricing toggle css, monthly annual switch, billing period toggle.

HTML

<label class="tgl-pricing">
  <input type="checkbox" role="switch" aria-label="Bill yearly">
  <span class="tgl-pricing-track" aria-hidden="true"></span>
  <span class="tgl-pricing-month">Monthly</span>
  <span class="tgl-pricing-year">Yearly</span>
  <span class="tgl-pricing-badge">Save 20%</span>
</label>

CSS

.tgl-pricing {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  align-items: center;
  gap: 0.7em;
  font-family: system-ui, sans-serif;
  font-weight: 600;
  white-space: nowrap;
}
.tgl-pricing input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-pricing-month { order: -1; }
.tgl-pricing-month,
.tgl-pricing-year {
  font-size: 0.95em;
  transition: opacity 0.3s ease;
}
.tgl-pricing-year { opacity: 0.45; }
.tgl-pricing-track {
  position: relative;
  width: 2.9em;
  height: 1.6em;
  border-radius: 0.8em;
  background: var(--c);
}
.tgl-pricing-track::after {
  content: "";
  position: absolute;
  top: 0.2em;
  left: 0.2em;
  width: 1.2em;
  height: 1.2em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.08em 0.2em rgb(0 0 0 / 0.25);
  transition: transform 0.35s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.tgl-pricing-badge {
  padding: 0.4em 0.75em;
  border-radius: 99em;
  font-size: 0.7em;
  font-weight: 700;
  background: color-mix(in srgb, var(--c) 14%, transparent);
  color: var(--c);
  opacity: 0.6;
  transform: scale(0.9);
  transition:
    transform 0.45s cubic-bezier(0.3, 1.8, 0.5, 1),
    opacity 0.3s ease,
    background-color 0.3s ease,
    color 0.3s ease;
}
.tgl-pricing input:checked + .tgl-pricing-track::after { transform: translateX(1.3em); }
.tgl-pricing input:checked ~ .tgl-pricing-month { opacity: 0.45; }
.tgl-pricing input:checked ~ .tgl-pricing-year { opacity: 1; }
.tgl-pricing input:checked ~ .tgl-pricing-badge {
  background: var(--c);
  color: #fff;
  opacity: 1;
  transform: rotate(-4deg) scale(1.05);
}
.tgl-pricing input:focus-visible + .tgl-pricing-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-pricing, .tgl-pricing *, .tgl-pricing::before, .tgl-pricing::after, .tgl-pricing *::before, .tgl-pricing *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --tgl-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --tgl-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 24px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar toggle switches