AI CSS Animations

OTP Code Input Animation

Six code boxes fill one digit at a time while the focus ring and caret step from box to box, then a small wave marks the code as complete. Each box reads its index from --i.

Also known as: verification code animation, pin input typing css, one time code input.

HTML

<div class="txt-otp-code-typing" aria-label="Verification code 482916">
  <span class="txt-otp-code-typing-box" style="--i: 0" aria-hidden="true"><b>4</b></span>
  <span class="txt-otp-code-typing-box" style="--i: 1" aria-hidden="true"><b>8</b></span>
  <span class="txt-otp-code-typing-box" style="--i: 2" aria-hidden="true"><b>2</b></span>
  <span class="txt-otp-code-typing-box" style="--i: 3" aria-hidden="true"><b>9</b></span>
  <span class="txt-otp-code-typing-box" style="--i: 4" aria-hidden="true"><b>1</b></span>
  <span class="txt-otp-code-typing-box" style="--i: 5" aria-hidden="true"><b>6</b></span>
</div>

CSS

.txt-otp-code-typing {
  --c: var(--txt-color, #3b82f6);
  font-size: var(--txt-size, 32px);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  display: inline-flex;
  gap: 0.3em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: calc(var(--txt-size, 32px) * 0.8);
  font-variant-numeric: tabular-nums;}
.txt-otp-code-typing-box {
  position: relative;
  display: grid;
  place-items: center;
  width: 1.35em;
  height: 1.7em;
  border-radius: 0.3em;
  background: color-mix(in srgb, currentColor 4%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 22%, transparent);
  outline: 0.1em solid transparent;
  outline-offset: 0.08em;
  /* box i has focus for one beat, then the whole row confirms */
  animation:
    txt-otp-code-typing-focus 0.32s step-end calc(0.3s + var(--i) * 0.32s),
    txt-otp-code-typing-done 0.5s cubic-bezier(0.3, 1.4, 0.5, 1) calc(2.45s + var(--i) * 60ms) forwards;
}
.txt-otp-code-typing-box::after {
  content: "";
  position: absolute;
  top: 25%;
  bottom: 25%;
  left: 50%;
  width: 0.07em;
  margin-left: -0.035em;
  background: var(--c);
  visibility: hidden;
  animation: txt-otp-code-typing-caret 0.32s step-end calc(0.3s + var(--i) * 0.32s);
}
.txt-otp-code-typing-box b {
  font-weight: 700;
  animation: txt-otp-code-typing-digit 0.3s cubic-bezier(0.3, 1.5, 0.5, 1) calc(0.62s + var(--i) * 0.32s) both;
}
@keyframes txt-otp-code-typing-focus {
  0%, 100% { outline-color: color-mix(in srgb, var(--c) 45%, transparent); }
}
@keyframes txt-otp-code-typing-caret {
  0%, 100% { visibility: visible; }
}
@keyframes txt-otp-code-typing-digit {
  from { opacity: 0; transform: translateY(0.3em) scale(0.6); }
}
@keyframes txt-otp-code-typing-done {
  50% { transform: translateY(-0.15em); }
  to {
    color: var(--c);
    background: color-mix(in srgb, var(--c) 10%, transparent);
    box-shadow: inset 0 0 0 1px var(--c);
  }
}

@media (prefers-reduced-motion: reduce) {
  .txt-otp-code-typing, .txt-otp-code-typing *, .txt-otp-code-typing::before, .txt-otp-code-typing::after, .txt-otp-code-typing *::before, .txt-otp-code-typing *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --txt-color on the element or a parent. Currently previewing #3b82f6.
  • Font size: set --txt-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 40px.
  • 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 text animations