Text Scramble Effect: Build a Decode Headline in JS and CSS (Accessible)
The text scramble effect is a headline that starts as random characters and settles, letter by letter, into the real words. You've seen it on hacker-themed landing pages, on developer tools and on anything that wants to feel like a terminal. Done well it's a short, memorable entrance for a hero headline. Done badly it shifts the layout, makes a screen reader read gibberish, and keeps moving for people who asked it not to.
This post explains how the effect works, then gives you a starter in plain JavaScript and CSS, with the accessibility and layout details handled. After that: running it once when it scrolls into view, a short React version, and notes for Webflow, Framer and plain HTML.
The code below is a hand-written starter, written for this post. It is not code from any product, and it's meant to be edited.
How the effect works
There are only three ideas:
- Keep the real text. You know the final string, for example "Decode every headline".
- Show random glyphs in place of letters that haven't resolved yet. On each tick, every unresolved position gets a new random character from a pool.
- Resolve left to right. A counter moves along the string. Positions before it show the real letter, positions after it keep scrambling. When the counter passes the end, you stop.
Spaces usually stay as spaces, so word shapes are visible from the start. That's most of it. Everything else is making it behave: not shifting the page, not confusing assistive tech, and respecting motion settings.
The rules to get right first
- Real text stays real. Search engines and screen readers should get the actual headline, not whatever frame the animation was on. Put the real text in the accessible tree once and hide the animated copy with
aria-hidden="true". - Reserve the width. Random characters have different widths in a proportional font, so a line of scrambled text keeps changing length. Give each letter a fixed-width box, or use a monospace font.
- Show the end state to people who want less motion. With
prefers-reduced-motion: reduce, skip the animation and show the final text. - Run once. A headline that scrambles every time it scrolls past gets tiring. Play it once, the first time it's visible.
- Make it work without JavaScript. The real text should be in the HTML, so the page is complete before any script runs.
The markup
The headline holds the real text. The script adds a second, hidden-from-assistive-tech layer for the animation.
<h1 class="scramble" data-scramble>Decode every headline</h1>
After the script runs, the heading looks like this:
<h1 class="scramble" data-scramble>
<span class="sr-only">Decode every headline</span>
<span class="scramble__glyphs" aria-hidden="true">
<span class="scramble__ch">D</span><span class="scramble__ch">e</span>...
</span>
</h1>
A screen reader reads the sr-only copy once and skips the glyphs. Copy-paste of the hidden text still works, and the heading keeps its meaning as a heading.
The CSS
/* Hand-written starter: text scramble */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.scramble {
font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
}
.scramble__ch {
display: inline-block;
width: 1ch; /* every letter gets the same box */
text-align: center;
}
/* Word spaces stay as wide as a character */
.scramble__ch--space {
width: 1ch;
}
/* Unresolved letters are dimmer, so the settling reads clearly */
.scramble__ch--noise {
opacity: 0.55;
}
Because every letter box is 1ch wide in a monospace font, the heading takes the same width at every frame, whether it's showing "Q#7x" or "Decode". If your design needs a proportional font, you can measure each letter's final width first and set it on its span, but monospace is the simplest way to get zero shift.
Also reserve the height. If the heading wraps onto two lines at your breakpoint, it should wrap the same way from the first frame. Setting the real text first, then replacing letters in place, does that.
The JavaScript
// Hand-written starter: text scramble
const POOL = "!<>-_\\/[]{}=+*^?#0123456789ABCDEFabcdef";
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)");
function setup(el) {
const text = el.textContent;
// Real text for assistive tech and search; animated copy hidden from both.
const real = document.createElement("span");
real.className = "sr-only";
real.textContent = text;
const glyphs = document.createElement("span");
glyphs.className = "scramble__glyphs";
glyphs.setAttribute("aria-hidden", "true");
const cells = [...text].map((ch) => {
const cell = document.createElement("span");
cell.className = "scramble__ch" + (ch === " " ? " scramble__ch--space" : "");
cell.textContent = ch;
glyphs.appendChild(cell);
return cell;
});
el.textContent = "";
el.append(real, glyphs);
return { text, cells };
}
function play({ text, cells }, { duration = 1200, fps = 30 } = {}) {
const chars = [...text];
const start = performance.now();
let last = 0;
function frame(now) {
const t = Math.min(1, (now - start) / duration);
const resolved = Math.floor(t * chars.length);
if (now - last >= 1000 / fps || t === 1) {
last = now;
cells.forEach((cell, i) => {
if (chars[i] === " " || i < resolved) {
cell.textContent = chars[i];
cell.classList.remove("scramble__ch--noise");
} else {
cell.textContent = POOL[Math.floor(Math.random() * POOL.length)];
cell.classList.add("scramble__ch--noise");
}
});
}
if (t < 1) requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
}
Notes on this:
[...text]splits by code point rather than UTF-16 unit, so most emoji don't get cut in half. Complex scripts and combined emoji may still split badly, so for those, leave the effect off.- The pool is a plain string you can change. Digits and symbols look "technical". Letters only look more like a cipher.
- The effect is throttled to about 30 updates a second. Swapping a character every 60th of a second is too fast to read and mostly adds work.
- Only text content and an opacity class change. Nothing alters the box size, so there's no layout shift.
Run once, when it scrolls into view
An IntersectionObserver starts the effect the first time the headline is visible, then stops watching it. It also handles reduced motion by leaving the real text alone.
document.querySelectorAll("[data-scramble]").forEach((el) => {
if (reduce.matches) return; // final text stays as written
const state = setup(el);
const io = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
play(state);
io.disconnect(); // once only
}
},
{ threshold: 0.4 }
);
io.observe(el);
});
Two details here. First, when reduced motion is on, the function returns before touching the heading, so it stays as plain real text with no hidden copy and no animation. Second, before the headline is visible, it shows the real letters (the cells start with the real characters). If you'd rather it started scrambled, call a single render with noise in setup, but the real-letters-first version is safer if the script is slow to load.
If a visitor changes their motion setting while the page is open, this starter doesn't react. For a hero headline that's usually fine.
React version
In React, render the real text once as screen-reader text, render the animated letters as hidden, and drive the letters from state.
import { useEffect, useRef, useState } from "react"
const POOL = "!<>-_\\/[]{}=+*^?#0123456789ABCDEFabcdef"
export function Scramble({ text, duration = 1200 }: { text: string; duration?: number }) {
const ref = useRef<HTMLHeadingElement>(null)
const chars = [...text]
const [shown, setShown] = useState(chars)
useEffect(() => {
const el = ref.current
if (!el) return
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
let raf = 0
const io = new IntersectionObserver(
([entry]) => {
if (!entry.isIntersecting) return
io.disconnect()
const start = performance.now()
const tick = (now: number) => {
const t = Math.min(1, (now - start) / duration)
const resolved = Math.floor(t * chars.length)
setShown(
chars.map((c, i) =>
c === " " || i < resolved ? c : POOL[Math.floor(Math.random() * POOL.length)]
)
)
if (t < 1) raf = requestAnimationFrame(tick)
}
raf = requestAnimationFrame(tick)
},
{ threshold: 0.4 }
)
io.observe(el)
return () => {
io.disconnect()
cancelAnimationFrame(raf)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [text, duration])
return (
<h1 ref={ref} className="scramble">
<span className="sr-only">{text}</span>
<span className="scramble__glyphs" aria-hidden="true">
{shown.map((c, i) => (
<span key={i} className="scramble__ch">
{c}
</span>
))}
</span>
</h1>
)
}
The initial state is the real text, so the server-rendered HTML and the first client render match and there's nothing to mismatch on hydration. The effect only runs in the browser. The same CSS as above applies. This version updates state on every frame, which is fine for a single headline but not for dozens at once.
Platform notes
- Plain HTML. Put the CSS in your stylesheet and the script before
</body>. Mark any heading withdata-scramble. The real text is in the HTML, so the page works without the script. - Webflow. Add the CSS and the script in an Embed element or in Site settings → Custom code → Footer code. Give your heading the
scrambleclass and thedata-scramblecustom attribute in the Designer's settings panel. Custom code runs on the published site, not in the Designer preview. - Framer. Create a code component and paste the React version, with the CSS in the component or under Site Settings → Custom Code. Expose the text as a property so editors can change it without opening code. Check it in Preview.
Choosing a font
Monospace is the usual choice, and it suits the look. If your brand font is proportional, an easy compromise is to use it for everything on the page and switch only the scrambled headline to a monospace one. Check that your fallback fonts are monospace too, so the width doesn't change if the first choice doesn't load.
When you don't want to maintain it
The starter above is enough for one headline. If you want it across a site, with other effects too, and without maintaining your own accessibility and layout handling, Kinetic Type is the drop-in option. Its Decrypt preset does the effect described here: "Random characters decode into your words." There's also a Matrix Rain preset, where "green glyph trails rain into each letter".
From its product page:
- Real text. Screen readers read it once, in one piece, and headings and links keep their meaning. It's selectable, searchable and in the HTML before any script runs.
- No layout shift. When a scramble changes a letter's shape, its box keeps its width.
- Reduce Motion shows the final text, still.
- Seven triggers, including in view, scroll-scrubbed, hover, click and loop, plus code control.
- Works as a script tag, a React component and a Framer code component, with the same settings in each.
It's one of 34 presets, so if a decode headline is only the first effect you try, the rest are there. Kinetic Type is $12 one-time under a single-project license, meaning one website or project per license. The Kinetic Type page has a live demo of every preset.
For the wider picture on animating type without breaking it, see kinetic typography that stays real text. Other effects for the same kinds of pages are on the effects page.
Pricing
Kinetic Type is $12 one-time for one website or project. It's also part of the All-Access Pass, which bundles the Parallel Studio web components.
Whichever route you take, test three things before you ship: the heading with Reduce Motion on, the page once with a screen reader, and the layout while the scramble runs, to make sure nothing moves.