A CSS-only typewriter effect
Written by Stefan Judis
- Published at
- Updated at
- Reading time
- 1min
I just found Marco Denig's article "CSS tricks". There are some good tricks in it. The trick that stood out to me was the CSS-only typewriter effect.
It's a pretty sweet effect, and I want to document it for my future self. I mean, look at it. ๐ The typing below is done without JavaScript!
Marco uses a combination of white-space: nowrap
/ overflow: hidden
to hide overflowing characters, defines a monospace font to make the container's width predictable, and then animates the width of the surrounding container. The blinking cursor is realized by animating the right border.
Overall that's pretty smart!
/* adjusted CSS of the above custom element */
type-writer .text {
width: 33ch;
animation: typing 2s steps(22),
blink .5s step-end infinite alternate;
white-space: nowrap;
overflow: hidden;
border-right: 3px solid;
font-family: monospace;
font-size: 1.5em;
margin: 1em auto;
}
@keyframes typing {
from {
width: 0
}
}
@keyframes blink {
50% {
border-color: transparent
}
}
If you enjoyed this article...
Join 5.4k readers and learn something new every week with Web Weekly.