- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Masking
In CSS, masking is a powerful technique that allows you to control the visibility of an element by using another image or shape as a "stencil." Think of it like placing a cutout over a photo: only the parts visible through the holes remain on display. This enables sophisticated designs like shaped images, text textures, or smooth transitions that go far beyond standard rectangular boxes.
Masking selectively shows or hides parts of an element based on the mask's transparency (alpha channel) or brightness (luminance). This is achieved using the mask property or its various sub-properties.
Masking with mask-image
The mask-image property is the foundation of CSS masking. It sets an image, a gradient, or an SVG as the mask. By default, the element is visible wherever the mask image is opaque and hidden where it is transparent.
Syntax
.element {
/* Use a PNG or SVG with transparency */
mask-image: url('mask-shape.png');
/* Determines if the mask uses transparency (alpha) or brightness (luminance) */
mask-mode: alpha;
}
-webkit- prefix for masking properties to work correctly. For production, you should include both: -webkit-mask-image: url(...); and mask-image: url(...);.
Masking with mask
Much like the background property, the mask property is a shorthand that lets you define several masking attributes at once. This keeps your CSS clean and readable.
Syntax
.element {
/* Shorthand: image | position / size | repeat */
mask: url('mask-layer.png') center / cover no-repeat;
}
mask-size (like cover or contain) if your mask image doesn't match the element's aspect ratio. This prevents the mask from stretching awkwardly.
Masking with SVG
SVG elements are excellent for masks because they are resolution-independent and can contain complex paths. You can point to an external SVG file or even an inline <mask> element defined within your HTML.
Example
.profile-card {
/* Masking with a custom SVG shape */
mask-image: url('blob-shape.svg');
mask-repeat: no-repeat;
mask-size: contain;
}
mask-mode: alpha. Only the transparency (the alpha channel) determines what gets hidden. A solid black circle and a solid red circle will create the exact same mask.
Masking Modes
The mask-mode property defines the logic used to apply the mask. This is useful when your mask source isn't a transparent PNG but perhaps a high-contrast black-and-white image.
- alpha: The default. Uses the transparency of the image. Transparent areas hide the element; opaque areas show it.
- luminance: Uses the brightness of the image. White areas show the element, black areas hide it, and gray areas create semi-transparency.
- match-source: Automatically chooses the mode based on the type of mask source being used.
Masking with Gradients
CSS gradients are technically images, which means they make perfect masks. This is the most common way to create a "fade-out" effect where an image or text gradually disappears into the background.
Example: The Hero Fade
Imagine you want a hero image to smoothly fade into a solid background color at the bottom of the section.
.hero-image {
mask-image: linear-gradient(to bottom, black 60%, transparent 100%);
-webkit-mask-image: linear-gradient(to bottom, black 60%, transparent 100%);
}
Masking with Browser Support
While CSS masking is widely supported, it remains one of the more "fragmented" parts of CSS. Chrome, Safari, and Edge (WebKit/Blink engines) have supported it for years but largely under the -webkit- prefix. Firefox supports the standard properties but may have different behavior for SVG-based masks.
Summary
CSS masking is a versatile tool for any developer's toolkit. It allows you to break away from "boxy" layouts and create organic, artistic shapes and transitions. By mastering mask-image, understanding the difference between alpha and luminance, and remembering to use vendor prefixes, you can significantly elevate the visual polish of your web projects.