CSS - Masking


Introduction to CSS Masking

CSS masking is a technique that lets you partially or fully hide parts of an element by using another element, image, or gradient as a mask. It provides a way to create visually appealing effects and to control the visibility of content on a web page.

Masking is useful when you want to create complex shapes, reveal or hide parts of an image, or create interesting visual effects. You can use it to create cut-out effects, spotlight effects, or to blend images together.

CSS masking has been around for a while, but it gained more attention and support in recent years. The concept of masking originated from graphics software like Adobe Photoshop, where designers could use masks to hide or reveal parts of an image. With the introduction of CSS masking properties, web developers can now achieve similar effects directly in the browser.

The CSS Masking Module Level 1 specification was first introduced in 2014 and has since been implemented in modern browsers. It defines several properties and values that allow you to control how masks are applied to elements.

There are two main techniques for masking in CSS:

Technique Description
Clip-path masking This technique uses the clip-path property to define a clipping region that determines which parts of an element are visible.
CSS Masks This technique uses the mask property and its related properties to apply alpha or luminance masks to an element.

In the following sections, we will look at these techniques in more detail and learn how to use them to create impressive masking effects in your web projects.

Example of Clip-path Masking

<div class="clip-path-example">
  <img src="image.jpg" alt="Example Image">
.clip-path-example {
  clip-path: circle(50% at 50% 50%);

Example of CSS Masks

<div class="mask-example">
  <img src="image.jpg" alt="Masked Image">
.mask-example {
  mask-image: url("mask.png");
  mask-size: cover;

Masking with CSS Clip-path

The clip-path property in CSS lets you create masks by defining a clipping region that determines which parts of an element are visible. It is a powerful tool for creating complex shapes and revealing or hiding specific areas of an element.

The clip-path property takes a shape value that defines the clipping path. CSS provides several basic shapes that you can use for clipping:

Shape Description
circle() Creates a circular clipping path.
ellipse() Creates an elliptical clipping path.
polygon() Creates a clipping path consisting of a polygon with specified points.
inset() Creates a rectangular clipping path inset from the element's edges.

Example: Using the circle() shape to create a circular clipping path

.clipped-element {
  clip-path: circle(50% at 50% 50%);

In this example, the circle() function takes two arguments: the radius of the circle (50% of the element's size) and the position of the circle's center (50% from the top and left edges).

You can also create custom shapes using the polygon() function. It lets you define a set of points that form the shape of the clipping path.

Example: Custom shapes with the polygon() function

.custom-shape {
  clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);

This creates a diamond-shaped clipping path by specifying four points: top center, right center, bottom center, and left center.

One of the exciting features of clip-path is the ability to animate the clipping path. By applying transitions or animations to the clip-path property, you can create dynamic and engaging effects.

Example: Animating the clip-path property

.animated-clip {
  clip-path: circle(0%);
  transition: clip-path 0.5s ease-in-out;

.animated-clip:hover {
  clip-path: circle(100%);

In this example, the element starts with a circular clipping path of 0% radius, making it invisible. When the element is hovered over, the clipping path animates to a circle with a 100% radius, revealing the entire element. The transition property ensures a smooth animation between the two states.

Example of animating clip-path

<div class="animated-clip">
  <img src="image.jpg" alt="Animated Clipped Image">
.animated-clip {
  clip-path: polygon(50% 0%, 100% 0%, 100% 100%, 50% 100%);
  transition: clip-path 0.5s ease-in-out;

.animated-clip:hover {
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);

By using clip-path, you can create visually appealing masks, reveal hidden content, and add interactive elements to your web pages. Experiment with different shapes and animations to achieve the desired effects.

Masking with CSS Masks

CSS masks create masking effects by using images, gradients, or SVG elements as masks. Unlike clip-path, which uses shapes to define the clipping region, CSS masks use the transparency or luminance values of an image to determine which parts of an element are visible.

There are two types of masks in CSS:

Mask Type Description
Luminance Masks The brightness values of the mask image determine the opacity of the masked element. White areas make the corresponding areas fully visible, while black areas make them fully transparent. Shades of gray create partial transparency.
Alpha Masks Alpha masks use the transparency values (alpha channel) of the mask image to control the visibility of the masked element. Transparent areas hide the corresponding areas, while opaque areas show them.

To apply a mask to an element, you use the mask-image property. It takes a URL value that points to the image, gradient, or SVG element you want to use as the mask.

Example: Applying mask-image property

.masked-element {
  mask-image: url("mask.png");

You can control various aspects of the mask using additional mask properties:

Property Description
mask-position Specifies the position of the mask image relative to the masked element. It works similarly to the background-position property.
mask-size Determines the size of the mask image. It can be set using length values, percentages, or keywords like cover and contain.
mask-repeat Defines how the mask image is repeated or tiled within the masked element. It accepts values like repeat, no-repeat, repeat-x, and repeat-y.
mask-origin Specifies the origin of the mask image, which can be the border box, padding box, or content box of the masked element.

Example: Controlling mask properties

.masked-element {
  mask-image: url("mask.png");
  mask-position: center;
  mask-size: cover;
  mask-repeat: no-repeat;
  mask-origin: border-box;

By combining these mask properties, you can fine-tune how the mask image is applied to the element and create various masking effects.

CSS masks offer a way to create masking effects using images or gradients. They provide more control over the transparency and blending of the masked elements compared to clip-path. Experiment with different mask images and properties to achieve visual effects in your web designs.

Advanced CSS Masking Techniques

You can combine clip-path and CSS masks to create more complex masking effects. By layering masks and clipping paths, you can make designs that would be hard to create with a single technique.

One way to create complex masks is by using SVG (Scalable Vector Graphics). SVG lets you define shapes and paths that can be used as masks. You can create an SVG element with the shape you want and then reference it as a mask using the mask property.

Example: Defining an SVG mask

<svg width="0" height="0">
    <mask id="complex-mask">
      <path d="M50,0 L100,50 L50,100 L0,50 Z" fill="white" />

<div class="masked-element"></div>
.masked-element {
  mask: url(#complex-mask);

An SVG mask is defined using a <mask> element inside the <defs> section of an SVG. The mask contains a <path> element that defines a custom shape. The mask is then referenced using the mask property on the element you want to mask.

Another technique for creating masks is by using CSS gradients. Gradients can create smooth transitions and blending effects. By using a gradient as a mask, you can control the transparency of different parts of an element.

Example: Using a CSS gradient as a mask

.gradient-masked-element {
  mask-image: linear-gradient(to right, transparent 0%, black 50%, transparent 100%);

A linear gradient is used as the mask image. The gradient starts with transparency on the left, transitions to black in the middle, and then back to transparency on the right. This creates a masking effect where the middle part of the element is fully visible, while the sides fade out.

Text masking is another application of CSS masking. You can use an image or a gradient as a mask to create text effects. To create a text mask, you apply the mask to a container element and then put the text inside that container.

Example: Applying a text mask

<div class="text-mask">
  <h1>Masked Text</h1>
.text-mask {
  background-image: url("mask-image.jpg");
  background-size: cover;
  -webkit-background-clip: text;
  color: transparent;

The text-mask class is applied to a container element. The container has a background image that serves as the mask. The background-size property is set to cover to make the image fill the container. The -webkit-background-clip: text property clips the background to the shape of the text, and the color: transparent property makes the text itself transparent, allowing the background image to show through.

By combining clip-path, CSS masks, SVG, gradients, and text masking techniques, you can create stunning and interactive effects on your web pages. Try different combinations and be creative with your masking designs!