AI CSS Animations

Mute Microphone Toggle

A video call mic button: live, it takes the accent and sends out soft ripples; muted, it turns red and a slash draws across the mic, cut out of the icon by a second stroke in the button color.

Also known as: mic mute button css, microphone on off toggle, unmute button.

HTML

<label class="tgl-mic-mute-toggle">
  <input type="checkbox" role="switch" aria-label="Microphone" checked>
  <span class="tgl-mic-mute-toggle-btn" aria-hidden="true">
    <svg viewBox="0 0 24 24">
      <path class="tgl-mic-mute-toggle-capsule" d="M12 3a3 3 0 0 0-3 3v5a3 3 0 0 0 6 0V6a3 3 0 0 0-3-3z"/>
      <path d="M6.5 11a5.5 5.5 0 0 0 11 0M12 16.5V20M9 20.5h6"/>
      <path class="tgl-mic-mute-toggle-cut" pathLength="1" d="M4.5 4.5l15 15"/>
      <path class="tgl-mic-mute-toggle-slash" pathLength="1" d="M4.5 4.5l15 15"/>
    </svg>
  </span>
</label>

CSS

.tgl-mic-mute-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-mic-mute-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-mic-mute-toggle-btn {
  --bg: #ef4444;
  position: relative;
  display: grid;
  place-items: center;
  width: 3.2em;
  height: 3.2em;
  border-radius: 50%;
  color: #fff;
  background: var(--bg);
  box-shadow: 0 0.2em 0.5em color-mix(in srgb, var(--bg) 40%, transparent);
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
/* ripples while the mic is live */
.tgl-mic-mute-toggle-btn::before,
.tgl-mic-mute-toggle-btn::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 0.12em solid var(--c);
  opacity: 0;
}
.tgl-mic-mute-toggle-btn svg {
  position: relative;
  display: block;
  width: 1.5em;
  height: 1.5em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}
.tgl-mic-mute-toggle-capsule {
  fill: currentColor;
  stroke: none;
}
.tgl-mic-mute-toggle-cut,
.tgl-mic-mute-toggle-slash {
  stroke-dasharray: 1 2;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.3s ease, stroke 0.3s ease;
}
.tgl-mic-mute-toggle-cut {
  stroke: var(--bg);
  stroke-width: 5.5;
}
.tgl-mic-mute-toggle input:checked + .tgl-mic-mute-toggle-btn { --bg: var(--c); }
.tgl-mic-mute-toggle input:checked + .tgl-mic-mute-toggle-btn::before,
.tgl-mic-mute-toggle input:checked + .tgl-mic-mute-toggle-btn::after {
  animation: tgl-mic-mute-toggle-ripple 2s cubic-bezier(0, 0.4, 0.4, 1) infinite;
}
.tgl-mic-mute-toggle input:checked + .tgl-mic-mute-toggle-btn::after { animation-delay: -1s; }
.tgl-mic-mute-toggle input:checked + .tgl-mic-mute-toggle-btn .tgl-mic-mute-toggle-cut,
.tgl-mic-mute-toggle input:checked + .tgl-mic-mute-toggle-btn .tgl-mic-mute-toggle-slash {
  stroke-dashoffset: -1.05;
}
@keyframes tgl-mic-mute-toggle-ripple {
  0% { transform: scale(1); opacity: 0.7; }
  100% { transform: scale(1.55); opacity: 0; }
}
.tgl-mic-mute-toggle:active .tgl-mic-mute-toggle-btn { transform: scale(0.95); }
.tgl-mic-mute-toggle input:focus-visible + .tgl-mic-mute-toggle-btn {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

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