{"id":333543,"date":"2021-02-03T08:30:02","date_gmt":"2021-02-03T16:30:02","guid":{"rendered":"https:\/\/css-tricks.com\/?page_id=333543"},"modified":"2021-05-10T10:58:04","modified_gmt":"2021-05-10T17:58:04","slug":"mask-mode","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/m\/mask-mode\/","title":{"rendered":"mask-mode"},"content":{"rendered":"\n
The The property accepts one keyword value, or a comma-separated list of two or all three of the This property can take a comma-separated list of values for mask modes and each value is applied to a corresponding mask layer image specified in the mask-image property.<\/p>\n\n\n\n In the following example, the first value specifies the mask mode corresponding to the first image, the second value for the second image, and so on.<\/p>\n\n\n\n Masking in CSS comes with two methods that have some differences in calculating the mask values.<\/p>\n\n\n Images are made of pixels, each pixel has some data containing color values and maybe alpha values with transparency information. An image with an alpha channel can be an alpha mask<\/strong>, like PNG images with black and transparent areas.<\/p>\n\n\n\n In a simple masking operation, we have an element and a mask image placed on top of it. The alpha value of each pixel in the mask image will be merged with its corresponding pixel in the element.<\/p>\n\n\n\n So, in this method, the mask value at a given point is simply the value of the alpha channel at that point of the mask image and the color channels do not contribute to the mask value.<\/p>\n\n\n\n The example bellow is an alpha mask which only contains black (alpha value of 1) and transparent areas (alpha value of 0) and you can see the result that has some parts fully visible and others fully transparent:<\/p>\n\n\n\n But in the following example, we are using a gradient that has different level of transparency. The result isn\u2019t only visible or transparent, but there are some translucent areas:<\/p>\n\n\n\n And here\u2019s what the result looks like in the browser:<\/p>\n\n\n\n In a luminance mask, colors and alpha values matter. When the alpha value is 0 (i.e. fully transparent), the element is hidden; when there alpha value is 1, the mask values vary depending on the color channel of that pixel. For example, when the color is white, the element is visible; in the case of black area, the element is hidden.<\/p>\n\n\n\n While calculating the mask values in an alpha mask is based only on the alpha values of the mask image, the mask values of a luminance mask<\/strong> are computed from the luminance and the alpha values. Browsers do this in the following steps:<\/p>\n\n\n\n \/explanation For more information about these calculations you can check out the mask processing<\/a> section in the CSS Masking Module 1 specification<\/a> from the September 2019 Editor\u2019s Draft.<\/p>\n\n\n\n Bellow is a mask image with a white sun in the center and transparent areas around it. As you can see, the while areas are fully visible:<\/p>\n\n\n\n And in the next example, a colorful gradient is used as a mask image and you can see the effect of different colors on the mask values in the luminance mode:<\/p>\n\n\n\n In the following demo we are using a mask image with transparent and black areas:<\/p>\n\n\n\nmask-mode<\/code> CSS property indicates whether the CSS mask layer image<\/a> is treated as an alpha mask or a luminance mask.<\/p>\n\n\n\n\n\n\n\n
.element {\n mask-image: url(sun.svg);\n mask-mode: alpha;\n}<\/code><\/pre>\n\n\n
Syntax<\/h3>\n\n\n
mask-mode: alpha | luminance | match-source<\/code><\/pre>\n\n\n\n
alpha<\/code>,
luminance<\/code> and
mask-source<\/code> values.<\/p>\n\n\n\n
match-source<\/code><\/li>
Values<\/h3>\n\n\n
\/* Keyword values *\/\nmask-mode: alpha;\nmask-mode: luminance;\nmask-mode: match-source;\n\n\/* Global values *\/\nmask-mode: inherit;\nmask-mode: initial;\nmask-mode: unset;<\/code><\/pre>\n\n\n\n
alpha<\/code>:<\/strong> specifies that the alpha values (alpha channel) of the mask layer image should be used as the mask values.<\/li>
luminance<\/code>:<\/strong> specifies that the luminance values of the mask image should be used as the mask values.<\/li>
match-source<\/code>:<\/strong> the default value, which sets the mask mode to alpha if the mask reference of the
mask-image<\/code> property is a CSS
<image><\/code> element like an image URL or a gradient. However, if the mask reference of the
mask-image<\/code> property is an SVG
<mask><\/code> element, the value specified by the referenced
<mask><\/code> element\u2019s mask-type<\/a> property must be used.<\/li>
initial<\/code>:<\/strong> applies the property\u2019s default setting, which is
match-source<\/code>.<\/li>
inherit<\/code>:<\/strong> adopts the mask-mode value of the parent.<\/li>
unset<\/code>:<\/strong> removes the current mask-mode from the element.<\/li><\/ul>\n\n\n
Using multiple values<\/h3>\n\n\n
.element {\n mask-image: url(image1.png), url(image2.png), url(image3.png);\n mask-mode: luminance, alpha, match-source;\n}<\/code><\/pre>\n\n\n
Alpha and luminance masks<\/h3>\n\n\n
Alpha masks<\/h4>\n\n\n
img {\n mask-image: linear-gradient(black, transparent);\n mask-mode: alpha;\n}<\/code><\/pre>\n\n\n\n
Luminance masks<\/h4>\n\n\n
Demo<\/h3>\n\n\n