![CSS Selectors](https://codetofun.com/wp-content/themes/codetofun/images/large/css-selectors.webp)
CSS Properties
CSS mask-image Property
![CSS mask-image Property](https://codetofun.com/wp-content/themes/codetofun/images/large/css-mask-image.webp)
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.
element {
mask-image: value;
}
🎛️ Default Value
The default value of the mask-image
property is none, meaning no mask is applied.
🏠 Property Values
Value | Description |
---|---|
none | No mask is applied, and the element is fully visible. |
url() | Specifies the URL of the image to use as the mask. |
gradient | Uses 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.
<!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:
Author
![author](https://codetofun.com/wp-content/themes/codetofun/images/mari-selvan-400.webp)
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
If you have any doubts regarding this article (CSS mask-image Property), please comment here. I will help you immediately.