Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

hwb() — hue mixed with white and black

`hwb()` describes an `sRGB` color by hue angle and proportions of whiteness and blackness, with optional alpha.

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

Syntax

hwb( [ from <color> ]? [ <hue> | none ] [ <percentage> | <number> | none ]{2} [ / [ <alpha-value> | none ] ]? )

hwb() describes an sRGB color by hue angle and proportions of whiteness and blackness, with optional alpha.

The example renders hwb(120 20% 30%) as 51,179,51,255. For hwb(40 80% 50%), the components exceed the range and normalize to gray 157,157,157,255; relative hwb(from rgb(100 150 200 / 50%) h 10% 20% / alpha) yields 26,114,203,128. Comma form hwb(120, 20%, 30%) returns false.

Grammar: hwb( [ from <color> ]? [ <hue> | none ] [ <percentage> | <number> | none ]{2} [ / [ <alpha-value> | none ] ]? ).

When W + B reaches or exceeds 100%, the pure-hue contribution disappears and whiteness and blackness normalize proportionally, so hue has no effect. Relative syntax starts with from, converts the origin color to sRGB, and exposes h, w, b, and alpha components; none represents a missing component. Modern syntax uses spaces and a slash before alpha, not commas.

Examples

const sample=async color=>{const svg='<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"><rect width="4" height="4" fill="'+color+'"/></svg>';const image=new Image();image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg);await image.decode();const canvas=document.createElement('canvas');canvas.width=canvas.height=4;const context=canvas.getContext('2d');context.drawImage(image,0,0);return [...context.getImageData(2,2,1,1).data].join(',')};
const absolute=await sample('hwb(120 20% 30%)');
const normalized=await sample('hwb(40 80% 50%)');
const relative=await sample('hwb(from rgb(100 150 200 / 50%) h 10% 20% / alpha)');
const commaSyntax=CSS.supports('color','hwb(120, 20%, 30%)');
console.log('absolute='+absolute+'; normalized='+normalized+'; relative='+relative+'; comma-syntax='+commaSyntax);

Result

absolute=51,179,51,255; normalized=157,157,157,255; relative=26,114,203,128; comma-syntax=false

`hwb()` describes an `sRGB` color by hue angle and proportions of whiteness and blackness, with optional alpha.

Sources