Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS property `mask-mode`

`mask-mode` chooses per layer whether mask values come from alpha, from luminance multiplied by alpha, or from the source type.

Version: CSS modules; captured CSSWG editor index includes draft and obsolete features requiring review

Syntax

<masking-mode>#

mask-mode chooses per layer whether mask values come from alpha, from luminance multiplied by alpha, or from the source type.

The Chromium raster example uses opaque black: alpha keeps it visible at 255, luminance hides it at 0, and match-source yields 255 for a CSS gradient but 0 for a URL to <mask mask-type="luminance">.

Grammar: <masking-mode>#.

match-source means alpha for an ordinary <image>, but for a URL to an SVG <mask> it follows that element’s mask-type; explicit alpha or luminance overrides the source choice. Fully opaque black is therefore a full mask in alpha mode and a zero mask in luminance mode. The initial value is match-source; its list repeats or truncates to the mask-image layer count and does not affect mask-border-source.

Examples

<!doctype html><meta charset="utf-8"><svg id="stage" xmlns="http://www.w3.org/2000/svg" width="150" height="20"><defs><mask id="lum" maskUnits="userSpaceOnUse" x="120" y="0" width="30" height="20" mask-type="luminance"><rect x="120" width="30" height="20" fill="black"/></mask></defs><rect id="alphaBox" width="30" height="20" fill="orange" style="mask-image:linear-gradient(black,black);mask-mode:alpha"/><rect id="luminanceBox" x="40" width="30" height="20" fill="orange" style="mask-image:linear-gradient(black,black);mask-mode:luminance"/><rect id="matchImage" x="80" width="30" height="20" fill="orange" style="mask-image:linear-gradient(black,black);mask-mode:match-source"/><rect id="matchMask" x="120" width="30" height="20" fill="orange" style="mask-image:url(#lum);mask-mode:match-source"/></svg><pre id="result"></pre><script>

const rasterize=svg=>new Promise((resolve,reject)=>{const image=new Image();image.onload=()=>{const canvas=document.createElement('canvas');canvas.width=Number(svg.getAttribute('width'));canvas.height=Number(svg.getAttribute('height'));const context=canvas.getContext('2d');context.drawImage(image,0,0);resolve(context)};image.onerror=()=>reject(new Error('SVG rasterization failed'));image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(new XMLSerializer().serializeToString(svg))});
const alpha=(context,x,y)=>context.getImageData(x,y,1,1).data[3];
(async()=>{const context=await rasterize(stage),a=alpha(context,15,10),l=alpha(context,55,10),mi=alpha(context,95,10),mm=alpha(context,135,10),modes=[alphaBox,luminanceBox,matchImage,matchMask].map(node=>getComputedStyle(node).maskMode).join('/');if(a<250||l>5||mi<250||mm>5||modes!=='alpha/luminance/match-source/match-source')throw new Error('mask-mode interpretation mismatch');result.textContent='alpha='+a+'; luminance='+l+'; match-image='+mi+'; match-mask='+mm+'; modes='+modes})().catch(error=>result.textContent='ERROR:'+error.message);
</script>

Result

alpha=255; luminance=0; match-image=255; match-mask=0; modes=alpha/luminance/match-source/match-source

The example was executed locally in an isolated, network-free Chrome session; the result comes from the browser CSS API.

Sources