DEV Community

Cover image for Real RGB Split in CSS with mix-blend-mode: screen
glitch.ezolab
glitch.ezolab

Posted on Originally published at glitch.ezolab.dev

Real RGB Split in CSS with mix-blend-mode: screen

Many CSS "RGB split" effects put red and cyan text-shadow copies behind white text. They look like colored outlines, because nothing is actually being split.

Real chromatic aberration separates light. Red, green and blue add up to white where they line up, and color appears only where they don't. One CSS property gives you that: mix-blend-mode: screen.

The short version

Make the text pure green, add a red and a blue copy, and blend both with screen:

<span class="rgb" data-text="SIGNAL">SIGNAL</span>
Enter fullscreen mode Exit fullscreen mode
.rgb {
  position: relative;
  display: inline-block;
  isolation: isolate;
  color: #0f0;               /* green channel */
}

.rgb::before,
.rgb::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  mix-blend-mode: screen;    /* R + G + B = white */
  pointer-events: none;
}

.rgb::before { color: #f00; transform: translateX(-3px); }
.rgb::after  { color: #00f; transform: translateX(3px); }
Enter fullscreen mode Exit fullscreen mode

Put it on a dark background. Where all three layers overlap, the text is white. Where two overlap, you get yellow, cyan or magenta without writing those colors anywhere.

Try the live interactive demo → (next to the text-shadow version)

Why screen works

For each color channel, from 0 to 1, screen computes:

1 - (1 - a) * (1 - b)
Enter fullscreen mode Exit fullscreen mode
  • Black is 0, so it changes nothing.
  • Pure red, green and blue each fill a different channel, so together they make white.
  • With the layers lined up, the word looks white. Move them apart and the colors appear.

That makes it easy to animate. Let the channels swing out, then settle:

.rgb::before,
.rgb::after {
  animation: split 3.2s ease-in-out infinite;
}
.rgb::before { --dir: -1; }
.rgb::after  { --dir: 1; }

@keyframes split {
  0%, 35%, 100% { transform: translateX(0); }
  50% { transform: translateX(calc(var(--dir) * 8px)); }
  62% { transform: translateX(calc(var(--dir) * -4px)); }
  74% { transform: translateX(calc(var(--dir) * 2px)); }
}
Enter fullscreen mode Exit fullscreen mode

Try the live interactive demo →

On a white page, use CMY and multiply

screen can't make anything darker, and white can't get any lighter. If the text blends with a white page, it disappears. If you isolate it, you still get bright text that is hard to read on white. On light pages, work with ink instead of light:

<span class="cmy" data-text="PRINT">PRINT</span>
Enter fullscreen mode Exit fullscreen mode
.cmy {
  position: relative;
  display: inline-block;
  isolation: isolate;
  color: #0ff;               /* cyan */
}

.cmy::before,
.cmy::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  mix-blend-mode: multiply;  /* C * M * Y = black */
  pointer-events: none;
}

.cmy::before { color: #f0f; transform: translateX(-3px); } /* magenta */
.cmy::after  { color: #ff0; transform: translateX(3px); }  /* yellow */
Enter fullscreen mode Exit fullscreen mode

Try the live interactive demo →

The rule of thumb: RGB + screen on dark, CMY + multiply on light.

Don't skip isolation: isolate

Without it, the red and blue copies can also blend with whatever is behind the heading, such as a gradient, a card or a photo, and the colors wash out. isolation: isolate gives the heading its own stacking context, so the layers blend only with each other.

Try the live interactive demo → (with and without isolation)

If it doesn't look right

  1. The page is too light. Switch to multiply with cyan, magenta and yellow.
  2. The layers are split between stacking contexts. An element only blends inside its nearest stacking context. Opacity below 1, filters and transforms all create one. The text and both copies must share it, so put isolation: isolate on the text element itself.
  3. data-text is missing. content: attr(data-text) then has nothing to show.
  4. The copies don't line up. The parent needs position: relative, and the copies need position: absolute; inset: 0.
  5. The text wraps. The copies may break lines in different places. Keep the word or heading on one line.

Accessibility

Some screen readers announce generated text. Give the copies empty alt text where the syntax is supported, and turn off the motion for people who ask for less. Put this after the rules above:

.rgb::before,
.rgb::after {
  content: attr(data-text);
  content: attr(data-text) / "";
}

@media (prefers-reduced-motion: reduce) {
  .rgb::before,
  .rgb::after {
    animation: none;
    transform: none;
  }
}
Enter fullscreen mode Exit fullscreen mode

Keep going

  • The full guide on Glitch Lab also covers RGB split on images and torn slices, and has an FAQ.
  • Chromatic Wave is this effect with a damped wave. You can type your own text, change the settings and copy the code.
  • Glitch Image splits a photo into channels on hover.

This article was created with the help of AI.

Top comments (0)