liveBook · Manning

Mask

As explained in CSS in Depth, Second Edition

Masking is a CSS visual technique that selectively reveals or hides portions of an element using a mask image.

Masking selectively reveals or hides portions of an element using a mask image. Unlike border-radius, it can create irregular shapes while preserving the element’s background or content. section 14.2

Reveal an image through a star

Give an element a background image, then apply a mostly transparent mask containing an opaque star. The browser shows the element where the mask is visible and hides the rest. Use mask-size: 100% 100% to align the mask with the element, and include the -webkit-mask-image fallback when needed. 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 CSS pattern for applying a star-shaped image mask to a background image. section 14.2

Choose the mask source and behavior according to the visual effect:

  • Use an image, gradient, or SVG mask as mask-image; combine multiple masks with mask-composite. section 14.2.3
  • Control placement, dimensions, tiling, origin, and affected area with mask-position, mask-size, mask-repeat, mask-origin, and mask-clip. section 14.2.3
  • Use opacity or a gradient to create partial visibility, fades, vignettes, or stripes rather than only hard-edged shapes. section 14.2.1
  • Debug an invisible mask by temporarily applying the same source as background-image, making its shape visible while correcting the declarations. section 14.2.1
Listing 14.7 Applying a gradient as a mask
.mask {
  width: 300px;
  min-height: 300px;
  background-image: url(images/bird.jpg);
  background-size: cover;
  background-position: 100%;
  -webkit-mask-image: linear-gradient(to bottom, transparent, black);
  mask-image: linear-gradient(to bottom, transparent, black);
}

Inspect a gradient mask that fades an image from transparent at the top to black at the bottom. section 14.2.1

How CSS in Depth, Second Edition covers it

How deep it goes
Explains the opacity principle, demonstrates image and gradient masks, and catalogs related properties without descending into rendering internals.
Examples
Uses a star-shaped image mask, a fading linear gradient, a radial vignette, and repeating-gradient stripes.
What it assumes
Assumes basic CSS backgrounds and URLs while teaching mask images, gradients, opacity, and browser loading constraints.
What you can do after
Build masked images, tune their placement and repetition, create gradient fades, and diagnose invisible or misaligned masks.
How it connects
Connects masking with background images, gradients, SVG masks, filters, clipping paths, floats, and shape-outside.

Also explained in

Compare how 7 books explain mask →

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.