About this snippet

Allow users to switch between monthly and yearly pricing with this smooth toggle component. A simple JavaScript function updates the active state, which helps you upsell annual plans effectively.

Size
1.9 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
430
<div class="pricing-toggle">
  <span id="monthly" class="label active">Monthly</span>
  <div class="toggle-bg" onclick="togglePricing()">
    <div class="toggle-circle" id="circle"></div>
  </div>
  <span id="yearly" class="label">Yearly <span class="discount">-20%</span></span>
</div>
.pricing-toggle {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  display: flex;
  align-items: center;
  gap: 15px;
  background: #f9fafb;
  padding: 10px 20px;
  border-radius: 50px;
  display: inline-flex;
  border: 1px solid #e5e7eb;
}
.toggle-bg {
  width: 50px;
  height: 26px;
  background: #e5e7eb;
  border-radius: 50px;
  position: relative;
  cursor: pointer;
  transition: background 0.3s;
}
.toggle-circle {
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  position: absolute;
  top: 3px;
  left: 3px;
  transition: transform 0.3s;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.label {
  font-weight: 500;
  color: #9ca3af;
  cursor: pointer;
  transition: color 0.3s;
}
.label.active {
  color: #1f2937;
  font-weight: 600;
}
.discount {
  font-size: 0.7rem;
  background: #dbeafe;
  color: #2563eb;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 5px;
}
/* Active State for Toggle */
.pricing-toggle.yearly .toggle-bg {
  background: #3b82f6;
}
.pricing-toggle.yearly .toggle-circle {
  transform: translateX(24px);
}
function togglePricing() {
  const container = document.querySelector(".pricing-toggle");
  const mLabel = document.getElementById("monthly");
  const yLabel = document.getElementById("yearly");
  
  container.classList.toggle("yearly");
  
  if (container.classList.contains("yearly")) {
    mLabel.classList.remove("active");
    yLabel.classList.add("active");
    // Add logic to update prices here
  } else {
    mLabel.classList.add("active");
    yLabel.classList.remove("active");
  }
}
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save