Each letter strikes the page with a little thump and ink spread, and lands slightly off its baseline like an old machine with worn typebars. Uneven rhythm and inking come from nth-child.
Also known as: old typewriter effect css, typewriter strike animation, misaligned typewriter letters.
<div class="txt-vintage-typewriter-text" aria-label="Once upon a time"> <span class="txt-vintage-typewriter-text-t" aria-hidden="true"><span style="--i: 0">O</span><span style="--i: 1">n</span><span style="--i: 2">c</span><span style="--i: 3">e</span><span style="--i: 4"> </span><span style="--i: 5">u</span><span style="--i: 6">p</span><span style="--i: 7">o</span><span style="--i: 8">n</span><span style="--i: 9"> </span><span style="--i: 10">a</span><span style="--i: 11"> </span><span style="--i: 12">t</span><span style="--i: 13">i</span><span style="--i: 14">m</span><span style="--i: 15">e</span></span> </div>
.txt-vintage-typewriter-text {
--c: var(--txt-color, #3b82f6);
font-size: var(--txt-size, 32px);
font-family: system-ui, sans-serif;
font-weight: 700;
line-height: 1.2;
font-family: "Courier New", Courier, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
color: color-mix(in srgb, var(--c) 80%, currentColor);}
.txt-vintage-typewriter-text-t span {
display: inline-block;
animation: txt-vintage-typewriter-text-strike 0.24s cubic-bezier(0.2, 0.9, 0.3, 1) calc(0.3s + var(--i) * 105ms + var(--late, 0ms)) both;
}
/* worn typebars: a few letters sit high, low or tilted, and some ink lighter */
.txt-vintage-typewriter-text-t span:nth-child(3n) { --y: 0.035em; --r: -2deg; }
.txt-vintage-typewriter-text-t span:nth-child(4n + 1) { --y: -0.03em; --r: 1.5deg; }
.txt-vintage-typewriter-text-t span:nth-child(5n + 2) { --ink: 0.7; }
.txt-vintage-typewriter-text-t span:nth-child(7n) { --ink: 0.82; --r: 2.5deg; }
.txt-vintage-typewriter-text-t span:nth-child(n + 6) { --late: 70ms; }
.txt-vintage-typewriter-text-t span:nth-child(n + 11) { --late: 140ms; }
@keyframes txt-vintage-typewriter-text-strike {
0% {
opacity: 0;
transform: translateY(-0.25em) scale(1.35) rotate(var(--r, 0deg));
}
45% {
opacity: 1;
transform: translateY(0.04em) scale(0.95) rotate(var(--r, 0deg));
text-shadow: 0 0 0.08em currentColor;
}
100% {
opacity: var(--ink, 1);
transform: translateY(var(--y, 0)) rotate(var(--r, 0deg));
text-shadow: 0.015em 0 0.02em currentColor;
}
}
@media (prefers-reduced-motion: reduce) {
.txt-vintage-typewriter-text, .txt-vintage-typewriter-text *, .txt-vintage-typewriter-text::before, .txt-vintage-typewriter-text::after, .txt-vintage-typewriter-text *::before, .txt-vintage-typewriter-text *::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.