Back to CSS Toggles & Switches Sliding Pill with Label Pure CSS
Share
HTML
<label class="tg-pill">
  <input class="tg-pill-input" type="checkbox" checked>
  <span class="tg-pill-track" aria-hidden="true">
    <span class="tg-pill-text tg-pill-on">ON</span>
    <span class="tg-pill-text tg-pill-off">OFF</span>
    <span class="tg-pill-thumb"></span>
  </span>
  <span class="tg-pill-label">Public profile</span>
</label>
CSS
.tg-pill {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  color: #f0eeff;
  user-select: none;
}
.tg-pill-input {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}
.tg-pill-track {
  position: relative;
  width: 76px;
  height: 28px;
  background: #2a2a30;
  border-radius: 999px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  transition: background 0.25s ease;
}
.tg-pill-text {
  position: relative;
  z-index: 2;
  text-align: center;
  transition: color 0.25s ease;
  pointer-events: none;
}
.tg-pill-on  { color: #7c6cff; }
.tg-pill-off { color: #6a6a7a; }
.tg-pill-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: calc(50% - 2px);
  height: calc(100% - 4px);
  background: #f0eeff;
  border-radius: 999px;
  z-index: 1;
  transition: left 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.tg-pill-input:checked ~ .tg-pill-track .tg-pill-on  { color: #1a1a1a; }
.tg-pill-input:checked ~ .tg-pill-track .tg-pill-off { color: #6a6a7a; }
.tg-pill-input:not(:checked) ~ .tg-pill-track .tg-pill-on  { color: #6a6a7a; }
.tg-pill-input:not(:checked) ~ .tg-pill-track .tg-pill-off { color: #1a1a1a; }
.tg-pill-input:not(:checked) ~ .tg-pill-track .tg-pill-thumb {
  left: 50%;
}
.tg-pill-input:focus-visible ~ .tg-pill-track {
  outline: 2px solid #7c6cff;
  outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
  .tg-pill-thumb,
  .tg-pill-text,
  .tg-pill-track { transition: none; }
}