AI CSS Animations

Settings Row Toggle Switch

A full settings row: a title, a line of help text and a switch on the right, all one label so the whole row is clickable. The row tints with your accent while the setting is on, using :has().

Also known as: settings toggle list item, toggle switch with description, preference row switch.

HTML

<label class="tgl-settings-row">
  <span class="tgl-settings-row-text">
    <span class="tgl-settings-row-title">Push notifications</span>
    <span class="tgl-settings-row-hint">Replies, mentions and invites</span>
  </span>
  <input type="checkbox" role="switch" checked>
  <span class="tgl-settings-row-track" aria-hidden="true"></span>
</label>

CSS

.tgl-settings-row {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: flex;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  align-items: center;
  justify-content: space-between;
  gap: 1.5em;
  width: 17.5em;
  padding: 0.85em 1em;
  border-radius: 0.9em;
  font-family: system-ui, sans-serif;
  text-align: left;
  background: color-mix(in srgb, currentColor 4%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 12%, transparent);
  transition: background-color 0.25s ease, box-shadow 0.25s ease;
}
.tgl-settings-row input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-settings-row:hover { background: color-mix(in srgb, currentColor 7%, transparent); }
.tgl-settings-row:has(input:checked) {
  background: color-mix(in srgb, var(--c) 7%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 35%, transparent);
}
.tgl-settings-row-text {
  display: grid;
  gap: 0.2em;
}
.tgl-settings-row-title {
  font-size: 0.95em;
  font-weight: 600;
}
.tgl-settings-row-hint {
  font-size: 0.78em;
  color: color-mix(in srgb, currentColor 60%, transparent);
}
.tgl-settings-row-track {
  position: relative;
  flex: none;
  width: 2.8em;
  height: 1.7em;
  border-radius: 0.85em;
  background: color-mix(in srgb, currentColor 16%, transparent);
  transition: background-color 0.25s ease;
}
.tgl-settings-row-track::before {
  content: "";
  position: absolute;
  top: 0.15em;
  left: 0.15em;
  width: 1.4em;
  height: 1.4em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.1em 0.3em rgb(0 0 0 / 0.2);
  transition: transform 0.3s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.tgl-settings-row input:checked + .tgl-settings-row-track { background: var(--c); }
.tgl-settings-row input:checked + .tgl-settings-row-track::before { transform: translateX(1.1em); }
.tgl-settings-row input:focus-visible + .tgl-settings-row-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-settings-row, .tgl-settings-row *, .tgl-settings-row::before, .tgl-settings-row::after, .tgl-settings-row *::before, .tgl-settings-row *::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