Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS mask-image Property

Posted in CSS Tutorial
Updated on Jul 27, 2024
By Mari Selvan
👁️ 22 - Views
⏳ 4 mins
💬 1 Comment
CSS mask-image Property

Photo Credit to CodeToFun

🙋 Introduction

The mask-image property in CSS allows you to define an image as a mask for an element, which can be used to partially or fully hide parts of the element's content based on the mask's transparency.

This property is particularly useful for creating complex visual effects and shapes without requiring additional images or SVGs. Masks work by controlling the visibility of each pixel of the masked element, where a completely transparent mask pixel makes the corresponding part of the element invisible, and a completely opaque mask pixel shows the element.

💡 Syntax

The syntax for the mask-image property allows for a variety of values, including URLs to images, gradients, and more.

Syntax
Copied
Copy To Clipboard
element {
  mask-image: value;
}

🎛️ Default Value

The default value of the mask-image property is none, meaning no mask is applied.

🏠 Property Values

ValueDescription
noneNo mask is applied, and the element is fully visible.
url()Specifies the URL of the image to use as the mask.
gradientUses a gradient as the mask, such as linear-gradient() or radial-gradient().
image()A function that allows more complex image masks, including image(), element(), and cross-fade().
element()Uses another element as the mask.

📄 Example

In this example, we'll use an image mask to partially hide an image.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS mask-image Example</title>
  <style>
    .masked-image {
      width: 300px;
      height: 300px;
      background-image: url('image.jpg');
      mask-image: url('mask.png');
    }
  </style>
</head>
<body>
  <h1>Image with Mask</h1>
  <div class="masked-image"></div>
</body>
</html>

In this example, image.jpg is the background image of the element, and mask.png is used as the mask image. The parts of mask.png that are transparent will hide the corresponding parts of the background image.

🖥️ Browser Compatibility

The mask-image property is supported in most modern browsers, including Chrome, Firefox, Safari, and Edge. However, it is recommended to check the compatibility of this property with the specific versions of browsers you are targeting, as implementation may vary.

🎉 Conclusion

The mask-image property is a powerful tool for creating visually stunning effects by manipulating the visibility of elements using masks.

Whether you're looking to create custom shapes, hide parts of an image, or apply intricate designs, this property offers a flexible and versatile solution. Experiment with different masks and see how you can enhance your web designs with this technique.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy