Masking and clipping
Definition and purpose
Clipping restricts paint to a geometric region; masking controls visibility using a mask image. Both can change the visible shape without changing the element’s normal layout box.
Clip a rectangle
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 100px; height: 80px; background: red; } #a { clip-path: inset(0 50% 0 0); } </style> </head> <body> <div id="a" aria-hidden="true"></div> </body></html>Only the left half paints; the width remains 100px.
Syntax and mental model
clip-path accepts a basic shape such as inset() or circle(). mask-image can be a gradient. In alpha mode an opaque black mask reveals just as much as opaque white; transparency hides. In luminance mode opaque black hides, opaque white reveals, and intermediate luminance and alpha affect coverage. Anti-aliased clip edges can contain partial coverage. Clipping affects hit testing outside its region, whereas masking alone does not remove masked areas from hit testing. Keep interactive targets and essential text unmasked.
Luminance versus alpha
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 100px; height: 80px; background: red; } #a { mask-image: linear-gradient(90deg, black 50%, white 50%); mask-mode: luminance; } </style> </head> <body> <div id="a" aria-hidden="true"></div> </body></html>Black hides the left half and white reveals the right half. In alpha mode both halves would be visible.
Common mistakes
A transparent pixel is not necessarily an absent interactive element. Do not use masking for access control or hiding sensitive information; the underlying content remains present.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Blending and isolation. Next lesson: Shapes and paint implications.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Masking and clipping — knowledge check
Five questions on masking and clipping.
What does opaque black do in alpha mode?
What does opaque black do in luminance mode?
Which property specifies a clipping region?
What happens to the layout box when clipped?
Can a masked area still receive pointer events?