SplitText
Quick Start
CDN Link
gsap.registerPlugin(SplitText)
Minimal usage
// split elements with the class "split" into words and characters
let split = SplitText.create(".split", { type: "words, chars" });
// now animate the characters in a staggered fashion
gsap.from(split.chars, {
duration: 1,
y: 100, // animate from 100px below
autoAlpha: 0, // fade in from opacity: 0 and visibility: hidden
stagger: 0.05 // 0.05 seconds between each
});
Or use the new onSplit() syntax available in v3.13.0+:
SplitText.create(".split", {
type: "lines, words",
mask: "lines",
autoSplit: true,
onSplit(self) {
return gsap.from(self.words, {
duration: 1,
y: 100,
autoAlpha: 0,
stagger: 0.05
});
}
});
SplitText is a small JavaScript library that splits an HTML element's text into individual characters, words, and/or lines (each in its own, newly-created element), allowing you to create gorgeous staggered animations. It's highly configurable and smarter than other text splitting tools thanks to features like automatic screen reader accessibility, masking for reveal effects, responsive re-splitting, and much more.
Detailed Walkthrough - Major rewrite in
v3.13.0 - half the size, 14 new features!Features
Feature Highlights
The new v3.13.0+ features are marked below with "*"
- Screen reader Accessibility* - Adds
aria-labelto the split element(s) andaria-hiddento the freshly-created line/word/character elements. - Responsive re-splitting* - Avoid funky line breaks when resizing or when fonts load with
autoSplitandonSplit(). Offers automatic cleanup and resuming of animations too! - Slice right through nested elements* - Elements like
<span>,<strong>, and<a>that span multiple lines are handled effortlessly withdeepSliceso they don't stretch lines vertically. - Masking* - Wrap characters, words or lines with an extra clipping element for easy mask/reveal effects.
- Integrates seamlessly with GSAP's
context(),matchMedia()anduseGSAP() - Flexible targeting - Apply your own class names to characters, words, or lines. Append
"++"to auto-increment them (e.g.word1,word2, etc.). EnablepropIndex* to apply CSS variables like--word: 3. - Ignore certain elements* - Perhaps you'd like to leave
<sup>elements unsplit, for example. - Supports emojis & more - SplitText does an excellent job with foreign characters too.
- Revert anytime - Restore the element's original
innerHTMLanytime withrevert() - Handle complex edge cases with custom
RegExp* orprepareText()*
Splitting
Basic Usage
Feed SplitText.create() the element(s) you'd like to split and it'll return a SplitText instance with chars, words, and lines properties where you can access the resulting elements.
// the target can be selector text, an element, or an Array of elements
let split = SplitText.create(".headline");
// Array of characters
split.chars
// Array of words
split.words
// Array of lines
split.lines
