Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

alpha() — relative alpha-channel change

`alpha()` creates a relative color that keeps the origin color channels and replaces or calculates only its alpha channel.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

alpha(from <color> / [<alpha-value> | none])

alpha() creates a relative color that keeps the origin color channels and replaces or calculates only its alpha channel.

The example applies alpha(from rgb(20 40 60 / 80%) / calc(alpha * .5)): the color channels stay unchanged, alpha falls from 0.8 to 0.4, and compositing on white produces a measurable pixel.

Grammar: alpha(from <color> / [<alpha-value> | none]).

The alpha component keyword exposes the origin alpha as a number where 1 means fully opaque. The result remains in the origin color space, calculated alpha is clamped to the range from 0 to 1, and none preserves a missing component. The alpha() function is currently marked at-risk in CSS Color 5, so it can be removed before that specification level stabilizes.

Examples

const swatch=document.createElement('div');document.body.append(swatch);swatch.style.backgroundColor='alpha(from rgb(20 40 60 / 80%) / calc(alpha * .5))';const computed=getComputedStyle(swatch).backgroundColor;if(!computed.includes('/ 0.4'))throw new Error('alpha() did not preserve the expected half alpha: '+computed);const canvas=document.createElement('canvas');canvas.width=1;canvas.height=1;const ctx=canvas.getContext('2d');ctx.fillStyle='white';ctx.fillRect(0,0,1,1);ctx.fillStyle=computed;ctx.fillRect(0,0,1,1);const pixel=[...ctx.getImageData(0,0,1,1).data];if(pixel.slice(0,3).join(',')!=='161,169,177')throw new Error('unexpected alpha composite: '+pixel);console.log('computed='+computed+'; over-white='+pixel.slice(0,3).join(','));

Result

computed=color(srgb 0.0784314 0.156863 0.235294 / 0.4); over-white=161,169,177

`alpha()` creates a relative color that keeps the origin color channels and replaces or calculates only its alpha channel.

Sources