mask-size CSS property specifies the sizes of the mask images. The size of the image can be fully or partially constrained in order to preserve its intrinsic ratio.

/* Keywords syntax */
mask-size: cover;
mask-size: contain;
/* One-value syntax */
/* the width of the image (height set to 'auto') */
mask-size: 50%;
mask-size: 3em;
mask-size: 12px;
mask-size: auto;
/* Two-value syntax */
/* first value: width of the image, second value: height */
mask-size: 50% auto;
mask-size: 3em 25%;
mask-size: auto 6px;
mask-size: auto auto;
/* Multiple values */
/* Do not confuse this with mask-size: auto auto */
mask-size: auto, auto;
mask-size: 50%, 25%, 25%;
mask-size: 6px, auto, contain;
/* Global values */
mask-size: inherit;
mask-size: initial;
mask-size: unset;
					
注意: If the value of this property is not set in a mask shorthand property that is applied to the element after the mask-size CSS property, the value of this property is then reset to its initial value by the shorthand property.

句法

One or more <bg-size> values, separated by commas.

A <bg-size> can be specified in one of three ways:

  • using the keyword contain
  • using the keyword cover
  • using width and height values

To specify a size using width and height, you can supply one or two values:

  • If only one value is given it sets the width, with the height set to auto .
  • If two values are given, the first sets width and the second sets height.

Each value can be a <length> <percentage> ,或 auto .

<length>
A <length> value scales the mask image to the specified length in the corresponding dimension. Negative lengths are not allowed.
<percentage>
A <percentage> value scales the mask image in the corresponding dimension to the specified percentage of the mask positioning area, which is determined by the value of mask-origin . The mask positioning area is, by default, the area containing the content of the box and its padding; the area may also be changed to just the content or to the area containing borders, padding and content. Negative percentages are not allowed.
auto

A keyword that scales the mask image in the corresponding directions in order to maintain its intrinsic proportion.

contain
A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image is letterboxed within the container. The image is automatically centered unless over-ridden by another property such as mask-position .
cover
A keyword that is the inverse of contain . Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container. When the image and container have different dimensions, the image is clipped either on left/right or at top/bottom.

The interpretation of possible values depends on the image's intrinsic dimensions (width and height) and intrinsic proportion (ratio of width and height). A bitmap image always has intrinsic dimensions and an intrinsic proportion. A vector image may have both intrinsic dimensions and thus it has an intrinsic proportion too. It also may have one or no intrinsic dimensions and in either case it might or might not have an intrinsic proportion. Gradients are treated as images with no intrinsic dimensions or intrinsic proportion.

The rendered size of the mask image is then computed as follows:

If both components of mask-size are specified and are not auto :

The mask image renders at the specified size.

mask-size is contain or cover :

The image is rendered by preserving its intrinsic proportion at the largest size contained within or covering the mask positioning area. If the image has no intrinsic proportion, then it is rendered at the size of the mask positioning area.

mask-size is auto or auto auto :
If the image has both intrinsic dimensions, it is rendered at that size. If it has no intrinsic dimensions and no intrinsic proportion, it is rendered at the size of the mask positioning area. If it has no dimensions but has a proportion, it's rendered as if contain had been specified instead. If the image has one intrinsic dimension and a proportion, it's rendered at the size determined by that one dimension and the proportion. If the image has one intrinsic dimension but no proportion, it's rendered using the intrinsic dimension and the corresponding dimension of the mask positioning area.
mask-size has one auto component and one non- auto component:

If the image has an intrinsic proportion, then render it using the specified dimension and compute the other dimension from the specified dimension and the intrinsic proportion. If the image has no intrinsic proportion, use the specified dimension for that dimension. For the other dimension, use the image's corresponding intrinsic dimension if there is one. If there is no such intrinsic dimension, use the corresponding dimension of the mask positioning area.

形式定义

初始值 auto
适用于 all elements; In SVG, it applies to container elements excluding the defs element and all graphics elements
继承 no
计算值 as specified, but with relative lengths converted into absolute lengths
动画类型 repeatable list of simple list of length, percentage, or calc

形式句法

<bg-size>#

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

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

范例

Setting mask size as a percentage

CSS

#masked {
  width: 200px;
  height: 200px;
  background: blue linear-gradient(red, blue);
  -webkit-mask-image: url(https://mdn.mozillademos.org/files/12668/MDN.svg);
  mask-image: url(https://mdn.mozillademos.org/files/12668/MDN.svg);
  -webkit-mask-size: 50%;
  mask-size: 50%; /* Can be changed in the live sample */
  margin-bottom: 10px;
}
					

HTML

<div id="masked">
</div>
<select id="maskSize">
  <option value="auto">auto</option>
  <option value="40% 80%">40% 80%</option>
  <option value="50%" selected>50%</option>
  <option value="200px 100px">200px 100px</option>
  <option value="cover">cover</option>
  <option value="contain">contain</option>
</select>
					

JavaScript

var maskSize = document.getElementById("maskSize");
maskSize.addEventListener("change", function (evt) {
  document.getElementById("masked").style.maskSize = evt.target.value;
});
					

结果

规范

规范 状态 注释
CSS Masking Module Level 1
The definition of 'mask-size' in that specification.
候选推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
mask-size Chrome 完整支持 4 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge 完整支持 18 Firefox 完整支持 53
完整支持 53
不支持 20 — 53 Disabled
Disabled ). To change preferences in Firefox, visit
IE 不支持 No Opera 完整支持 15 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari 完整支持 4 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
WebView Android 完整支持 4.4 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android 完整支持 18 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android 完整支持 53
完整支持 53
不支持 20 — 53 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 14 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari iOS 完整支持 2 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Samsung Internet Android 完整支持 1.0 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. CSS Masking
  4. 特性
    1. clip
    2. clip-path
    3. mask
    4. mask-border
    5. mask-border-mode
    6. mask-border-outset
    7. mask-border-repeat
    8. mask-border-slice
    9. mask-border-source
    10. mask-border-width
    11. mask-clip
    12. mask-composite
    13. mask-image
    14. mask-mode
    15. mask-origin
    16. mask-position
    17. mask-repeat
    18. mask-size
    19. mask-type

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1