AI CSS Animations

AI Prompt Typing Animation

A prompt types itself into an AI input box, then the send button lights up in the accent color with a small pop. Letters are spans with staggered delays, so any font works.

Also known as: ai chat input animation, prompt box typing effect, animated ai search bar.

HTML

<div class="txt-ai-prompt-typing" aria-label="Plan a 3-day trip to Kyoto">
  <svg class="txt-ai-prompt-typing-spark" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 2.5l2.1 7.4 7.4 2.1-7.4 2.1-2.1 7.4-2.1-7.4-7.4-2.1 7.4-2.1z"/></svg>
  <span class="txt-ai-prompt-typing-field" aria-hidden="true">
    <span class="txt-ai-prompt-typing-ph">Ask anything...</span>
    <span class="txt-ai-prompt-typing-t"><span style="--i: 0">P</span><span style="--i: 1">l</span><span style="--i: 2">a</span><span style="--i: 3">n</span><span style="--i: 4">&nbsp;</span><span style="--i: 5">a</span><span style="--i: 6">&nbsp;</span><span style="--i: 7">3</span><span style="--i: 8">-</span><span style="--i: 9">d</span><span style="--i: 10">a</span><span style="--i: 11">y</span><span style="--i: 12">&nbsp;</span><span style="--i: 13">t</span><span style="--i: 14">r</span><span style="--i: 15">i</span><span style="--i: 16">p</span><span style="--i: 17">&nbsp;</span><span style="--i: 18">t</span><span style="--i: 19">o</span><span style="--i: 20">&nbsp;</span><span style="--i: 21">K</span><span style="--i: 22">y</span><span style="--i: 23">o</span><span style="--i: 24">t</span><span style="--i: 25">o</span></span>
  </span>
  <span class="txt-ai-prompt-typing-send" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M12 19V5M5.5 11.5 12 5l6.5 6.5"/></svg></span>
</div>

CSS

.txt-ai-prompt-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;
  align-items: center;
  gap: 0.55em;
  box-sizing: border-box;
  width: 17em;
  padding: 0.4em 0.4em 0.4em 0.8em;
  border-radius: 99em;
  font-size: calc(var(--txt-size, 32px) * 0.62);
  font-weight: 500;
  background: color-mix(in srgb, currentColor 5%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 16%, transparent);}
.txt-ai-prompt-typing-spark {
  display: block;
  flex: none;
  width: 1.1em;
  height: 1.1em;
  fill: var(--c);
  animation: txt-ai-prompt-typing-twinkle 0.6s cubic-bezier(0.3, 1.5, 0.5, 1) 2s;
}
.txt-ai-prompt-typing-field {
  display: grid;
  flex: 1;
  white-space: nowrap;
  text-align: left;
}
.txt-ai-prompt-typing-ph,
.txt-ai-prompt-typing-t {
  grid-area: 1 / 1;
}
.txt-ai-prompt-typing-ph {
  color: color-mix(in srgb, currentColor 45%, transparent);
  animation: txt-ai-prompt-typing-gone 0s 0.5s forwards;
}
.txt-ai-prompt-typing-t span {
  position: relative;
  opacity: 0;
  animation: txt-ai-prompt-typing-key 0s calc(0.5s + var(--i) * 50ms) forwards;
}
/* each letter holds the caret for one beat; the last one keeps it */
.txt-ai-prompt-typing-t span::after {
  content: "";
  position: absolute;
  top: 0.1em;
  bottom: 0.05em;
  right: -0.06em;
  width: 0.08em;
  background: var(--c);
  opacity: 0;
  animation: txt-ai-prompt-typing-caret 50ms step-end calc(0.5s + var(--i) * 50ms);
}
.txt-ai-prompt-typing-t span:last-child::after {
  animation: txt-ai-prompt-typing-blink 1s step-end calc(0.5s + var(--i) * 50ms) infinite;
}
.txt-ai-prompt-typing-send {
  display: grid;
  flex: none;
  place-items: center;
  width: 1.9em;
  height: 1.9em;
  border-radius: 50%;
  color: color-mix(in srgb, currentColor 45%, transparent);
  background: color-mix(in srgb, currentColor 10%, transparent);
  animation: txt-ai-prompt-typing-ready 0.45s cubic-bezier(0.3, 1.6, 0.5, 1) 1.95s forwards;
}
.txt-ai-prompt-typing-send svg {
  display: block;
  width: 1em;
  height: 1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
@keyframes txt-ai-prompt-typing-gone {
  to { visibility: hidden; }
}
@keyframes txt-ai-prompt-typing-key {
  to { opacity: 1; }
}
@keyframes txt-ai-prompt-typing-caret {
  0%, 100% { opacity: 1; }
}
@keyframes txt-ai-prompt-typing-blink {
  0% { opacity: 1; }
  50% { opacity: 0; }
}
@keyframes txt-ai-prompt-typing-ready {
  0% { transform: scale(1); }
  50% { transform: scale(1.18); }
  100% { transform: scale(1); color: #fff; background: var(--c); }
}
@keyframes txt-ai-prompt-typing-twinkle {
  50% { transform: rotate(45deg) scale(1.25); }
}

@media (prefers-reduced-motion: reduce) {
  .txt-ai-prompt-typing, .txt-ai-prompt-typing *, .txt-ai-prompt-typing::before, .txt-ai-prompt-typing::after, .txt-ai-prompt-typing *::before, .txt-ai-prompt-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