AI CSS Animations

Character Counter Typing Effect

A commit message types in while a counter and a small progress ring keep count, tick for tick. One registered integer drives the counter text, the ring and the steps() typing together.

Also known as: character count animation css, text counter typing, commit message counter.

Commit summary Fix login redirect

HTML

<div class="txt-character-counter-typing">
  <span class="txt-character-counter-typing-label">Commit summary</span>
  <span class="txt-character-counter-typing-line"><span class="txt-character-counter-typing-t">Fix login redirect</span><span class="txt-character-counter-typing-caret" aria-hidden="true"></span></span>
  <span class="txt-character-counter-typing-count" aria-hidden="true"><i></i></span>
</div>

CSS

@property --txt-character-counter-typing-n {
  syntax: "<integer>";
  initial-value: 0;
  inherits: true;
}
.txt-character-counter-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-grid;
  gap: 0.35em;
  box-sizing: border-box;
  width: 15em;
  padding: 0.75em 0.9em 0.6em;
  border-radius: 0.6em;
  font-size: calc(var(--txt-size, 32px) * 0.6);
  font-weight: 500;
  text-align: left;
  background: color-mix(in srgb, currentColor 5%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 16%, transparent);
  /* 18 characters: the width, the counter and the ring all step together */
  animation: txt-character-counter-typing-count 1.44s steps(18) 0.4s forwards;}
.txt-character-counter-typing-label {
  font-size: 0.75em;
  color: color-mix(in srgb, currentColor 50%, transparent);
}
.txt-character-counter-typing-line {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-weight: 600;
  white-space: pre;
}
.txt-character-counter-typing-t {
  display: inline-block;
  vertical-align: top;
  overflow: hidden;
  width: calc(var(--txt-character-counter-typing-n) * 1ch);
}
.txt-character-counter-typing-caret {
  display: inline-block;
  vertical-align: top;
  width: 0.09em;
  height: 1.1em;
  margin-top: 0.05em;
  background: var(--c);
  animation: txt-character-counter-typing-blink 1s step-end infinite;
}
.txt-character-counter-typing-count {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4em;
  font-size: 0.75em;
  font-variant-numeric: tabular-nums;
  color: color-mix(in srgb, currentColor 60%, transparent);
  counter-reset: txt-character-counter-typing-c var(--txt-character-counter-typing-n);
}
.txt-character-counter-typing-count::before {
  content: counter(txt-character-counter-typing-c) " / 50";
}
.txt-character-counter-typing-count i {
  width: 1.1em;
  height: 1.1em;
  border-radius: 50%;
  /* 7.2deg = one character of 50 */
  background: conic-gradient(var(--c) calc(var(--txt-character-counter-typing-n) * 7.2deg), color-mix(in srgb, currentColor 15%, transparent) 0);
  -webkit-mask: radial-gradient(farthest-side, transparent 55%, #000 58%);
  mask: radial-gradient(farthest-side, transparent 55%, #000 58%);
}
@keyframes txt-character-counter-typing-count {
  99.9%, to { --txt-character-counter-typing-n: 18; }
}
@keyframes txt-character-counter-typing-blink {
  50% { opacity: 0; }
}

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