Rotating Text Swap
A headline word that slides up and is replaced by the next, with a pause control.
AI prompt
Create the headline "Design with" followed by a rotating word slot cycling through "clarity", "motion", "feeling" and "intent", in 26px semibold. The slot is an inline-block with overflow hidden, a fixed width equal to the longest word and a height of one line. Every 2200ms the current word animates translateY(0) to translateY(-100%) with opacity to 0 while the next word animates from translateY(100%) to 0, over 420ms cubic-bezier(.3,.8,.2,1). Add a small Pause button that toggles to Play, and also pause while the pointer is over the headline or any element inside has focus. The visible animated words have aria-hidden and a visually hidden span reads "Design with clarity, motion, feeling and intent" once. Under prefers-reduced-motion start paused and swap words without movement when the user presses Play.About this pattern
Definition
One word in a sentence changes on a timer. The old word slides up and fades while the new one rises from below, keeping the sentence stable around it.
Purpose & UX principles
- Shows several benefits in one line without extra space.
- Automatically moving text must be pausable (WCAG 2.2.2): there is a pause button, and it also pauses while hovered or focused.
- The sentence is readable without the animation: the live region is off and a full list of the words is available to screen readers once.
Interaction states
- Playing, paused (button shows Play), hover-paused, focus-visible on the button.
Motion specification
- Each swap: old word translateY 0 to -100% and opacity to 0, new word from +100%; 420ms cubic-bezier(.3,.8,.2,1). A new word every 2200ms.
- Reduced motion: the rotation starts paused and swaps without movement when the button is pressed.
Responsive behaviour
- The word slot has a fixed width equal to the longest word so surrounding text does not jump.
- Font size scales with the container.
Code
<div class="hd"><h2 aria-label="Design with clarity, motion, feeling and intent"><span aria-hidden="true">Design with </span><span class="slot" id="slot" aria-hidden="true"><span class="w">clarity</span></span></h2>
<button id="pz" type="button">Pause</button></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.hd{display:grid;justify-items:center;gap:12px;text-align:center}
h2{font-size:26px;font-weight:600;letter-spacing:-.01em;white-space:nowrap}
.slot{position:relative;display:inline-block;vertical-align:bottom;overflow:hidden;height:1.25em;width:4.6em;text-align:left}
.w{position:absolute;left:0;top:0;line-height:1.25}
#pz{height:28px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12px;cursor:pointer}
#pz:hover{border-color:var(--accent)}
#pz:focus-visible{outline:2px solid var(--accent);outline-offset:2px}const words=['clarity','motion','feeling','intent'],slot=document.getElementById('slot'),pz=document.getElementById('pz'),hd=document.querySelector('.hd');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
let k=0,paused=reduce,hover=false,t=0;pz.textContent=paused?'Play':'Pause';
function swap(){k=(k+1)%words.length;const old=slot.firstElementChild,nw=document.createElement('span');nw.className='w';nw.textContent=words[k];slot.append(nw);
if(reduce){old.remove();return;}
const o={duration:420,easing:'cubic-bezier(.3,.8,.2,1)'};
old.animate([{transform:'none',opacity:1},{transform:'translateY(-100%)',opacity:0}],{...o,fill:'forwards'}).finished.then(()=>old.remove());
nw.animate([{transform:'translateY(100%)',opacity:0},{transform:'none',opacity:1}],o);}
function tick(){clearInterval(t);t=setInterval(()=>{if(!paused&&!hover)swap();},2200);}tick();
pz.addEventListener('click',()=>{paused=!paused;pz.textContent=paused?'Play':'Pause';if(reduce&&!paused)swap();});
hd.addEventListener('pointerenter',()=>hover=true);hd.addEventListener('pointerleave',()=>hover=false);
hd.addEventListener('focusin',()=>hover=true);hd.addEventListener('focusout',()=>hover=false);One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Rotating Text Swap — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.hd{display:grid;justify-items:center;gap:12px;text-align:center}
h2{font-size:26px;font-weight:600;letter-spacing:-.01em;white-space:nowrap}
.slot{position:relative;display:inline-block;vertical-align:bottom;overflow:hidden;height:1.25em;width:4.6em;text-align:left}
.w{position:absolute;left:0;top:0;line-height:1.25}
#pz{height:28px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:12px;cursor:pointer}
#pz:hover{border-color:var(--accent)}
#pz:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
</style></head>
<body><div class="stage">
<div class="hd"><h2 aria-label="Design with clarity, motion, feeling and intent"><span aria-hidden="true">Design with </span><span class="slot" id="slot" aria-hidden="true"><span class="w">clarity</span></span></h2>
<button id="pz" type="button">Pause</button></div>
</div>
<script>
const words=['clarity','motion','feeling','intent'],slot=document.getElementById('slot'),pz=document.getElementById('pz'),hd=document.querySelector('.hd');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
let k=0,paused=reduce,hover=false,t=0;pz.textContent=paused?'Play':'Pause';
function swap(){k=(k+1)%words.length;const old=slot.firstElementChild,nw=document.createElement('span');nw.className='w';nw.textContent=words[k];slot.append(nw);
if(reduce){old.remove();return;}
const o={duration:420,easing:'cubic-bezier(.3,.8,.2,1)'};
old.animate([{transform:'none',opacity:1},{transform:'translateY(-100%)',opacity:0}],{...o,fill:'forwards'}).finished.then(()=>old.remove());
nw.animate([{transform:'translateY(100%)',opacity:0},{transform:'none',opacity:1}],o);}
function tick(){clearInterval(t);t=setInterval(()=>{if(!paused&&!hover)swap();},2200);}tick();
pz.addEventListener('click',()=>{paused=!paused;pz.textContent=paused?'Play':'Pause';if(reduce&&!paused)swap();});
hd.addEventListener('pointerenter',()=>hover=true);hd.addEventListener('pointerleave',()=>hover=false);
hd.addEventListener('focusin',()=>hover=true);hd.addEventListener('focusout',()=>hover=false);
</script>
</body></html>