The cross-fade()
function takes a list of images with a percentage defining how much of each image is retained in terms of opacity when it is blended with the other images. The percent value must be coded without quotes, must contain the '%'
symbol, and its value must be between 0% and 100%.
The function can be used in CSS anywhere an ordinary image reference can be used.
Cross-fade percentages
Think of the percentage as an opacity value for each image. This means a value of 0% means the image is fully transparent while a value of 100% makes the image fully opaque.
cross-fade(url(white.png) 0%, url(black.png) 100%);
cross-fade(url(white.png) 25%, url(black.png) 75%);
cross-fade(url(white.png) 50%, url(black.png) 50%);
cross-fade(url(white.png) 75%, url(black.png) 25%);
cross-fade(url(white.png) 100%, url(black.png) 0%);
cross-fade(url(green.png) 75%, url(red.png) 75%);
If any percentages are omitted, all the specified percentages are summed together and subtracted from 100%
. If the result is greater than 0%, the result is then divided equally between all images with omitted percentages.
In the simplest case, two images are faded between each other. To do that, only one of the images needs to have a percentage, the other one will be faded accordingly. For example, a value of 0% defined for the first image yields only the second image, while 100% yields only the first. A 25% value renders the first image at 25% and the second at 75%. The 75% value is the inverse, showing the first image at 75% and the second at 25%.
The above could also have been written as:
cross-fade(url(white.png) 0%, url(black.png));
cross-fade(url(white.png) 25%, url(black.png));
cross-fade(url(white.png), url(black.png));
cross-fade(url(white.png) 75%, url(black.png));
cross-fade(url(white.png) 100%, url(black.png));
cross-fade(url(green.png) 75%, url(red.png) 75%);
If no percentages are declared, both the images will be 50% opaque, with a cross-fade rendering as an even merge of both images. The 50%/50% example seen above did not need to have the percentages listed, as when a percentage value is omitted, the included percentages are added together and subtracted from 100%. The result, if greater than 0, is then divided equally between all images with omitted percentages.
In the last example, the sum of both percentages is not 100%, and therefore both images include their respective opacities.
If no percentages are declared and three images are included, each image will be 33.33% opaque. The two following are lines (almost) identical:
cross-fade(url(red.png), url(yellow.png), url(blue.png));
cross-fade(url(red.png) 33.33%, url(yellow.png) 33.33%, url(blue.png) 33.33%);