GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • DocsDocs
  • ExamplesExamples
  • AI KitAIAI KitAI
  • React
  • JS
  • Vue
  • AI
  • animate
  • Timeline sequences
  • animateLayout
  • animateView
  • scroll
  • Easing
  • drag

This view

  • Original page
  • New address

GeezerVison is showing motion.dev — splitText | Split and staggered text animations | Motion

MOTION+

This feature is available with Motion+.

splitText is a small (+2.5kb) utility that makes it simple to create complex, beautiful split text animations with Motion.

Break apart text into individual characters, words, and lines, to create staggered enter or scroll-driven text effects. All with just a few lines of code. For the full list of techniques, from typewriters to tickers, see the text animation guide.

splitText is exclusive to Motion+ members. Motion+ is a one-time payment, lifetime membership that unlocks Motion UI, premium examples and access to a private Discord community.

Split TextOpen example

Features

  • Animate anything: Split text into characters, words and lines to animate each independently.

  • Preserves markup: Links, <br> and other inline elements stay where they are.

  • Respects kerning: Each character stays exactly where the browser rendered it.

  • Every language: Emoji, Arabic, Indic, Chinese, Japanese and Thai text split correctly.

  • Accessible: Screen readers read whole words, and copied text stays the same.

  • Lightweight: Only adds 2.5kb to your project bundle.

  • Flexible: Animate with Motion, CSS, or a library of your choice.

Install

First, install Motion UI in your project. You need to be a Motion+ member to generate an access token.

Once installed, splitText can be imported via motion-plus:

import { splitText } from "motion-plus"
TIP

Using React? See React split text for a hook that handles fonts, resizing and cleanup for you.

Black Friday

Motion+ is 20% off until 30 Nov.

One payment, lifetime updates. Get every premium example, Motion UI and AI Kit for your coding agent.

Get 20% offGet 20% off

Usage

splitText accepts a CSS selector or an HTML Element. It replaces the element's text content with <span> tags that wrap individual characters, words, and (optionally) lines.

It returns an object containing chars, words, and lines as arrays of elements, so they can be animated right away with Motion's animate and stagger functions.

const { chars } = splitText("h1")

animate(
  chars,
  { opacity: [0, 1], y: [10, 0] },
  { duration: 1, delay: stagger(0.05) }
)

The returned arrays are regular elements so you're not limited to animating them with Motion. You could animate them with CSS, or another animation library of your choice. Or, you could attach gesture recognisers to each element individually, for instance with Motion's hover function.

Split Text: ScatterOpen example
const { words } = splitText("h1")

hover(words, (wordElement) => {
  // Hover logic
})

Enter animations

To perform enter animations, it might be necessary to set the container to visibility: hidden via CSS until we're ready to animate.

.container {
  visibility: hidden;
}
document.querySelector(".container").style.visibility = "visible"

const { words } = splitText(".container")
animate(words, { opacity: [0, 1] })

Otherwise we might see a flash of visible text until our JS has loaded.

Working with custom fonts

If a custom font downloads after splitText runs, the text can split incorrectly because its size has changed.

First wait for the browser's document.fonts.ready promise:

Split Text: WavyOpen example
document.fonts.ready.then(() => {
  const { words } = splitText(element)

  animate(
    words,
    { y: [-10, 10] },
    { delay: stagger(0.04) }
  )
})

To split again whenever a font changes, pass a callback instead. See Handle resize.

Handle resize

To split text, we first have to measure the current layout. Therefore, if the container changes width, or a font changes, the lines will be wrong.

By passing a callback as the second argument, splitText handles this for you. It calls the callback with the new chars, words and lines every time it splits.

splitText("h1", ({ words }) => {
  return animate(
    words,
    { opacity: [0, 1], y: [10, 0] },
    { delay: stagger(0.05) }
  )
})

The callback runs in a scopeMotion scope rooted at the element. Before each new split, everything the callback started is stopped: animations, gestures like hover and press, and inView or scroll listeners. Selectors inside the callback find elements within the split element.

Return the animation and the new animation also continues from the same elapsed time, so a resize mid-animation doesn't restart it.

If splitText itself runs inside a scopeMotion scope, stopping that scope also reverts the split.

Gradient text

Text with background-clip: text usually disappears once its characters are transformed. splitText gives each character its own slice of the background, so the gradient stays continuous while the characters move, and when the text splits again on resize.

Split text resizeOpen example

Masked reveals

To slide words or lines in, within a clipped box, clip each line with CSS. No extra wrapper is needed. Because each line is clipped on its own, the reveal still works after the text splits again on resize:

.split-line {
  clip-path: inset(0 -0.1em -0.15em);
}
splitText("h1", ({ words }) =>
  animate(words, { y: ["110%", "0%"] }, { delay: stagger(0.04) })
)

The small negative insets leave room for italic overhangs and descenders.

Split text mask revealOpen example

Languages & emoji

Chinese, Japanese, Thai, Lao, Khmer and Myanmar text has no spaces between words. splitText finds the words with the browser's Intl.Segmenter. Emoji made from several parts, such as skin tones, flags and families, stay whole.

Arabic, Indic and other scripts where letters join are split into words but not characters, so their letters keep their shape.

Split text languages and emojiOpen example

Lines are read from the browser's layout, so vertical writing modes work too. Here, Japanese text set with writing-mode: vertical-rl is revealed word by word:

Split text JapaneseOpen example

Targeting with CSS

Each split component will receive split-char, split-word and split-line classes so you can select them with CSS. Each component additionally receives a data-index of its position within the overall character, word or line lists respectively.

.split-char[data-index=3] {
  color: red;
}

The provided classes are configurable with the charClass, wordClass and lineClass options.

splitText(element, { charClass: "my-char-class" })

Reverting

splitText returns a revert function that puts the original nodes back.

const { chars, revert } = splitText("h1")

revert()

If you passed a callback, revert also stops splitting on resize, and stops the animation the callback returned.

Cleaning up between splits

Motion gestures and animations started in the callback are cleaned up automatically, so you can attach them to the new elements without returning anything:

splitText("h1", ({ chars }) => {
  hover(chars, (char) => {
    animate(char, { scale: 1.2 })
    return () => animate(char, { scale: 1 })
  })
})

For other work, like your own event listeners, return a function. It runs before the next split and on revert.

splitText("h1", ({ chars }) => {
  const onClick = () => {}
  chars.forEach((char) => char.addEventListener("click", onClick))

  return () => {
    chars.forEach((char) => char.removeEventListener("click", onClick))
  }
})

Accessibility

Some split text implementations break screen readers, which end up reading characters, words or lines separately. splitText ensures that screen readers read copy as it's originally written.

Troubleshooting

Motion+ docsOne payment
Lifetime updates
Motion+ docs

Unlock splitText with Motion+

Motion+ is a lifetime purchase unlocking 550+ examples, Motion UI, AI Kit and more.

Examples
550+
Tutorials
110+
Updates
Lifetime
Get Motion+Get Motion+Already a member? Sign in
  1. 01animate

    animate() runs easing and spring animations, hardware-accelerated where supported.

  2. 02scrambleText

    scrambleText() animates text by scrambling characters, with stagger and timing controls.

  3. 03stagger

    stagger() distributes a delay across elements, with control over origin, grids and easing.

  • Split text animation exampleSplit text
  • Split Text animation exampleSplit Text
  • Split Text: Scatter animation exampleSplit Text: Scatter

Motion is supported by

Motion Cloud members who help fund Motion’s open-source development.

Become a supporter
  • nimit
  • electrinity
  • BBeto
  • DDamián Navarro
  • CCNTNAI
  • AAriel Rodribuez
  • AAbhishek
  • AAriel Conti
  • Sean Bugeja
  • Joshosullivam
  • Meepo
  • SStanislav Shilin
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.