API
Everything it does
One class: seven options, four properties, two methods.
Constructor
import ColorLetters from '@andresclua/color-letters';
const cl = new ColorLetters({
element: '[data-color-letters]',
color: '#ff4d6d',
charClass: 'c--char',
by: 'chars',
colors: ['#ff4d6d', '#ffd166', '#06d6a0'],
random: false,
ariaLabel: true,
});
Options
| Option | Type | Default | Description |
|---|---|---|---|
element | string · Element · NodeList · Element[] | '[data-color-letters]' | What to split. A selector matches every element on the page. |
color | string | none | Any CSS color. Set as --color-letters on each element. Left unset if omitted. |
charClass | string | 'c--char' | Class added to every unit (letter or word). |
by | 'chars' · 'words' | 'chars' | Which spans get charClass and the palette colors. |
colors | string[] | none | Palette. Each unit gets --char-color, cycling through the list. An empty array is ignored. |
random | boolean | false | With colors, each unit gets a random palette color instead. |
ariaLabel | boolean | true | Adds a visually hidden copy of the text and hides the split spans, so screen readers read words, not letters. Skipped when the element contains links, buttons or inputs. |
Properties and methods
| Member | Description |
|---|---|
elements | The split elements, as an array. |
items | The spans that got charClass: letters, or words with by: 'words'. |
chars | Every letter <span>, across all elements. |
words | Every word <span>, across all elements. |
setColor(color) | Sets --color-letters on every element. |
destroy() | Restores the original HTML and removes the classes and variables it added. A new instance on the same element splits again. |
What lands in the DOM
Type below to see the markup a call produces.
CSS variables
| Variable | Set on | Set by |
|---|---|---|
--color-letters | element | ColorLetters (color, setColor()) |
--char-color | each unit | ColorLetters (colors) |
--char-index | each letter | Splitting |
--char-total · --word-total | element | Splitting |
--word-index | each word | Splitting |
Claude Code skill
Use it with Claude
The package ships a skill, color-letters: a guide that helps Claude use the library in your project. Ask for an effect ("a hover trail on the hero title", "letters that fall on page change"), an option, a Nuxt or React setup, or why the colors do not show. It checks the version you have installed and never invents options.
// what is inside
skill/SKILL.md // rules + quick API
skill/references/api.md // options, properties, methods, variables, markup
skill/references/recipes.md // the 11 examples + Pagefold page transitions
skill/references/frameworks.md // vanilla, UMD, Vue/Nuxt, React/Next, Astro, WordPress
skill/references/pitfalls.md // symptom → cause → fix
Notes
- Browser only. Splitting reads
documentwhen it is imported. In Nuxt, Astro or Next, import it on the client. - Screen readers. With
ariaLabel(on by default) they read the original text once. It is skipped when the element holds links, buttons or inputs, because hiding those is worse than reading letter by letter. - CDN / UMD. The UMD build expects
window.Splitting: load Splitting first, thencolor-letters.umd.cjs. - Nested markup such as
<b>or<a>is kept; only the text inside is split.