mask-size

Mojtaba Seyedi on Updated on
##description##
Ad
`, } ); } })();

In CSS, the mask-size property specifies the size of a mask layer image. In many ways, it works very much like the background-size property.

.element {
  mask-image: url(star.svg);
  mask-size: 200px 100px;
}

Masking allows you to display selected parts of an element while hiding the rest. The size of the mask is defined by the mask-size property.

In the following demo you can play around with the size of the mask layer image:

Syntax

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

That’s basically saying that the syntax accepts a background size (<bg-size>) value that can either be one or two lengths and/or percentages (<length-percentage>), set to auto,  or one of two keywords (cover and contain).

Values

/* Lengths */
mask-size: 200px; /* width is 200px and height is auto */
mask-size: 50% 100%; /* width is 50% and height is 100% */


/* Keywords */
mask-size: contain;
mask-size: cover;


/* Global values */
mask-size: auto;
mask-size: intial;
mask-size: inherit;
mask-size: unset;

Value definitions

Using multiple values

This property can take a comma-separated list of values for mask sizes and each value is applied to a corresponding mask layer image specified in the mask-image property.

In the following example, the first value specifies the size of the first image, the second value specifies the size of the second image, and so on.

.element {
  mask-image: url(image1.png), url(image2.png), url(image3.png);
  mask-size: 100px 100%, auto, contain;
}

The auto value

If the value of the mask-size property is specified as auto, like this:

.element {
  mask-size: auto auto;
  /* or */
  mask-size: auto;
}

…then the mask image scales in the corresponding directions in order to maintain its aspect ratio. That said,  we can get various results depending on the image’s intrinsic dimensions and proportion.

Proportion/DimensionNo intrinsic dimensionsOne intrinsic dimensionBoth intrinsic dimensions
Has ProportionRendered as if contain had been specified insteadRendered at the size determined by that one dimension and the proportionRendered at that size
No ProportionRendered at the size of the mask positioning areaRendered using the intrinsic dimension and the corresponding dimension of the mask positioning areaN/A

If the value of mask-size is specified with auto and another non-auto value like the following:

.element {
  mask-size: auto 200px;
}

…then:

Understanding cover and contain

The following video explains how the contain and cover keywords work. Note that the initial position of a mask layer is at the center of the positioning area:

If the image has no intrinsic aspect ratio, then specifying either cover or contain renders the mask image at the size of the mask positioning area.

And just what the heck is an intrinsic dimension and intrinsic proportion?

Intrinsic dimensions are width and height of an element and intrinsic proportion is ratio of them.

Demo

The following demo uses a length for the mask-size. Try change the value to other types of values in the code and check the result.

Browser support

More information