Masking: mask-image, Luminance and Alpha Masks, and the mask Properties
Images and gradients as masks: mask-image with alpha and luminance modes, sizing and repeating like backgrounds, combining layers with mask-composite, the mask shorthand, SVG masks and mask-type, border masks, fading edges and recolorable icons.
kvi5hnuCode Sprout team member7 min readOn this page
A mask is an image whose transparency, or brightness, decides how visible each part of an element is. Where clip-path (article 100) is all or nothing, a mask can be partial: a gradient mask fades an element out smoothly. Masks use the same syntax as backgrounds: images, gradients, sizes, positions, repeats and layers.
.fade-bottom { mask-image: linear-gradient(black 70%, transparent); } /* fade out the last 30% */
Masks work unprefixed in all browsers since Chrome 120. Older Chromium and Safari needed -webkit-mask-*, which still works.
Alpha and luminance masks

mask-mode chooses how a mask image is read:
mask-mode | Visible where the mask is | Hidden where the mask is |
|---|---|---|
alpha | opaque | transparent |
luminance | white and opaque | black or transparent |
match-source (default) | alpha for images and gradients; luminance for an SVG <mask> |
With the default, colors don't matter in images and gradients, only transparency. Tested in Chrome with a pixel check:
linear-gradient(black, white)as a mask showed the element fully, since every pixel of it is opaque.- With
mask-mode: luminance, the same gradient hid the black end and showed the white end. linear-gradient(black, transparent)faded the element out with the default mode.
So the usual pattern is a gradient from any opaque color to transparent.
Sizing, positioning and repeating
These properties work exactly like their background-* equivalents (article 49):
| Property | Default | Note |
|---|---|---|
mask-image | none | images, gradients, url(#svg-mask); a comma-separated list for layers |
mask-size | auto | cover, contain, lengths |
mask-position | 0% 0% | as background-position |
mask-repeat | repeat | as background-repeat; note it repeats by default |
mask-origin | border-box | where positioning starts; backgrounds default to padding-box |
mask-clip | border-box | the area the mask applies to; no-clip lets it apply beyond |
Tested in Chrome: mask-repeat computed to repeat, and mask-origin and mask-clip to border-box. Since masks repeat by default, an icon mask smaller than its element tiles, so set mask-repeat: no-repeat with a size and position:
.dotted { mask-image: radial-gradient(circle 6px, black 98%, transparent); mask-size: 16px 16px; } /* a dot pattern */
.logo-mask { mask: url(logo.svg) center / contain no-repeat; }
Combining layers: mask-composite
Several mask images are layered like backgrounds, the first listed on top. mask-composite says how each layer combines with the layers below it:
| Value | Result |
|---|---|
add (default) | visible where either layer is |
subtract | the current layer minus the layers below |
intersect | only where both are |
exclude | where exactly one is |
Tested in Chrome, with a full layer listed first and a left-half layer second:
subtractshowed only the right half: the full layer minus the half below it.- Listing them the other way round, the half minus the full layer, showed nothing.
intersectshowed the left half.
A common use is a gradient border that works with rounded corners, by cutting the padding box out of the border box:
.gradient-border {
border: 3px solid transparent;
border-radius: 12px;
background: linear-gradient(135deg, #ff3d7f, #5ee7df) border-box;
mask: linear-gradient(black, black) padding-box, linear-gradient(black, black) border-box;
mask-composite: exclude; /* only the border ring stays visible */
}
This hides the element's content too, so it's put on a pseudo-element, as in the playground below.

In the bottom row, the layers are the same in each pair; only mask-composite changes which parts stay visible.
The mask shorthand
.hero { mask: linear-gradient(black 80%, transparent) bottom / 100% 100% no-repeat; }
It takes, per layer, the image, position / size, repeat, origin, clip, composite and mode, in any order except that size follows position after a slash. Like all shorthands, it resets what you leave out, including mask-mode and mask-composite.
SVG masks and mask-type
mask-image: url(#id) can reference an SVG <mask> element, which can contain shapes, gradients and text. SVG masks are luminance masks by default. The mask-type property on the <mask> element changes that to alpha, so the gradient's opacity, not its brightness, decides visibility:
<svg width="0" height="0" aria-hidden="true">
<mask id="spot" maskContentUnits="objectBoundingBox" style="mask-type: alpha">
<radialGradient id="g"><stop offset="0.4" stop-color="black"/><stop offset="1" stop-color="black" stop-opacity="0"/></radialGradient>
<rect width="1" height="1" fill="url(#g)"/>
</mask>
</svg>
.spotlight { mask-image: url(#spot); }
If the masked element sets mask-mode to something other than match-source, it wins over the SVG's mask-type.
Border masks
The spec also defines mask-border, which slices a mask image over the border area like border-image (article 41). It's supported in Safari 17.2. Chrome supports only the old prefixed name: tested in Chrome, CSS.supports('mask-border', …) was false while -webkit-mask-box-image was true. Firefox doesn't support either. Use border-image or the composite technique above instead.
Practical masks
Fade the edges of a scroll container, so content appears to scroll under the edges:
.scroller {
overflow-x: auto;
mask-image: linear-gradient(90deg, transparent, black 2rem, black calc(100% - 2rem), transparent);
}
Recolorable icons. An SVG used as a mask on an element with a background-color takes any color, including currentColor, without editing the SVG:
.icon {
inline-size: 1.2em; block-size: 1.2em;
background-color: currentColor;
mask: url(icon-check.svg) center / contain no-repeat;
}
Masked content is still in the accessibility tree and still focusable; masking is purely visual. Text that's faded out by a mask is still read, and still selectable. Like clip-path, a mask creates a stacking context and is a backdrop root (article 98), and, unlike with clip-path, masked-out areas still receive clicks. Tested in Chrome: on an element whose right half was masked to transparent, elementFromPoint() found the element in both halves.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.photo {
block-size: 4.5rem; margin-top: 0.4rem; border-radius: 8px;
background: linear-gradient(#58a6d8, #a4d4ee 55%, #3f8f4f 56%, #2f6f3c);
mask-image: linear-gradient(90deg, black var(--fade, 60%), transparent);
}
.scroller {
display: flex; gap: 0.4rem; overflow-x: auto; margin: 0.6rem 0; padding: 0.3rem 0;
mask-image: linear-gradient(90deg, transparent, black 1.5rem, black calc(100% - 1.5rem), transparent);
}
.scroller span { flex: none; padding: 0.3rem 0.6rem; border-radius: 999px; background: #f4efe6; }
.row { display: grid; grid-template-columns: 4rem 1fr; gap: 0.6rem; align-items: center; }
.dots {
block-size: 4rem; background: linear-gradient(135deg, #8a3b12, #f4c95d);
mask-image: radial-gradient(circle 5px, black 96%, transparent); mask-size: 14px 14px;
}
.ring { position: relative; padding: 0.8rem; border-radius: 12px; text-align: center; font-weight: 700; }
.ring::before {
content: ""; position: absolute; inset: 0; border: 3px solid transparent; border-radius: inherit;
background: linear-gradient(135deg, #ff3d7f, #5ee7df) border-box;
mask: linear-gradient(black, black) padding-box, linear-gradient(black, black) border-box;
mask-composite: exclude;
}
.icons { grid-column: 1 / -1; margin: 0; display: flex; gap: 1rem; font-weight: 600; }
.icons span { display: inline-flex; align-items: center; gap: 0.3rem; }
.ok { color: #2f7d32; } .err { color: #c0392b; }
.icon {
display: inline-block; inline-size: 1.3em; block-size: 1.3em; background-color: currentColor;
mask: linear-gradient(black, black) center / 100% 24% no-repeat, /* a plus sign from two bars */
linear-gradient(black, black) center / 24% 100% no-repeat;
}
The slider moves where the gradient mask starts fading; the chips fade at both edges of the scroller; the dots come from a repeating radial mask; the ring uses mask-composite: exclude; and the same plus-sign mask, made from two gradient bars, takes each label's color through currentColor; an SVG file in mask works the same way.
What's current
maskand its longhands, unprefixed: all browsers since Chrome 120.mask-border: Safari only (Chrome as-webkit-mask-box-image).mask-typefor SVG masks: all browsers.
Common questions
Why does my colored gradient mask show the element fully?
By default, image and gradient masks use alpha, and an opaque gradient is fully opaque. Fade to transparent, or set mask-mode: luminance.
Why is my mask icon tiled?
mask-repeat defaults to repeat. Add no-repeat with a size.
Mask or clip-path?
clip-path for hard-edged shapes, and it's cheap; masks for soft edges, image shapes and partial transparency.
Does masking hide content from screen readers? No. It's purely visual.
Summary
- A mask's alpha (default for images and gradients) or luminance (default for SVG
<mask>) sets how visible each pixel is. - Mask sizing, positioning and repeating work like backgrounds, but masks repeat by default and use the border box.
mask-compositecombines layers:add,subtract,intersect,exclude; the first listed layer is on top.- Masks are great for fading edges, gradient borders and recolorable icons.
- Masking is visual only, creates a stacking context, and doesn't change layout.
Practice questions
Write a mask that fades the bottom 25% of an article preview to transparent.
Answer
.preview { mask-image: linear-gradient(black 75%, transparent); }
Why does mask-image: linear-gradient(white, black) not fade the element, and how do you fix it?
Answer
The default mode for gradients is alpha, and both colors are opaque, so the element stays fully visible. Add mask-mode: luminance, or use linear-gradient(black, transparent).
Write CSS for a 1.5em icon that takes the text color, using star.svg as a mask.
Answer
.star-icon {
display: inline-block; inline-size: 1.5em; block-size: 1.5em;
background-color: currentColor;
mask: url(star.svg) center / contain no-repeat;
}
Two masks are listed as mask-image: A, B with mask-composite: subtract. What's visible?
Answer
The parts of A that aren't covered by B. A is the top layer, and subtract removes the layers below it.
Previous: Clipping: clip-path With inset(), circle(), polygon(), path() and shape()
Next: Styling Forms: appearance, accent-color, field-sizing and Customizable select

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.