Back to CSS Notes
Topic #255

Masking with Gradients

You will use a CSS gradient as a mask to fade an element’s edges smoothly instead of hiding it with a solid overlay.

What it is

A CSS mask is a stencil that controls which parts of an element are visible. When the stencil is a gradient, the gradient’s alpha values create a smooth fade: fully opaque areas show the element, fully transparent areas hide it. In an alpha mask, the color itself is less important than its transparency, so black is often used as a convenient opaque value and transparent as the hidden value. Related terms include mask-image, mask-size, mask-repeat, and mask-mode.

Why it matters

  • Fade images or cards into a page background without adding a colored overlay.
  • Create soft vignettes and edge reveals that look more natural than hard cuts.
  • Reveal text or UI elements gradually by animating mask-position or gradient stops.
  • Keep markup simple by applying the effect directly to the element.

Syntax or steps

The smallest useful pattern is a linear gradient assigned to mask-image:

.fade {
  -webkit-mask-image: linear-gradient(to bottom, black 60%, transparent);
  mask-image: linear-gradient(to bottom, black 60%, transparent);
}

Choose a direction, place an opaque color where content should remain visible, and place transparent where it should fade out. Add mask-size: 100% 100% and mask-repeat: no-repeat if the gradient does not cover the element as expected.

Example

<div class="fade">
  <h2>Mountain Lake</h2>
  <p>A calm alpine scene.</p>
</div>

.fade {
  width: min(420px, 100%);
  height: 260px;
  padding: 1rem;
  background: #1e3a8a;
  color: #fff;
  -webkit-mask-image: linear-gradient(to bottom, black 55%, transparent);
  mask-image: linear-gradient(to bottom, black 55%, transparent);
  mask-size: 100% 100%;
  mask-repeat: no-repeat;
}

The panel fades toward the bottom. The stop black 55% keeps the upper part fully visible, while transparent hides the lower edge. mask-size makes the gradient cover the whole panel, and mask-repeat prevents tiling.

Common mistakes

  • Applying the mask to a parent and forgetting that it can hide child elements too.
  • Using the mask shorthand and losing other mask settings; if that happens, set mask-size and mask-repeat explicitly.
  • Expecting luminance behavior. With the default alpha mask, transparency controls visibility; with mask-mode: luminance, brightness controls visibility.
  • Choosing the wrong gradient direction, which fades the opposite edge.

When to use it

TechniqueEffectUse when
mask-image with gradientSoft, pixel-level fadeYou need smooth edges or animated reveals
opacityUniform transparencyThe whole element should fade equally
clip-pathHard shape cutYou need a crisp boundary, not a fade
Gradient overlayColor tint over contentYou want to darken or lighten, not hide pixels

Practice

Guided exercise: Fade the right edge of an image using linear-gradient(to right, black 70%, transparent). Expected result: the left side stays visible and the right side fades out.

Challenge: Create a soft vignette that fades all four edges. Hint: use radial-gradient(circle at center, black 60%, transparent) as the mask image.

Quick check

Question: In a default CSS alpha mask, what does transparent do?

Answer: It sets the mask alpha to zero, hiding that part of the element.

Summary

Gradient masks turn transparency into a reusable visual tool: opaque areas show content, transparent areas hide it. They are ideal for soft fades, edge reveals, and vignettes when uniform opacity or hard clipping is not enough.

Want to go beyond the notes?

Join Coding Now Tech Institute's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Masking with Gradients – FAQs

Quick answers about learning Masking with Gradients in CSS.

This free note from Coding Now Tech Institute explains Masking with Gradients in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on Coding Now Tech Institute, including Masking with Gradients, is 100% free with no signup required.
With focused practice, most students grasp Masking with Gradients in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now