Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

cross-fade() — weighted image blending

`cross-fade()` defines an image made by weighted blending of one or more images or colors, painted in premultiplied sRGB.

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

Syntax

cross-fade( <cf-image># )

cross-fade() defines an image made by weighted blending of one or more images or colors, painted in premultiplied sRGB.

The example tests real Chromium: standard cross-fade(red 25%, blue 75%) returns false, so an earlier green background remains visible. Legacy -webkit-cross-fade() is supported and produces pixel 64,0,191, a red-blue mixture.

Grammar: cross-fade( <cf-image># ).

Each percentage must be from 0% through 100%. Omitted weights share the remainder; a total above 100% is normalized, while a total below 100% keeps the missing share as transparent. Natural dimensions are a weighted average of image arguments only. Since this Chromium does not support the unprefixed syntax, place an ordinary image or gradient fallback first and treat the prefixed form as historical compatibility only.

Examples

const standard = CSS.supports('background-image', 'cross-fade(red 25%, blue 75%)');
const legacyValue = '-webkit-cross-fade(linear-gradient(red,red),linear-gradient(blue,blue),0.75)';
const legacy = CSS.supports('background-image', legacyValue);
const sample = async background => {
  const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><foreignObject width="8" height="8"><div xmlns="http://www.w3.org/1999/xhtml" style="width:8px;height:8px;background:' + background + '"></div></foreignObject></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 = 8;
  const context = canvas.getContext('2d'); context.drawImage(image, 0, 0);
  return [...context.getImageData(4, 4, 1, 1).data].slice(0, 3).join(',');
};
const fallbackPixel = await sample('rgb(0 128 0);background-image:cross-fade(red 25%, blue 75%)');
const legacyPixel = legacy ? await sample(legacyValue) : 'unavailable';
console.log('standard=' + standard + '; legacy=' + legacy + '; fallback=' + fallbackPixel + '; legacy-pixel=' + legacyPixel);

Result

standard=false; legacy=true; fallback=0,128,0; legacy-pixel=64,0,191

`cross-fade()` defines an image made by weighted blending of one or more images or colors, painted in premultiplied sRGB.

Sources