Examples

Eleven ways to use it

Same call every time, different options and CSS. Move the sliders: the code below each demo follows.

Hover me slowly

01 · Hover trail

Letters light up under the pointer and fade out behind it. The case it was born for.

Rainbow wave

02 · Wave

Splitting sets --char-index on each letter. Multiply it to delay a keyframe animation.

Five colors, in order

03 · Palette

colors sets --char-color on each letter, cycling through the list.

new ColorLetters({
  element: '.title',
  colors: ['#ff4d6d', '#ffd166', '#06d6a0', '#4cc9f0', '#9b5de5'],
});
.c--char { color: var(--char-color); }

Shuffle the deck

04 · Random palette

Add random: true and each letter picks a palette color at random. Create a new instance to shuffle again.

let cl = new ColorLetters({ element: '.title', colors, random: true });

button.addEventListener('click', () => {
  cl.destroy();
  cl = new ColorLetters({ element: '.title', colors, random: true });
});

Hue by index

05 · Gradient by index

No palette needed: each letter gets its own hue from its position, spread with --char-total.

.c--char {
  color: oklch(78% 0.17
    calc(var(--char-index) / var(--char-total) * 300));
}

One color per word, not per letter

06 · Words

by: 'words' puts the class and the palette on words. Hover them.

new ColorLetters({ element: '.title', by: 'words', colors });
.c--char { display: inline-block; transition: color .3s, translate .3s; }
.c--char:hover { color: var(--char-color); translate: 0 -0.1em; }

Colors in on scroll

07 · Reveal on scroll

Toggle one class with an IntersectionObserver. The letters fill in one after another.

const cl = new ColorLetters({ element: '.title', color: '#06d6a0' });
new IntersectionObserver(([e]) =>
  e.target.classList.toggle('is-in', e.isIntersecting)
).observe(cl.elements[0]);

Typed, letter by letter.

08 · Typewriter

Letters start hidden and appear on a delay from their index. Pure CSS once split.

.c--char { opacity: 0; animation: type 0s forwards; }
.is-playing .c--char { animation-delay: calc(var(--char-index) * 90ms); }
@keyframes type { to { opacity: 1; color: var(--color-letters); } }

Works with GSAP

09 · GSAP

cl.chars is ready right after new, so hand it to any animation library.

import gsap from 'gsap';

const cl = new ColorLetters({ element: '.title', colors });
gsap.from(cl.chars, {
  yPercent: 110, rotate: 12, opacity: 0,
  stagger: 0.04, ease: 'back.out(2)',
});

Pick a color

10 · setColor()

Change the variable at any time, for example from a project color or a color picker.

const cl = new ColorLetters({ element: '.title', color: '#4cc9f0' });
input.addEventListener('input', () => cl.setColor(input.value));

Split / unsplit

11 · destroy()

Puts the original HTML back and removes the variables. Create a new instance to split again.

cl.destroy();
cl = new ColorLetters({ element: '.title', color: '#9b5de5' });