Modules
StringSplit
Rebuilds text into lines, words, or characters and exposes deterministic indices for animation.
StringSplit
StringSplit rebuilds text into line, word, and character wrapper spans, each with CSS variables you can use for animation. It preserves inline HTML inside the split, handles kerning, and can optionally scale text to fill the container width.
Registration
import StringTune, { StringSplit } from '@fiddle-digital/string-tune';
const stringTune = StringTune.getInstance();
stringTune.use(StringSplit);
stringTune.start(60);
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
string-split | string | "" | Declares which wrappers and alignment variables to build. Also accepts data-string-split. |
string-split-restore-after | number-like string | not set | Restores the original HTML after the given timeout in milliseconds. |
Grammar
The string-split attribute controls what gets generated. Full reference: Split Grammar.
Tokens are separated by |. Each token enables a wrapper level and accepts an optional alignment parameter in []:
string-split="char[start]"
string-split="word[start]|char-word[start]"
string-split="line[center]|char-line[center]"
string-split="line[start]|fit"
Available tokens:
| Token | Creates |
|---|---|
line | .-s-line wrappers |
word | .-s-word wrappers |
word-line | .-s-line + .-s-word |
char | .-s-char wrappers |
char-line | .-s-line + .-s-char |
char-word | .-s-word + .-s-char |
fit | Scales text to fill container width |
Available alignment params: start, center, end, random(min,max).
Quick Example
<h1
class="hero-title"
string="split"
string-split="line[center]|char-line[center]"
>
Crafting<br />Digital<br />Experiences
</h1>
.hero-title .-s-line {
overflow: hidden;
padding-bottom: 0.1em;
}
.hero-title .-s-char {
display: inline-block;
transform: translateY(100%);
animation: charReveal 0.8s cubic-bezier(0.2, 0, 0, 1) forwards;
animation-delay: calc(
var(--line-index, 0) * 0.15s + var(--char-index, 0) * 0.03s
);
}
@keyframes charReveal {
to { transform: translateY(0); }
}
CSS Variables
Full reference: DOM Output.
On .-s-line
| Variable | Description |
|---|---|
--line-index | 0-based line index |
--word-total | Number of words in this line |
--fit-font-size | Computed fit font size (when line-level fit is active) |
Plus alignment variables when requested: --line-start, --line-center, --line-end, --line-random.
On .-s-word
| Variable | Description |
|---|---|
--word-index | Global 0-based word index |
--char-total | Number of chars in this word |
Plus alignment variables: --word-start, --wordLine-start, etc.
On .-s-char
| Variable | Description |
|---|---|
--char-index | Global 0-based char index |
--kerning | Kerning adjustment between this char and the next (when needed) |
Plus alignment variables: --char-start, --charWord-start, --charLine-center, etc.
On the source element
| Variable | When |
|---|---|
--char-global-total | Char wrappers are active |
--word-global-total | Word wrappers are active |
--line-global-total | Line wrappers are active |
--fit-font-size | Global fit without line-level split |
--fit-scale-y | Global fit without line-level split |
--fit-aspect-ratio | Global fit without line-level split |
Inline HTML
You can use standard inline elements inside split text:
<h1 string="split" string-split="word[start]|char-word[start]">
Crafted for <em>motion</em> systems
</h1>
The module preserves your inline markup in the rebuilt DOM. <em>, <strong>, <a>, <span>, and other inline elements keep their tag and attributes so your CSS continues to work.
Elements like <img>, <svg>, and <table> are kept as-is without splitting.
<br> forces a line break.
Full details: Inline HTML.
split-class
<split-class> is a helper element for applying CSS classes to generated wrappers:
<h2 string="split" string-split="word[start]|char-word[start]">
<split-class class="is-accent">Signal</split-class> Layer
</h2>
The <split-class> element itself does not appear in the output. Its classes are applied to the generated word or char wrappers that contain its children.
If every char in a word shares the same split-class, the classes are hoisted to the word wrapper.
Fit
Adding fit to string-split scales text to fill the container width:
<h1 string="split" string-split="line[start]|fit">
Big Headline
</h1>
.headline .-s-line {
font-size: calc(var(--fit-font-size) * 1px);
}
With line-level split, each line gets its own --fit-font-size. Without line-level split, the source element gets --fit-font-size, --fit-scale-y, and --fit-aspect-ratio.
Full details: Fit And Rebuild.
Lifecycle
When the DOM is rebuilt
- On initial connection
- On desktop width resize
- On reconnect
The module caches the original HTML on first split. On each rebuild, it starts from that cached original to produce a clean result.
Mobile
Mobile rebuild is disabled by default. The split DOM stays as initially built when the viewport height or width changes on mobile.
Restore After
<h1
string="split"
string-split="char[start]"
string-split-restore-after="2000"
>
Intro Text
</h1>
After 2000ms, the module restores the original HTML and adds class -restored. Useful for one-shot intro effects.
Accessibility
- All generated wrappers get
aria-hidden="true" - The source element gets
aria-labelwith the original text - Screen readers read the original content, not individual wrapper spans
Events
StringSplit does not emit module-specific events. Consume its output through CSS selectors and variables.
Deep Dives
- Split Grammar — full attribute reference
- DOM Output — wrappers, classes, variables, and nesting
- Inline HTML — how markup survives the split
- Fit And Rebuild — fit mode and rebuild timing