liveBook · Manning

Mask-size

As explained in CSS in Depth, Second Edition

The CSS mask-size property specifies the dimensions of a mask image using lengths, percentages, or sizing keywords.

mask-size sets a mask image’s horizontal and vertical dimensions with lengths, percentages, or the keywords cover, contain, and auto. It works much like background-size, controlling how the mask fits the element. section 14.2.3 section 14.2

Use mask-size with related mask properties to control the visible result:

  • Set explicit dimensions such as 100% 100% to stretch a mask across the element. section 14.2
  • Use cover or contain when the mask should scale proportionally within the element. section 14.2
  • Place mask-size after mask-position and a slash when using the mask shorthand. section 14.2.3
  • Omit mask-size for gradients that automatically fill the element. section 14.2.1

Stretching a star mask

A 300px square element can display a background image through a star-shaped mask by sizing the mask to the element: -webkit-mask-size: 100% 100%; mask-size: 100% 100%; section 14.2

Listing 14.6 Applying an image mask
.mask {
  width: 300px;
  min-height: 300px;
  background-image: url(images/bird.jpg);
  background-size: cover;
  background-position: 100%;
  -webkit-mask-image: url(images/star-mask.png);    #1
  -webkit-mask-size: 100% 100%;                     #1
  mask-image: url(images/star-mask.png);       #2
  mask-size: 100% 100%;           #3
}

Inspect the complete image-mask example, including the prefixed fallback and the unprefixed mask-size declaration. section 14.2

How CSS in Depth, Second Edition covers it

How deep it goes
Defines accepted values and shorthand placement, then demonstrates explicit sizing without detailing browser rendering internals.
Examples
Uses a star-shaped image mask sized to 100% 100% and contrasts it with gradients that fill the element automatically.
What it assumes
Assumes familiarity with CSS backgrounds and introduces mask sizing alongside mask images, gradients, and related mask properties.
What you can do after
Apply it via CSS to stretch, contain, or cover image masks and diagnose invisible mask behavior through background-image comparisons.
How it connects
Connects mask-size with mask-image, mask-position, mask shorthand, mask-repeat, gradients, and background-size.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.