Home / Learn / How do you rotate words inside a headline?

How do you rotate words inside a headline?

One headline with words that take turns in a fixed spot: a smart way to say several things in one line. The key is that we swap whole words, so Arabic letters never break apart.

Steps

  1. Wrap the changing word in an inline-block span inside the headline.
  2. Prepare the word list in JavaScript, each word as one complete string.
  3. Every 2.5 seconds: add a class that slides the word up and fades it out, swap the text, then remove the class so the new word slides in.
  4. Never split words into letters; splitting breaks Arabic letter joining.
  5. Under prefers-reduced-motion, keep the first word static and never start the timer.

The code

HTML

<h2 class="headline">نصنع محتوى <span class="rotator" id="rotator">يُبهر</span></h2>

<script>
const words = ["يُبهر", "يُقنع", "يبيع", "يُلهم"];
const rotator = document.getElementById("rotator");
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let index = 0;

if (!reduceMotion) {
  setInterval(() => {
    rotator.classList.add("word-out");
    setTimeout(() => {
      index = (index + 1) % words.length;
      rotator.textContent = words[index];
      rotator.classList.remove("word-out");
    }, 300);
  }, 2500);
}
</script>

<style>
.rotator {
  display: inline-block;
  color: #6d5ef0;
  transition: transform 0.3s ease, opacity 0.3s ease;
}
.rotator.word-out {
  transform: translateY(-0.6em);
  opacity: 0;
}
@media (prefers-reduced-motion: reduce) {
  .rotator { transition: none; }
}
</style>

RTL & Arabic notes: swapping whole words preserves letter joining, and inline-block keeps the word inside the Arabic sentence flow.

Didn't find the motion you need? Request it from the Vellox team.