mask

基线 2023

新功能

2023 年 12 月起,此功能可在最新的设备和浏览器版本中使用。此功能可能在较旧的设备或浏览器中无法使用。

mask CSS 简写属性 通过在特定点遮罩或裁剪图像来隐藏元素(部分或全部)。

注意:除了下面列出的属性外,mask 简写还将 mask-border 重置为其初始值。因此,建议使用 mask 简写而不是其他简写或单个属性来覆盖级联中较早的任何遮罩设置。这将确保 mask-border 也已重置,以允许新的样式生效。

构成属性

语法

css
/* Keyword values */
mask: none;

/* Image values */
mask: url(mask.png); /* Pixel image used as mask */
mask: url(masks.svg#star); /* Element within SVG graphic used as mask */

/* Combined values */
mask: url(masks.svg#star) luminance; /* Element within SVG graphic used as luminance mask */
mask: url(masks.svg#star) 40px 20px; /* Element within SVG graphic used as mask positioned 40px from the top and 20px from the left */
mask: url(masks.svg#star) 0 0/50px 50px; /* Element within SVG graphic used as mask with a width and height of 50px */
mask: url(masks.svg#star) repeat-x; /* Element within SVG graphic used as horizontally repeated mask */
mask: url(masks.svg#star) stroke-box; /* Element within SVG graphic used as mask extending to the box enclosed by the stroke */
mask: url(masks.svg#star) exclude; /* Element within SVG graphic used as mask and combined with background using non-overlapping parts */

/* Global values */
mask: inherit;
mask: initial;
mask: revert;
mask: revert-layer;
mask: unset;

/* Multiple masks */
mask:
  url(masks.svg#star) left / 16px repeat-y,
  /* Element within SVG graphic is used as a mask on the left-hand side with a width of 16px */
    url(masks.svg#circle) right / 16px repeat-y; /* Element within SVG graphic is used as a mask on the right-hand side with a width of 16px */

<mask-reference>

设置遮罩图像源。请参阅 mask-image

<masking-mode>

设置遮罩图像的遮罩模式。请参阅 mask-mode

<position>

设置遮罩图像的位置。请参阅 mask-position

<bg-size>

设置遮罩图像的大小。请参阅 mask-size

<repeat-style>

设置遮罩图像的重复。请参阅 mask-repeat

<geometry-box>

如果只给出一个 <geometry-box> 值,则它会同时设置 mask-originmask-clip。如果存在两个 <geometry-box> 值,则第一个设置 mask-origin,第二个设置 mask-clip

<geometry-box> | no-clip

设置受遮罩图像影响的区域。请参阅 mask-clip

<compositing-operator>

设置当前遮罩层上使用的合成操作。请参阅 mask-composite

正式定义

初始值与简写属性的每个属性相同
应用于所有元素;在 SVG 中,它应用于容器元素,不包括 <defs> 元素和所有图形元素
继承
百分比与简写属性的每个属性相同
计算值与简写属性的每个属性相同
  • mask-image: 按指定方式,但 url() 值已设为绝对值
  • mask-mode:按指定值处理
  • mask-repeat:包含两个关键字,每个维度一个
  • mask-position:包含两个表示原点和从该原点偏移的关键字,每个都以绝对长度(如果给出<length>)给出,否则以百分比给出。
  • mask-clip:按指定值处理
  • mask-origin:按指定值处理
  • mask-size:按指定值处理,但将相对长度转换为绝对长度
  • mask-composite:按指定值处理
动画类型与简写属性的每个属性相同
创建堆叠上下文

正式语法

mask = 
<mask-layer>#

<mask-layer> =
<mask-reference> ||
<position> [ / <bg-size> ]? ||
<repeat-style> ||
<geometry-box> ||
[ <geometry-box> | no-clip ] ||
<compositing-operator> ||
<masking-mode>

<mask-reference> =
none |
<image> |
<mask-source>

<position> =
[ left | center | right | top | bottom | <length-percentage> ] |
[ left | center | right ] && [ top | center | bottom ] |
[ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] |
[ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ]

<bg-size> =
[ <length-percentage [0,∞]> | auto ]{1,2} |
cover |
contain

<repeat-style> =
repeat-x |
repeat-y |
[ repeat | space | round | no-repeat ]{1,2}

<geometry-box> =
<shape-box> |
fill-box |
stroke-box |
view-box

<compositing-operator> =
add |
subtract |
intersect |
exclude

<masking-mode> =
alpha |
luminance |
match-source

<image> =
<url> |
<gradient>

<mask-source> =
<url>

<length-percentage> =
<length> |
<percentage>

<shape-box> =
<visual-box> |
margin-box

<url> =
<url()> |
<src()>

<visual-box> =
content-box |
padding-box |
border-box

<url()> =
url( <string> <url-modifier>* ) |
<url-token>

<src()> =
src( <string> <url-modifier>* )

示例

蒙版图像

css
.target {
  mask: url(#c1) luminance;
}

.anothertarget {
  mask: url(resources.svg#c1) 50px 30px/10px 10px repeat-x exclude;
}

规范

规范
CSS 蒙版模块级别 1
# the-mask

浏览器兼容性

BCD 表格仅在浏览器中加载

另请参阅