splitText()
Creates a text splitter that wraps text content in animatable elements.Syntax
Parameters
CSS selector, DOM element(s), or NodeList of elements to split
Configuration options for text splitting.
Enable line splitting. Pass
true or an object with template options.Template options:class: CSS class name for line elementswrap: Wrapper style (true,'clip', or CSS overflow value)clone: Clone effect direction ('left','right','top','bottom')
Enable word splitting. Pass
true or an object with template options.Enable character splitting. Pass
true or an object with template options.Include spaces as separate animatable elements.
Add visually-hidden accessible text for screen readers.
Enable visual debugging with colored outlines.
Returns
A TextSplitter instance with the following properties:
lines: Array of line elementswords: Array of word elementschars: Array of character elementssplit(clearCache): Re-split the textrefresh(): Refresh the split (clears cache)revert(): Restore original HTMLaddEffect(fn): Add animation effects
Examples
Basic Character Split
Word Animation
Line-by-Line Reveal
Custom Character Classes
Split with Spaces
Clone Effect
Responsive Splitting
Revert Split
TextSplitter Class
TheTextSplitter class provides advanced control over text splitting.
Properties
Array of split line elements (when
lines: true)Array of split word elements (when
words: true)Array of split character elements (when
chars: true)The original target element
Original HTML content before splitting
Whether the splitter is ready (fonts loaded, splitting complete)
Methods
split(clearCache)
Re-splits the text content.Whether to clear the cached split and re-split from original HTML
refresh()
Refreshes the split by clearing cache (alias forsplit(true)).
revert()
Reverts to the original HTML and cleans up.addEffect(effect)
Adds an animation effect that runs on split/refresh.Function that receives the TextSplitter instance and returns an animation or cleanup function
Constructor
Example: Class Usage
Template Customization
Customize the HTML structure of split elements:Custom Template Function
{value}: The text content (character, word, or line){i}: The index of the element
Predefined Templates
Notes
- Automatically handles font loading using the Font Loading API
- Preserves nested HTML structure (bold, italic, links, etc.)
- Automatically re-splits on window resize for responsive layouts
- Uses
ResizeObserverfor efficient resize detection - Supports international text with
Intl.Segmenterfor proper word/grapheme boundaries - Accessible by default (adds visually-hidden text for screen readers)
- Compatible with all modern browsers
Performance Tips
- Use
lines: trueonly when needed (more computationally expensive) - For simple animations, split only
charsorwords, not both - Call
revert()when done to free up memory - Use
includeSpaces: falseunless you need to animate spaces
Source
Defined in:/home/daytona/workspace/source/src/text/split.js:212-500