# Simpler CSS typing animation, with the ch unit

DevFeed: [Simpler CSS typing animation, with the ch unit](<https://devfeed.tech/articles/simpler-css-typing-animation-with-the-ch-unit-52042.md>)

Original publisher: [Read original article](<https://lea.verou.me/2012/02/simpler-css-typing-animation-with-the-ch-unit/>)

Author: Lea Verou

Published: 2012-02-05T00:00:00Z

Content type: article

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [CSS](<https://devfeed.tech/topics/css.md>), [web animation](<https://devfeed.tech/topics/web-animation.md>), [Web Development](<https://devfeed.tech/topics/web-development.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [animation](<https://devfeed.tech/tags/animation.md>), [blog](<https://devfeed.tech/tags/blog.md>), [browser-support](<https://devfeed.tech/tags/browser-support.md>), [coding](<https://devfeed.tech/tags/coding.md>), [css](<https://devfeed.tech/tags/css.md>), [css-animations](<https://devfeed.tech/tags/css-animations.md>), [css-values](<https://devfeed.tech/tags/css-values.md>), [easing](<https://devfeed.tech/tags/easing.md>), [fallback](<https://devfeed.tech/tags/fallback.md>), [firefox](<https://devfeed.tech/tags/firefox.md>), [script](<https://devfeed.tech/tags/script.md>), [tips](<https://devfeed.tech/tags/tips.md>)

## AI overview

The revised typing animation uses the `ch` unit to size steps by character width, removing the need for an obscuring span and allowing non-solid backgrounds. The character count remains hard-coded, but can be set by script without changing the animation stylesheets. Each animation uses one keyframe and relies on the browser to derive missing start and end keyframes from fallback styles. The excerpt reports full support in Firefox 1+ and IE10, while WebKit-based browsers animate with an imperfect `em` fallback; browsers without CSS animations, such as Opera, show a solid, non-blinking caret. Firefox's `ch` support is noted as buggy with some fonts, including Monaco.

## Source excerpt

A while ago, I posted about how to use steps() as an easing function to create a typing animation that degrades gracefully. Today I decided to simplify it a bit and make it more flexible, at the cost of browser support. The new version fully works in Firefox 1+ and IE10, since Opera and WebKit don't support the ch unit and even though IE9 supports it, it doesn't support CSS animations. To put it simply, one ch unit is equivalent to the width of the zero (0) character of the font. So, in monospace fonts, it's equivalent to the width of every character, since every character has the same width. In the new version, we don't need an obscuring span, so no extra HTML and it will work with non-solid backgrounds too. Also, even though the number of characters still needs to be hard-coded, it doesn't need to be hardcoded in the animation any more, so it could be easily done through script without messing with creating/modifying stylesheets. Note how each animation only has one keyframe, and takes advantage of the fact that when the from (0%) and to (100%) keyframes are missing, the browser generates them from the fallback styles. I use this a lot when coding animations, as I hate duplication. In browsers that support CSS animations, but not the ch unit (such as WebKit based browsers), the animation will still occur, since we included a fallback in ems, but it won't be 100% perfect. I think that's a pretty good fallback, but if it bothers you, just declare a fallback of auto (or don't declare one at all, and it will naturally fall back to auto). In browsers that don't support CSS animations at all (such as Opera), the caret will be a solid black line that doesn't blink. I thought that's better than not showing it at all, but if you disagree, it's very easy to hide it in those browsers completely: Just swap the border-color between the keyframe and the h1 rule (hint: when a border-color is not declared, it's currentColor). Edit: It appears that Firefox's support for the ch uni