CSS Typewriter Effect

A typewriter animation that types a word, holds, deletes it, and blinks a caret beside a fixed sentence.


CSS Typewriter Effect


The CSS typewriter effect types a short phrase one character at a time, pauses, deletes it, and types the next phrase. People search for it because a hero headline that finishes its own sentence feels like a product thinking, and because the classic CSS version is a single @keyframes width trick that breaks the moment the words are different lengths.

This snippet keeps the caret in CSS and the changing word in a few lines of JavaScript. The fixed words “We ship” stay in the HTML. Only the rotating word is written into a span. A polite live region announces the full sentence when a word finishes, so the loop is not a silent animation.

Related reading: the typing indicator is the three-dot chat version of the same idea, and the text scramble is the version where letters lock in from noise.

When a typewriter belongs on a page

Use it for a list of short, parallel promises: “motion”, “systems”, “quiet UI”. Each word should fit the same grammatical slot. “We ship motion” and “We ship the detail” work. “We ship” followed by a full sentence does not, because the line jumps and the caret chases a paragraph.

One typewriter per view. A second loop lower on the page splits attention and makes the first one feel like a gimmick.

HTML

<h1>
  We ship
  <span class="word">
    <span id="typed"></span>
    <span class="caret" aria-hidden="true"></span>
  </span>
</h1>
<p id="live" class="sr" aria-live="polite"></p>

The caret is aria-hidden. The live region is visually hidden and updates only when a word is complete, so a screen reader hears “We ship motion” once per cycle.

CSS for the blinking caret

.word {
  display: inline-flex;
  align-items: center;
  background: #1c1917;
  color: #f6f1c8;
  border-radius: 999px;
  padding: 0.05em 0.38em 0.08em;
}
.caret {
  width: 2px;
  height: 0.78em;
  margin-left: 2px;
  background: currentColor;
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

steps(1) makes a true blink. A linear fade looks like a pulse, which is a different effect. The pill background grows with the word because the span is inline-flex and the typed text changes its width. You do not need a fixed ch width unless you want the pill to stop resizing. A min-width keeps the pill from collapsing while the word deletes to empty.

JavaScript loop

const words = ["motion.", "systems.", "quiet UI.", "the detail."];
let i = 0, count = 0, deleting = false;

function tick() {
  const word = words[i];
  count += deleting ? -1 : 1;
  typed.textContent = word.slice(0, count);
  if (!deleting && count === word.length) {
    live.textContent = "We ship " + word;
    deleting = true;
    setTimeout(tick, 1100);
    return;
  }
  if (deleting && count === 0) {
    deleting = false;
    i = (i + 1) % words.length;
  }
  setTimeout(tick, deleting ? 42 : 78);
}

Typing is slower than deleting. The hold at the end of a word is the moment a person can read it. If you shorten that hold below about a second, the headline becomes a flicker.

A pure CSS typewriter, and where it fails

The CSS-only version sets the text in the HTML, gives the element width: 0, overflow: hidden, white-space: nowrap, and animates width to 100% or to a ch measure, with steps(n) matching the character count. It is perfect for one fixed sentence, such as a code comment in a tutorial. It is the wrong tool for a rotating list, because each word needs its own step count and its own width. The JavaScript loop above is the version that matches the search “typewriter effect with multiple words”.

Accessibility

If prefers-reduced-motion: reduce matches, write the first word immediately and leave the caret still. The sentence is then “We ship motion.” which is a complete headline. Do not leave the span empty for people who have asked for less motion.

Keep the typed color and the pill background at a strong contrast. Cream #f6f1c8 on #1c1917 is the pair in the demo.

Questions about the typewriter effect

How do I make a typewriter effect in CSS?

For one sentence, animate the width of an overflow-hidden line from 0 to its full width with steps() equal to the character count, and blink a caret with a stepped opacity animation. For several words, type and delete in JavaScript and keep only the caret in CSS.

Why does my typewriter jump between words?

The container is sizing to each string. Give the word a min-width in ch, or reserve the width of the longest word, if the jump bothers the layout. Letting the pill grow is a valid look. Letting the whole page reflow is not.

Should the caret be in the accessibility tree?

No. Hide it with aria-hidden. Announce the finished phrase with an aria-live="polite" region when the word completes.