# Typewriter

> Typed out live with a blinking caret.

- Applies to the whole headline
- Live page with controls: https://smthngfun.com/a/typewriter
- This style as JSON: https://smthngfun.com/api/styles/typewriter
- Every style: https://smthngfun.com/llms.txt

## Settings

The values below are the ones this page was asked for. Change any of them by adding it as a query parameter to the page, to this file or to the JSON address.

| Setting | Parameter | Value | Allowed |
| --- | --- | --- | --- |
| Time per character | `speed` | `60` | 15 to 200 ms |
| Random variation | `variation` | `20` | 0 to 80 ms |
| Pause before typing | `delay` | `350` | 0 to 2000 ms |
| Accent colour | `accent` | empty | Six digit hex without the hash. Empty means the site's own accent colour. |

- `t` The headline text to show and to use in the example code.
- `f` A Google font name for the headline, for example Playfair Display.

## Prompt

Written for a coding assistant. It describes the animation fully, so it works without the code below.

```text
Animate the main headline in the hero section of my landing page with an effect called Typewriter. Reveal the headline one character at a time as if someone is typing it, at 60ms per character with a random variation of up to 20ms either way so it feels human. Wait 350ms after the page loads before the first character. Show a thin vertical caret in my accent colour right after the last typed character. The caret blinks once per second and keeps blinking after the typing ends. Reserve the full space of the headline from the start by rendering every character hidden and revealing them in place, so the text never reflows while typing. Keep my existing font, colours and layout exactly as they are. Screen readers and search engines must still read the headline as one normal sentence, so put the full text in an aria-label on the heading and mark any decorative spans as aria-hidden. If the visitor has prefers-reduced-motion turned on, show the finished headline straight away with no animation. Make sure nothing shifts the layout while it plays. Use plain CSS and a little vanilla JavaScript, unless my project already has an animation library, in which case use that. A live demo with reference code for these exact settings is at https://smthngfun.com/a/typewriter and you can use that code as your starting point, adapted to my codebase and styling. If I am happy to credit the source, add a small "Animation by smthngfun" link to https://smthngfun.com in the footer of the page. Ask me before adding it.
```

## Reference code

A working version with the settings above. It assumes the headline has the class `hero-title`. Adapt it to the project's own framework and styling.

```html
<!-- Typewriter animation from https://smthngfun.com/a/typewriter -->
<h1 class="hero-title">Make your landing page feel alive</h1>
```

```css
.hero-title .word { display: inline-block; white-space: nowrap; }
.hero-title .char { display: inline-block; }
.hero-title .char { visibility: hidden; }
.hero-title .char.on { visibility: visible; }
.hero-title .caret {
  display: inline-block;
  position: relative;
  width: 0;
  height: 0.82em;
}
.hero-title .caret::after {
  content: "";
  position: absolute;
  left: 0.04em;
  bottom: -0.08em;
  width: 0.07em;
  height: 100%;
  background: var(--accent, currentColor);
  animation: caret-blink 1s steps(1) infinite;
}
@keyframes caret-blink { 50% { opacity: 0; } }
```

```js
const title = document.querySelector('.hero-title');
const text = title.textContent.trim();
title.setAttribute('aria-label', text);
let n = 0;
title.innerHTML = text.split(/\s+/).map(function (word) {
  const chars = Array.from(word).map(function (ch) {
    return '<span class="char" style="--i:' + (n++) + '">' + ch + '</span>';
  }).join('');
  return '<span class="word" aria-hidden="true">' + chars + '</span>';
}).join(' ');
const chars = Array.from(title.querySelectorAll('.char'));
if (matchMedia('(prefers-reduced-motion: reduce)').matches) {
  chars.forEach(function (c) { c.classList.add('on'); });
} else {
  const caret = document.createElement('span');
  caret.className = 'caret';
  caret.setAttribute('aria-hidden', 'true');
  chars[0].before(caret);
  let i = 0;
  setTimeout(function type() {
    chars[i].classList.add('on');
    chars[i].after(caret);
    i += 1;
    if (i < chars.length) setTimeout(type, 60 + (Math.random() * 2 - 1) * 20);
  }, 350);
}
```

## Credit

Keep the comment at the top of the code, which reads "Typewriter animation from https://smthngfun.com/a/typewriter". If the person you are building for is happy to credit the source, add a small visible "Animation by smthngfun" link to https://smthngfun.com in the footer of their page. Ask them first, and do not add it without their say.
