CSS Advanced Features

CSS Masking

Master CSS masking to control element visibility and create stunning visual effects. Use masks to reveal, hide, and blend content in creative ways.

How do CSS masks work?

mask-image uses an image or gradient as a transparency map: the opaque parts of the mask show the element and the transparent parts hide it. Gradient masks are a popular way to fade out the edge of a scrolling list or image, for example mask-image: linear-gradient(black 80%, transparent).

.fade-bottom {
  mask-image: linear-gradient(to bottom, black 80%, transparent);
}
.icon {
  background: currentColor;
  mask: url(/icons/star.svg) center / contain no-repeat;
}

Interactive CSS Masking Demo

Code Editor

<img class="fade" src="https://picsum.photos/id/1018/400/220" alt="Mountains">
<div class="icon" title="Star icon made with a mask"></div>

Preview

Understanding CSS Masking

Mask Property Basics

/* Basic mask syntax */ .masked-element {
  mask: linear-gradient(to right, black, transparent);
  -webkit-mask: linear-gradient(to right, black, transparent);
}

/* Mask with image */ .image-mask {
  mask: url('mask.png');
  mask-size: cover;
  mask-repeat: no-repeat;
  mask-position: center;
}

/* Multiple mask layers */ .multi-mask {
  mask:
    radial-gradient(circle, transparent 50px, black 60px),
    linear-gradient(45deg, black 50%, transparent 50%);
  mask-composite: intersect;
}

/* SVG mask */ .svg-mask {
  mask: url('#mask-shape');
}

CSS masks control the visibility of elements using alpha channels from gradients, images, or SVG.

Gradient Masks

/* Linear gradient fade */ .fade-mask {
  mask: linear-gradient(
    to bottom,
    black 0%,
    black 50%,
    transparent 100%
  );
}

/* Radial spotlight */ .spotlight {
  mask: radial-gradient(
    circle at center,
    black 30%,
    transparent 70%
  );
}

/* Conic gradient */ .conic-mask {
  mask: conic-gradient(
    from 0deg,
    black 0deg,
    transparent 180deg,
    black 360deg
  );
}

Create smooth transitions and spotlight effects

Image and SVG Masks

/* PNG mask with transparency */ .png-mask {
  mask: url('alpha-mask.png');
  mask-mode: alpha;
}

/* SVG path mask */ .svg-path-mask {
  mask: url('data:image/svg+xml;...');
  mask-size: contain;
}

/* CSS-generated pattern */ .pattern-mask {
  mask: repeating-linear-gradient(
    45deg,
    black 0px,
    black 10px,
    transparent 10px,
    transparent 20px
  );
}

/* Text as mask */ .text-cutout {
  mask: url('#text-svg');
}

Use images and SVGs for complex masking shapes

Advanced Masking Techniques

Mask Composition and Layering

/* Composite multiple masks */ .composite-mask {
  mask:
    radial-gradient(circle at 25% 25%, transparent 50px, black 60px),
    linear-gradient(45deg, black 50%, transparent 50%),
    radial-gradient(circle at 75% 75%, black 40px, transparent 50px);
  mask-composite: intersect; /* or add, subtract, exclude */
}

/* Mask modes */ .mask-modes {
  mask: url('texture.jpg'), linear-gradient(to right, black, white);
  mask-mode: luminance, alpha;
  mask-composite: multiply;
}

/* Dynamic mask sizing */ .responsive-mask {
  mask: radial-gradient(circle, black 40%, transparent 60%);
  mask-size: clamp(100px, 20vw, 300px) clamp(100px, 20vw, 300px);
  mask-position: var(--mouse-x, 50%) var(--mouse-y, 50%);
}

Combine multiple masks with different composition modes for complex effects.

Animation and Transitions

/* Animated mask reveal */ .reveal-animation {
  mask: linear-gradient(
    90deg,
    transparent 0%,
    transparent var(--progress, 0%),
    black calc(var(--progress, 0%) + 10%),
    black 100%
  );
  transition: mask 0.5s ease;
}

/* Hover effect */ .hover-mask {
  mask: radial-gradient(
    circle at center,
    black 20%,
    transparent 40%
  );
  transition: mask 0.3s ease;
}

.hover-mask:hover {
  mask: radial-gradient(
    circle at center,
    black 80%,
    transparent 100%
  );
}

Create engaging interactive effects with mask animations

Performance Optimization

/* Optimize with will-change */ .animated-mask {
  will-change: mask;
  mask: radial-gradient(...);
}

/* Use transform instead of mask for simple cases */ .clip-alternative {
  clip-path: circle(50%);
  /* Often more performant than mask */
}

/* Contain layout */ .mask-container {
  contain: layout style paint;
}

/* Debounce mask updates */ .interactive-mask {
  mask: var(--cached-mask);
  transition: mask 0.1s ease;
}

Optimize mask performance for smooth animations

Practical Design Applications

Hero Sections and Banners

/* Hero image with text overlay area */ .hero-banner {
  background: url('hero-bg.jpg');
  background-size: cover;
  mask: linear-gradient(
    to bottom,
    black 0%,
    black 40%,
    rgba(0,0,0,0.7) 70%,
    transparent 100%
  );
}

/* Diagonal split mask */ .diagonal-hero {
  mask: linear-gradient(
    135deg,
    black 60%,
    transparent 60%
  );
}

/* Curved bottom mask */ .curved-bottom {
  mask: radial-gradient(
    ellipse 100% 100px at bottom,
    transparent 40%,
    black 50%
  );
}

Create stunning hero sections with gradient masks for text readability.

Image Galleries

/* Gallery item hover reveal */ .gallery-item {
  mask: radial-gradient(
    circle at center,
    black 30%,
    transparent 50%
  );
  transition: mask 0.4s ease;
}

.gallery-item:hover {
  mask: radial-gradient(
    circle at center,
    black 90%,
    transparent 100%
  );
}

/* Mosaic effect */ .mosaic-item {
  mask:
    repeating-conic-gradient(
      from 0deg,
      black 0deg 45deg,
      transparent 45deg 90deg
    );
}

Create interactive gallery effects with mask animations

Text Effects

/* Image-filled text */ .image-text {
  background: url('texture.jpg');
  background-size: cover;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  mask: url('#text-svg');
}

/* Gradient text reveal */ .reveal-text {
  background: linear-gradient(
    90deg,
    #ff6b6b,
    #4ecdc4
  );
  mask: linear-gradient(
    90deg,
    transparent 0%,
    black var(--reveal, 0%),
    black 100%
  );
}

Combine background-clip and mask for text effects

Browser Support & Fallbacks

Best Practices

  • • Always include -webkit-mask for compatibility
  • • Provide fallbacks for unsupported browsers
  • • Test performance on mobile devices
  • • Use will-change for animated masks
  • • Consider clip-path for simple shapes
  • • Optimize mask images for web

Creative Applications

  • • Hero sections with fade effects
  • • Interactive image reveals
  • • Text cutout effects
  • • Loading animations
  • • Artistic image compositions
  • • Brand logo treatments

CSS Masking — step by step

1 / 4
1

mask-image Property

Use mask-image: url() or a gradient to define what parts of an element are visible — white reveals, black hides, with gradients creating fade effects.

1 of 4
🔍White Reveals, Black Hides in Masks
In CSS masking, the luminance of the mask image determines visibility: fully white areas show the element, fully black areas hide it, and gray values create partial transparency.

🎯 Quick Check

Which mask value creates a smooth fade from visible to invisible?

CSS Masking: summary

Key points

  • The mask shorthand works like background, with mask-size, mask-position and mask-repeat.
  • Masking an SVG icon over a currentColor background gives you recolourable icons.
  • All modern browsers support unprefixed mask. Older Safari needs -webkit-mask.

Frequently asked questions

›How do I fade out the edge of an element?

Apply a gradient mask from opaque to transparent: mask-image: linear-gradient(to right, black 85%, transparent);.