AI CSS Animations

Notification Bell Toggle

Turn notifications on and the bell rides across, rings with a decaying swing while its clapper knocks the other way, and a red badge pops in. Off, a slash is drawn back over it.

Also known as: bell toggle switch css, notification on off switch, ringing bell animation.

HTML

<label class="tgl-notification-bell-toggle">
  <input type="checkbox" role="switch" aria-label="Notifications">
  <span class="tgl-notification-bell-toggle-track" aria-hidden="true">
    <span class="tgl-notification-bell-toggle-knob">
      <svg viewBox="0 0 24 24">
        <g class="tgl-notification-bell-toggle-bell">
          <path d="M12 3a1.5 1.5 0 0 0-1.5 1.5v.6A6 6 0 0 0 6 11v3.5L4.5 17v1h15v-1L18 14.5V11a6 6 0 0 0-4.5-5.9v-.6A1.5 1.5 0 0 0 12 3z"/>
          <path class="tgl-notification-bell-toggle-clapper" d="M10 19.3a2 2 0 0 0 4 0z"/>
        </g>
        <path class="tgl-notification-bell-toggle-cut" pathLength="1" d="M4 4l16 16"/>
        <path class="tgl-notification-bell-toggle-slash" pathLength="1" d="M4 4l16 16"/>
      </svg>
      <span class="tgl-notification-bell-toggle-badge"></span>
    </span>
  </span>
</label>

CSS

.tgl-notification-bell-toggle {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.tgl-notification-bell-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-notification-bell-toggle-track {
  position: relative;
  display: block;
  width: 5.6em;
  height: 2.6em;
  border-radius: 1.3em;
  background: color-mix(in srgb, currentColor 14%, transparent);
  transition: background-color 0.35s ease;
}
.tgl-notification-bell-toggle-knob {
  position: absolute;
  top: 0.3em;
  left: 0.3em;
  display: grid;
  place-items: center;
  width: 2em;
  height: 2em;
  border-radius: 50%;
  background: #fff;
  color: #9ca3af;
  box-shadow: 0 0.1em 0.25em rgba(0, 0, 0, 0.25);
  transition: transform 0.45s cubic-bezier(0.5, 1.3, 0.4, 1), color 0.3s ease;
}
.tgl-notification-bell-toggle-knob svg {
  display: block;
  width: 1.25em;
  height: 1.25em;
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}
.tgl-notification-bell-toggle-bell {
  fill: currentColor;
  stroke: none;
  transform-box: fill-box;
  transform-origin: 50% 0;
}
.tgl-notification-bell-toggle-clapper {
  transform-box: fill-box;
  transform-origin: 50% 0;
}
.tgl-notification-bell-toggle-cut,
.tgl-notification-bell-toggle-slash {
  stroke-dasharray: 1 2;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.3s ease;
}
.tgl-notification-bell-toggle-cut {
  stroke: #fff;
  stroke-width: 5;
}
.tgl-notification-bell-toggle-badge {
  position: absolute;
  top: 0.2em;
  right: 0.22em;
  width: 0.48em;
  height: 0.48em;
  border-radius: 50%;
  background: #ef4444;
  box-shadow: 0 0 0 0.09em #fff;
  transform: scale(0);
  transition: transform 0.2s ease;
}
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track { background: var(--c); }
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track .tgl-notification-bell-toggle-knob {
  transform: translateX(3em);
  color: var(--c);
}
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track .tgl-notification-bell-toggle-cut,
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track .tgl-notification-bell-toggle-slash {
  stroke-dashoffset: -1.05;
}
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track .tgl-notification-bell-toggle-bell {
  animation: tgl-notification-bell-toggle-ring 1s ease-in-out 0.3s;
}
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track .tgl-notification-bell-toggle-clapper {
  animation: tgl-notification-bell-toggle-clap 1s ease-in-out 0.34s;
}
.tgl-notification-bell-toggle input:checked + .tgl-notification-bell-toggle-track .tgl-notification-bell-toggle-badge {
  transform: scale(1);
  transition: transform 0.35s cubic-bezier(0.4, 2, 0.5, 1) 0.35s;
}
@keyframes tgl-notification-bell-toggle-ring {
  0%, 80%, 100% { transform: rotate(0deg); }
  10% { transform: rotate(22deg); }
  20% { transform: rotate(-18deg); }
  30% { transform: rotate(14deg); }
  40% { transform: rotate(-10deg); }
  50% { transform: rotate(6deg); }
  60% { transform: rotate(-3deg); }
  70% { transform: rotate(1deg); }
}
@keyframes tgl-notification-bell-toggle-clap {
  0%, 70%, 100% { transform: translateX(0); }
  10% { transform: translateX(-45%); }
  20% { transform: translateX(45%); }
  30% { transform: translateX(-35%); }
  40% { transform: translateX(25%); }
  50% { transform: translateX(-12%); }
  60% { transform: translateX(5%); }
}
.tgl-notification-bell-toggle input:focus-visible + .tgl-notification-bell-toggle-track {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

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