AI CSS Animations

Show Password Toggle

A password field with an eye switch: the closed eye opens with a springy blink and a scan line wipes the dots into readable text. The field text is a demo; in a form, flip the input's type.

Also known as: show hide password eye icon, password visibility toggle css, eye toggle.

HTML

<label class="tgl-show-password-toggle">
  <input type="checkbox" role="switch" aria-label="Show password">
  <span class="tgl-show-password-toggle-field" aria-hidden="true">
    <span class="tgl-show-password-toggle-value">
      <span class="tgl-show-password-toggle-dots">&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;</span>
      <span class="tgl-show-password-toggle-text">hunter2!</span>
    </span>
    <span class="tgl-show-password-toggle-eye">
      <svg viewBox="0 0 24 24">
        <g class="tgl-show-password-toggle-open">
          <path d="M2.5 12S6 5.5 12 5.5 21.5 12 21.5 12 18 18.5 12 18.5 2.5 12 2.5 12z"/>
          <circle cx="12" cy="12" r="2.8"/>
        </g>
        <path class="tgl-show-password-toggle-shut" d="M3 10.5s3.5 5 9 5 9-5 9-5M5.8 13.6l-1.5 2M12 15.5v2.4M18.2 13.6l1.5 2"/>
      </svg>
    </span>
  </span>
</label>

CSS

.tgl-show-password-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;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.tgl-show-password-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-show-password-toggle-field {
  display: flex;
  align-items: center;
  gap: 0.5em;
  box-sizing: border-box;
  width: 10.5em;
  height: 2.6em;
  padding: 0 0.35em 0 0.9em;
  border: 1px solid color-mix(in srgb, currentColor 22%, transparent);
  border-radius: 0.6em;
  background: color-mix(in srgb, currentColor 4%, transparent);
  transition: border-color 0.3s ease;
}
.tgl-show-password-toggle-value {
  position: relative;
  display: grid;
  flex: 1;
  align-items: center;
  height: 1.4em;
  overflow: hidden;
  font-size: 0.95em;
}
.tgl-show-password-toggle-dots,
.tgl-show-password-toggle-text {
  grid-area: 1 / 1;
  white-space: nowrap;
  transition: clip-path 0.55s cubic-bezier(0.6, 0, 0.3, 1);
}
.tgl-show-password-toggle-dots {
  letter-spacing: 0.1em;
  clip-path: inset(0 0 0 0);
}
.tgl-show-password-toggle-text {
  letter-spacing: 0.02em;
  clip-path: inset(0 100% 0 0);
}
/* the scan line rides the edge of the wipe */
.tgl-show-password-toggle-value::after {
  content: "";
  position: absolute;
  inset: 0.1em 0;
  background: linear-gradient(90deg, var(--c) 0 0.12em, transparent 0);
  opacity: 0;
}
.tgl-show-password-toggle-eye {
  display: grid;
  place-items: center;
  flex: none;
  width: 1.9em;
  height: 1.9em;
  border-radius: 0.45em;
  color: color-mix(in srgb, currentColor 60%, transparent);
  transition: color 0.3s ease, background-color 0.2s ease;
}
.tgl-show-password-toggle-eye svg {
  display: block;
  width: 1.3em;
  height: 1.3em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tgl-show-password-toggle-open {
  transform-box: fill-box;
  transform-origin: center;
  transform: scaleY(0.1);
  opacity: 0;
  transition: transform 0.2s ease, opacity 0.15s ease;
}
.tgl-show-password-toggle-shut { transition: opacity 0.2s ease; }
.tgl-show-password-toggle:hover .tgl-show-password-toggle-eye { background: color-mix(in srgb, currentColor 8%, transparent); }
.tgl-show-password-toggle input:checked + .tgl-show-password-toggle-field .tgl-show-password-toggle-dots { clip-path: inset(0 0 0 100%); }
.tgl-show-password-toggle input:checked + .tgl-show-password-toggle-field .tgl-show-password-toggle-text { clip-path: inset(0 0 0 0); }
.tgl-show-password-toggle input:checked + .tgl-show-password-toggle-field .tgl-show-password-toggle-value::after {
  animation: tgl-show-password-toggle-scan 0.55s cubic-bezier(0.6, 0, 0.3, 1);
}
@keyframes tgl-show-password-toggle-scan {
  0% { transform: translateX(0); opacity: 1; }
  85% { opacity: 1; }
  100% { transform: translateX(96%); opacity: 0; }
}
.tgl-show-password-toggle input:checked + .tgl-show-password-toggle-field .tgl-show-password-toggle-eye { color: var(--c); }
.tgl-show-password-toggle input:checked + .tgl-show-password-toggle-field .tgl-show-password-toggle-open {
  transform: scaleY(1);
  opacity: 1;
  transition: transform 0.4s cubic-bezier(0.3, 1.8, 0.5, 1), opacity 0.1s ease;
}
.tgl-show-password-toggle input:checked + .tgl-show-password-toggle-field .tgl-show-password-toggle-shut { opacity: 0; }
.tgl-show-password-toggle input:focus-visible + .tgl-show-password-toggle-field {
  border-color: var(--c);
  outline: 2px solid color-mix(in srgb, var(--c) 35%, transparent);
  outline-offset: 0.15em;
}

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