Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

CSS property `mask-image`

`mask-image` selects, for each layer, no image, a CSS image such as a gradient, or a URL to an image or SVG `<mask>` element.

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

Syntax

<mask-reference>#

mask-image selects, for each layer, no image, a CSS image such as a gradient, or a URL to an image or SVG <mask> element.

The Chromium raster example compares four sources: a lone none leaves the sample at alpha 255, the gradient yields 255/0 across its boundary, a valid URL to an SVG half-mask also yields 255/0, and a transparent image supplied by URL yields 0.

Grammar: <mask-reference>#.

The initial value is none. Inside a multi-layer list, none still occupies a transparent-black layer and can affect compositing. An empty image, download failure, missing reference, or unsupported source likewise becomes transparent black rather than revealing the element. Other mask-* lists are matched to the mask-image item count; a value other than none creates a stacking context.

Examples

<!doctype html><meta charset="utf-8"><svg id="stage" xmlns="http://www.w3.org/2000/svg" width="150" height="20"><defs><mask id="half" maskUnits="userSpaceOnUse" x="80" y="0" width="30" height="20" mask-type="alpha"><rect x="80" width="15" height="20" fill="white"/></mask></defs><rect id="plain" width="30" height="20" fill="purple" style="mask-image:none"/><rect id="gradient" x="40" width="30" height="20" fill="purple" style="mask-image:linear-gradient(to right,black 0 50%,transparent 50%)"/><rect id="linked" x="80" width="30" height="20" fill="purple" style="mask-image:url(#half);mask-mode:alpha"/><rect id="missing" x="120" width="30" height="20" fill="purple" style="mask-image:url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221%22%20height%3D%221%22%2F%3E)"/></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),none=alpha(context,15,10),gL=alpha(context,45,10),gR=alpha(context,65,10),uL=alpha(context,85,10),uR=alpha(context,105,10),transparent=alpha(context,135,10),computed=getComputedStyle(gradient).maskImage;if(none<250||gL<250||gR>5||uL<250||uR>5||transparent>5||!computed.startsWith('linear-gradient'))throw new Error('mask-image source boundary mismatch '+[none,gL,gR,uL,uR,transparent,computed]);result.textContent='none='+none+'; gradient='+gL+'/'+gR+'; url='+uL+'/'+uR+'; transparent-url='+transparent+'; computed-gradient=true'})().catch(error=>result.textContent='ERROR:'+error.message);
</script>

Result

none=255; gradient=255/0; url=255/0; transparent-url=0; computed-gradient=true

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

Sources