Three lines type one after another, and the caret hops down to each new line when the last one is done. Each line carries its length and start time as custom properties.
Also known as: typewriter multiple lines css, typing animation two lines, sequential typing text.
<div class="txt-multi-line-typewriter"> <span class="txt-multi-line-typewriter-line" style="--n: 12; --d: 0.2s; --hold: 1.4s">Hi, I'm Sam.</span> <span class="txt-multi-line-typewriter-line" style="--n: 12; --d: 1.6s; --hold: 1.4s">I design and</span> <span class="txt-multi-line-typewriter-line" style="--n: 18; --d: 3s">build for the web.</span> </div>
.txt-multi-line-typewriter {
--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-block;
width: calc(18ch + 0.1em);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-weight: 600;
line-height: 1.35;
text-align: left;}
.txt-multi-line-typewriter-line {
display: block;
box-sizing: content-box;
width: 0;
overflow: hidden;
white-space: pre;
border-right: 0.07em solid transparent;
/* --n = characters in the line, --d = when it starts, --hold = how long it keeps the caret */
animation:
txt-multi-line-typewriter-type calc(var(--n) * 75ms) steps(var(--n)) var(--d) forwards,
txt-multi-line-typewriter-caret var(--hold) step-end var(--d);
}
.txt-multi-line-typewriter-line:first-child {
color: var(--c);
}
.txt-multi-line-typewriter-line:last-child {
animation:
txt-multi-line-typewriter-type calc(var(--n) * 75ms) steps(var(--n)) var(--d) forwards,
txt-multi-line-typewriter-blink 0.9s step-end var(--d) infinite;
}
@keyframes txt-multi-line-typewriter-type {
99.9%, to { width: calc(var(--n) * 1ch); }
}
@keyframes txt-multi-line-typewriter-caret {
0%, 100% { border-right-color: var(--c); }
}
@keyframes txt-multi-line-typewriter-blink {
0% { border-right-color: var(--c); }
50% { border-right-color: transparent; }
}
@media (prefers-reduced-motion: reduce) {
.txt-multi-line-typewriter, .txt-multi-line-typewriter *, .txt-multi-line-typewriter::before, .txt-multi-line-typewriter::after, .txt-multi-line-typewriter *::before, .txt-multi-line-typewriter *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--txt-color on the element or a parent.
Currently previewing #3b82f6.--txt-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 40px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.