CSS mask-image Explained: Fade Edges Without Gradient Divs
Horizontally scrolling chip lists and tab bars usually hint 'there's more to scroll' with an absolutely positioned gradient div glued to each edge. It works on a solid background and falls apart on anything else. mask-image fades the actual content, not a fake overlay.

Open any app with a horizontally scrolling row — a chip list, a tab bar, a row of story avatars — and look at the right edge. Chances are there's a soft fade there, a little visual whisper that says "there's more, keep scrolling." Now switch the page to dark mode, or scroll until a product photo sits right under that fade.
Watch the fade turn into a flat, wrong-colored smudge sitting on top of the photo.
That's not a rendering bug. That's the fade working exactly as built — and the way it's built is the problem.
That's the standard trick, and it's everywhere. An absolutely positioned pseudo-element, pinned to the edge, painted with a gradient from transparent to a hardcoded color — usually whatever the background happens to be that day. It looks completely fine in whatever screenshot it was built against.
Before you keep reading: guess what happens the first time a designer swaps that background for an image. You already know, because you've seen it happen to someone else's component.
The pseudo-element isn't fading anything. It's painting a second, opaque layer of "white, going transparent" directly on top of whatever's really there, and hoping the two colors happen to match. When the row sits on a flat #fff background, the illusion holds — the gradient blends into the page and nobody notices there's a div-shaped lie sitting on top of the content.
The moment that assumption breaks — dark mode, a photo, a gradient background, a themeable design system — the lie is visible. You get a pale rectangle sitting over a dark background, or a hard-edged color band stamped across a product image, because the overlay never had any idea what was underneath it. It can't. It's a separate element, painted independently, guessing at a color.
So the "fix" becomes: read the computed background color in JS, update a CSS variable, recompute it on theme change, recompute it again if the row ever scrolls under something with a different background per item. You end up maintaining a tiny color-matching service for a decoration.
Here's the part that's easy to miss: the problem was never the color. It's that you're trying to hide pixels with more pixels, instead of making the pixels that are already there fade out.
CSS masking does something structurally different from an overlay. Instead of painting a new layer on top, mask-image takes an image — including a CSS gradient — and uses its alpha channel to control the opacity of the element it's applied to. Where the mask is fully opaque, the element renders normally. Where the mask fades to transparent, the element fades to transparent too — whatever is actually being rendered there: a photo, a solid chip, a gradient, dark-mode content, all of it.
No extra element. No hardcoded color to keep in sync with the theme. The gradient's actual color doesn't matter at all here — black and white behave identically, because what the browser reads is the alpha value at each point along the gradient, not the hue. transparent has 0 alpha, black has full alpha, and everything in between blends smoothly — so the row's own content, whatever it is, fades with it.
That -webkit- line isn't decoration — it's the one still worth keeping around. Masking has solid support across current Chrome, Firefox, and Safari, but it shipped under the -webkit- prefix first and the unprefixed property arrived later, so pairing both costs nothing and covers the stragglers. Browsers ignore the prefixed declaration they don't need and use the one they do.
Runs right in your browser — poke at it and watch the concept react live.
Try it yourself: the live demo below puts the overlay-div trick and mask-image side by side over the exact same scrolling content, lets you drag the fade width, and flips the background so you can watch one of them break.
Here's a question the gradient-div approach never had to face, because it never touched the real content: what happens to the stuff inside the fade? With mask-image, the answer is simple and a little surprising — the content is still fully there. Masking only ever changes what gets painted; it never removes anything from layout, from the DOM, or from the accessibility tree.
That means a masked-out chip at the edge of the row is still focusable, still readable by a screen reader, and still scrolls into full view the instant the user keeps scrolling — the fade is purely visual, the same way -webkit-line-clamp only ever clips what's painted, never what's selectable. If you were relying on the overlay div to also block interaction with the edge content — don't; pointer-events: none on the old pseudo-element did that job, and a bare mask-image won't do it for you. Mask what's painted, and handle interaction separately if you actually need to.
If your scroll row's fade is still two pseudo-elements and a color variable you update on every theme change, you're not doing anything wrong exactly — you're doing extra work for a worse result. Swap it for a mask-image gradient, keep the -webkit- line next to it, and delete the JS that was keeping the fade color in sync with the background. The fade stops needing to know what's behind it, because it's not painting over it anymore — it's revealing it, gradually, and that's the whole trick.
What's the ugliest "fake the background color" hack sitting in your CSS right now that this would quietly replace? I'd genuinely like to know.
Think it clicked? Take the 8-question quiz →
Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.
🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.
Thanks for reading! Let's stay connected:
- ⭐ GitHub — follow me and star the projects: github.com/parsajiravand
- 💬 Discord — join the frontend best-practices community: discord.gg/d9KRhuAwQ
- 📸 Instagram — frontend best practices, daily: @bestpractice___
Keep reading
One post a day, in your inbox
Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.
0 comments
Sign in to join the discussion, like comments, and save articles for later.