Typewriter Effect

Avatar of Geoff Graham
Geoff Graham on (Updated on )
.typewriter h1 {
  overflow: hidden; /* Ensures the content is not revealed until the animation */
  border-right: .15em solid orange; /* The typwriter cursor */
  white-space: nowrap; /* Keeps the content on a single line */
  margin: 0 auto; /* Gives that scrolling effect as the typing happens */
  letter-spacing: .15em; /* Adjust as needed */
  animation: 
    typing 3.5s steps(40, end),
    blink-caret .75s step-end infinite;
}

/* The typing effect */
@keyframes typing {
  from { width: 0 }
  to { width: 100% }
}

/* The typewriter cursor effect */
@keyframes blink-caret {
  from, to { border-color: transparent }
  50% { border-color: orange; }
}

Notes:

  • Demo relies on flexbox, so that could affect the layout in testing
  • Assumes the use of Autoprefixer
  • The width of the text container will be determined by the length of the text being used
  • Adding more steps to the typing animation will increase the smoothness of the typing
  • Adjust the letter-spacing based on the font-family and font-size being used

More!

Some use JavaScript, which may sometimes be preferable (literally adding a character at a time feels more like a real typewriter) and sometimes not be (potential accessibility concerns).

Typed.css

Came across this SCSS @mixin from Brandon McConnell with powerful options that can identify multiple strings, speed, caret configurations, pauses, and all kinds of other things.