πŸ“ Mastering mask-position in CSS

πŸ“Œ What Is mask-position?

The mask-position property controls where the mask image is placed inside the element’s box β€” similar to how background-position works for backgrounds.
It defines the starting position of the mask relative to the element, allowing you to align masks precisely for creative cutouts, text reveals, sticker shapes, UI highlights, and more. 🎨✨

>>β€œmask-position gives you full directional control of how your mask aligns over the element.”

🧩 Basic Syntax

Basic Syntax

mask-position: <position> | <x-pos> <y-pos>;

mask-position: center;
mask-position: left top;
mask-position: 20px 40px;
mask-position: 50% 50%;

βœ” You can use keywords, percentages, or length values
βœ” Works similarly to background-position

🎯 Accepted Values

Position TypeExamplesDescription
Keywordsleft, right, top, bottom, centerAligns mask to a side or center
Percentages50% 50%, 0% 100%Relative to element size
Lengths20px 40pxExact pixel offset from the top-left corner

πŸ§ͺ Example: Position Mask in the Center

Center mask

.circle-cutout {
  mask-image: url(circle.png);
  mask-position: center;
  mask-repeat: no-repeat;
}

βœ” The mask is perfectly centered over the element.

πŸ§ͺ Example: Offset Mask to Top-Left

Top-left

.shape {
  mask-image: url(shape.png);
  mask-position: left top;
}

βœ” Great when aligning masks with decorative shapes.

πŸ§ͺ Example: Exact Pixel Position

Pixel offset

.icon {
  mask-image: url(star.svg);
  mask-position: 10px 20px;
}

βœ” Useful for precise stencil placement.

πŸ§ͺ Example: Center Horizontally, Bottom Vertically

center bottom

.fade {
  mask-image: linear-gradient(to top, transparent, black);
  mask-position: center bottom;
}

βœ” Perfect for fade-out overlays at top or bottom.

🌈 Using Percentages

Percentage example

.spotlight {
  mask-image: radial-gradient(circle, white 40%, transparent 60%);
  mask-position: 70% 30%;
}

βœ” Moves the spotlight to the upper-right area β€” great for reveal effects.

🎨 Combining mask-position + mask-size

mask-size + mask-position

.pattern {
  mask-image: url(tile.png);
  mask-size: 50px 50px;
  mask-position: 10px 10px;
  mask-repeat: repeat;
}

βœ” Precise control for tiled mask patterns.

πŸ”₯ Example: Animated Mask Movement

Animated mask

@keyframes moveMask {
  to { mask-position: 100% 0%; }
}

.moving {
  mask-image: linear-gradient(90deg, transparent, white, transparent);
  mask-size: 200% 100%;
  animation: moveMask 3s infinite linear;
}

βœ” Creates a smooth scanning or shimmering effect.

🧠 Multiple Masks with Different Positions

Multiple masks

.multi {
  mask-image: url(mask1.png), url(mask2.png);
  mask-position: 0 0, center;
}

βœ” First mask at top-left
βœ” Second mask at center

⚠️ Browser Support

  • βœ” Chrome
  • βœ” Safari
  • βœ” Edge
  • ⚠️ Firefox: use -webkit-mask-position for compatibility

Note

Include both mask-position and -webkit-mask-position for better cross-browser support.

πŸ” Debugging Tips

  • If mask looks clipped β†’ check mask-size
  • If mask is repeating unexpectedly β†’ set mask-repeat: no-repeat
  • If alignment seems off β†’ try changing mask-origin
  • Use DevTools to inspect the mask visual box

πŸ”— Helpful Resources

>>β€œmask-position gives pixel-perfect control over your mask’s placement β€” precision meets creativity.” 🎭✨