Intro
I love this little effect. It has that old typewriter feel, and reminds me of crime shows where a hacker types furiously into a terminal and says “I’m in.” I keep finding fun places to use it.
In this post we’ll build it three ways: with steps() and a monospace font, with spans and delays for any font, and with JavaScript to cycle through different messages. Then we’ll add a blinking cursor and a backspace effect, and figure out which approach fits what you’re making.
Using steps() and monospace
This is the approach my laptop uses, and the simplest of the three. It’s pure CSS on a single element, with no extra markup. The catch is that it only works on one line of monospace text, and once you see how it works, you’ll see why.
How it works
The trick is to put the whole message in a box that’s too narrow to show it, then widen the box a little at a time. Start the box at width: 0. overflow: hidden hides anything that doesn’t fit, and white-space: nowrap keeps the text on one line, so instead of wrapping, the letters wait just out of sight. Then animate the width out to the length of the text. “Access granted” is 14 characters, spaces included, so the box grows to 14ch.
Access granted
.typing {
display: inline-block;
width: 0;
overflow: hidden;
white-space: nowrap;
font-family: menlo, monaco, monospace;
border-right: 2px solid;
animation:
type 4s steps(14) infinite,
blink 1s step-end infinite;
}
@keyframes type {
60%, 100% { width: 14ch; } /* type, then hold */
}
@keyframes blink {
50% { border-color: transparent; }
}The cursor is one border-right line, so it’s already in there. We’ll dig into cursors later on.
Why steps()?
On its own, a width animation glides, uncovering a sliver of a letter at a time. steps(14) breaks it into 14 equal jumps instead, one per character. That’s what makes it look typed instead of wiped. The number in steps() should match the number of characters. Too few and letters show up in pairs. Too many and some steps stop halfway through a letter.
Why monospace?
Monospace has that classic typewriter feel, and it’s the font we see when we type in a terminal, which made it perfect for my CSS laptop animation. But the real reason I reach for it here is simpler. In a monospace font, a skinny i takes up exactly as much space as a wide W.
In CSS, 1ch is the width of the digit 0. In a monospace font, every character is that same width, spaces included. So if we reveal a five-letter word over 5ch in five steps, each step uncovers exactly one letter.
Swap in a regular font and the steps are still equal, but the letters aren’t. One step might cut a W in half, while the next shows an i and a sliver of the letter after it. So monospace is what makes steps() line up with the letters.
But if you want to use another font, there is another way. Wrap each letter in its own span and reveal them one by one on a delay. That’s how the lede at the top of this page types itself out, and we’ll cover that method in the next section.
Using spans and delays
How it works
Instead of uncovering a box, this approach gives every letter its own span. They all start hidden, and each one appears a little later than the one before it. Nothing gets measured here, so none of the monospace math applies. Every letter shows up whole, whatever its width, which makes it perfect for any font. And since it’s still regular text, it wraps onto the next line like any other paragraph.
Any font works, and it wraps onto a new line when it runs out of room.
Each span gets a number, --i, for its place in the line. The delay is that number times 50ms, so the first letter shows up right away, the second at 50ms, the third at 100ms, and so on.
<p class="typing">
<span style="--i: 0">H</span>
<span style="--i: 1">e</span>
<span style="--i: 2">l</span>
<span style="--i: 3">l</span>
<span style="--i: 4">o</span>
</p>.typing span {
opacity: 0;
animation: show 1ms calc(var(--i) * 50ms) forwards;
}
@keyframes show {
to { opacity: 1; }
}The animation itself barely lasts a millisecond. All the timing lives in the delay, and forwards keeps each letter visible once it appears.
The catch
The catch is all those spans. Every letter needs its own, with its own number, so even one sentence turns into a long pile of markup, and longer text gets tedious fast. A few lines of code can split the text for you, and the lede at the top of this page is split up in React. But once the text gets longer, or needs to change, it’s just easier to let JavaScript handle the typing too. That’s the next approach.
Using JavaScript
Both CSS versions need to know the text ahead of time, and the spans version needs markup for every single letter. JavaScript needs neither. It takes whatever text is in a variable and adds it one letter at a time on a timer, so it can type anything: a single word, a full paragraph, or text you don’t know until the page loads, like a random quote or a reader’s name.
Here’s the same “Access granted” from the steps() section, typed with JavaScript instead.
Access granted
const typing = document.querySelector(".typing");
const text = "Access granted";
let length = 0;
let timer;
function tick() {
length += 1;
typing.textContent = text.slice(0, length);
if (length < text.length) {
timer = setTimeout(tick, 90); // next letter
} else {
length = 0;
timer = setTimeout(tick, 1600); // hold, then start over
}
}
function stop() {
clearTimeout(timer);
}
tick();No ch math, no step count, no spans. Change the text and it just works. And since the timing is all code, you can play with it. Hold longer, type faster, or add a little randomness to each wait so it feels more like a person typing.
Blinking cursor
A blinking cursor is what really sells the effect. It follows the last letter as the text types, then keeps blinking when the typing pauses, like it’s waiting for the next keystroke. Under the hood, the typing and the blinking are two separate animations, each with its own timing. Each version uses a slightly different trick to keep the cursor in the right spot.
With steps()
The box already grows as the text types, so its right border is always sitting right after the newest letter. Give it a border-right and make it blink. step-end makes the blink snap on and off instead of fading, like a real cursor.
.typing {
border-right: 2px solid;
animation:
type 4s steps(14) infinite,
blink 1s step-end infinite;
}
@keyframes blink {
50% { border-color: transparent; }
}With spans
There’s no growing box here, so there’s nothing to put a border on. Instead, each letter wears the cursor for a moment, then hands it to the next one. I use a box-shadow instead of a border, because a shadow doesn’t take up space. A border would nudge the letters over every time it moved. The last letter keeps the cursor and blinks once typing is done.
.typing span {
opacity: 0;
animation:
show 1ms calc(var(--i) * 50ms) forwards,
cursor 50ms steps(1) calc(var(--i) * 50ms);
}
/* Each letter wears the cursor for one step, then hands it off. */
@keyframes cursor {
from { box-shadow: 2px 0 0 currentColor; }
}With JavaScript
The easiest of the three. The cursor is its own element, right after the text. As the text grows and shrinks, the cursor just follows along.
<p>
<span class="typing"></span><span class="cursor"></span>
</p>
.cursor {
display: inline-block;
width: 0.55ch;
height: 1.1em;
background: currentColor;
animation: blink 1s step-end infinite;
}
@keyframes blink {
50% { opacity: 0; }
}Type, pause, backspace
This is a fun way to level up the effect. Type the message, let it sit for a beat, then backspace it one letter at a time. That little pause makes it feel like a real person is typing, rereading what they wrote, and changing their mind.
In the CSS versions, nothing actually gets deleted. The full text stays on the page, and we just hide the letters again from right to left. How we do that depends on how we revealed them in the first place. JavaScript is the exception. It really does remove each letter.
With steps()
The box can get narrower just as easily as it got wider, so the same width animation runs backward. The keyframes are the timeline. The text types until 40%, holds until 70%, then erases by 85%, a little faster than it typed, like a real backspace. Then it rests before starting over. steps() applies to each stretch between keyframes, so typing and erasing both move one letter at a time, and the hold stays perfectly still.
You look great
.backspace {
width: 0;
overflow: hidden;
white-space: nowrap;
font-family: menlo, monaco, monospace;
border-right: 2px solid;
animation:
type-and-erase 5s steps(14) infinite,
blink 1s step-end infinite;
}
@keyframes type-and-erase {
0% { width: 0; } /* start empty */
40% { width: 14ch; } /* type */
70% { width: 14ch; } /* hold */
85% { width: 0; } /* backspace, faster */
100% { width: 0; } /* rest */
}
@keyframes blink {
50% { border-color: transparent; }
}With spans
It’s possible, but it takes more work. Each letter gets two animations. The first reveals it, and the second hides it again after a pause. --count, the total number of letters, staggers the hide delays in reverse, so the last letter to appear is the first to disappear. This version plays once. To loop it, every letter has to finish its reveal, pause, and backspace in time to start the next round together, and typing a new message adds another whole sequence to line up. It gets complicated fast.
.typing span {
opacity: 0;
animation:
show 1ms calc(var(--i) * 50ms) forwards,
hide 1ms calc(3s + (var(--count) - var(--i)) * 30ms) forwards;
}
@keyframes hide {
to { opacity: 0; }
}With JavaScript
This is where JavaScript shines. Backspacing is just removing a letter from the end, and typing a different message next is just the next item in a list. Every tick, it adds or removes one letter. When a message is finished, it holds it, then starts deleting, a little faster than it typed. When it’s empty, it moves on to the next message.
You look great. Nice shoes. Drink some water
const typing = document.querySelector(".typing");
const messages = ["You look great", "Nice shoes", "Drink some water"];
let message = 0;
let length = 0;
let deleting = false;
let timer;
function tick() {
const text = messages[message];
length += deleting ? -1 : 1;
typing.textContent = text.slice(0, length);
let wait = deleting ? 45 : 90; // backspace faster than typing
if (!deleting && length === text.length) {
deleting = true;
wait = 1400; // hold the finished message
} else if (deleting && length === 0) {
deleting = false;
message = (message + 1) % messages.length; // on to the next one
}
timer = setTimeout(tick, wait);
}
function stop() {
clearTimeout(timer);
}
tick();Which one should you use?
steps()when you have a short, fixed line of monospace text. It’s the simplest version, and it’s what I used for the little laptop on my homepage.- Spans when you want to use a regular font or let the text wrap. Each letter gets its own span, so there’s more markup, but CSS still handles all the timing.
- JavaScript when you don’t know the text ahead of time or want to cycle through a list of messages. Typing, pausing, backspacing, and starting the next message is just a few lines, with no separate set of delays to line up for each phrase.
JavaScript is the most flexible, but it’s more to look after. The timer has to be stopped when you’re done with it, or it keeps running in the background. And nothing types until the script loads. Both CSS versions start the moment the page appears, and they still work if JavaScript doesn’t.
Keep it readable
It’s fun to watch, but it shouldn’t get in the way of reading. For instructions, longer passages, or anything someone needs to act on, you should show the full text right away.
Screen readers should get the whole message at once, not one letter at a time. Split letters can be read out one by one, so I hide the animated version with aria-hidden and put the full text next to it for screen readers only. Respect reduced motion. If someone has it turned on, skip the animation and show the text fully typed.