AI CSS Animations

Smooth Caret Typing Effect

Letters appear per keystroke while the caret glides to its new spot instead of jumping, then blinks with a soft fade, like a code editor with smooth caret on. Two animations make the glide.

Also known as: smooth cursor animation css, gliding text cursor, editor smooth caret.

Type smoothly

HTML

<div class="txt-smooth-caret-typing"><span class="txt-smooth-caret-typing-t">Type smoothly</span><span class="txt-smooth-caret-typing-caret" aria-hidden="true"></span></div>

CSS

.txt-smooth-caret-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;
  position: relative;
  display: inline-block;
  width: calc(13ch + 0.2em);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-weight: 600;
  text-align: left;
  white-space: pre;}
.txt-smooth-caret-typing-t {
  display: inline-block;
  vertical-align: top;
  overflow: hidden;
  width: 0;
  /* 13 characters, one every 100ms */
  animation: txt-smooth-caret-typing-type 1.3s steps(13, jump-start) 0.4s forwards;
}
.txt-smooth-caret-typing-caret {
  position: absolute;
  top: 0.1em;
  left: 0;
  width: 0.08em;
  height: 1em;
  border-radius: 0.04em;
  background: var(--c);
  /* translate jumps one ch per key; transform starts each jump 1ch behind and eases it in */
  animation:
    txt-smooth-caret-typing-step 1.3s steps(13, jump-start) 0.4s forwards,
    txt-smooth-caret-typing-glide 0.1s cubic-bezier(0.2, 0.7, 0.3, 1) 0.4s 13 forwards,
    txt-smooth-caret-typing-fade 0.5s ease-in-out 1.9s infinite alternate;
}
@keyframes txt-smooth-caret-typing-type {
  to { width: 13ch; }
}
@keyframes txt-smooth-caret-typing-step {
  to { translate: 13ch; }
}
@keyframes txt-smooth-caret-typing-glide {
  from { transform: translateX(-1ch); }
  to { transform: translateX(0); }
}
@keyframes txt-smooth-caret-typing-fade {
  0%, 40% { opacity: 1; }
  100% { opacity: 0.05; }
}

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