search
HomeWeb Front-endCSS TutorialDetailed explanation of CSS mask-image property

This time I will bring you a detailed explanation of the mask-image attribute of CSS. What are the precautions for using the mask-image attribute of CSS? The following is a practical case, let’s take a look. CSS mask The mask attribute has a very long history, even older than CSS3 border-radius and other attributes. It first appeared on the Safari browser and can be traced back to 2009.

But at that time, the mask could only be used as an experimental attribute and used for some special effects. After all, that era was still the era of

IE browser

. Although the properties were good, their value was limited. But now the situation has changed a lot. In addition to IE and Edge browsers not supporting it, Firefox, Chrome and mobile terminals have all supported it, and its actual application value is no longer the same. In particular, the Firefox browser, starting from version 55, has fully supported the CSS3 mask attribute. Moreover, the mask attribute specification has been included in the list of candidate recommended specifications. It will be said that it is a certainty that it will enter the established specification standards in the future. You can rest assured to learn it, and it will be useful in the future.

In the past, the CSS mask attribute was mask: xxx when used, but now with the standardization of this attribute, the mask attribute has actually become many mask-* The abbreviation of , which has the same properties as

background

, border. For specific abbreviations of which attributes, please refer to the following list:

    mask-image
  1. mask-mode
  2. mask-repeat
  3. mask-
  4. position

  5. mask- clip
  6. mask-origin
  7. mask-size
  8. mask-type
  9. mask-composite
  10. Let’s first introduce the usage of the mask-image attribute.

mask-image refers to the

image

resource used for masking. The default value is none, which is a no-mask image. Therefore, similar to the border-style attribute in the border attribute, it is an attribute value that must be set if it is to have an effect. Mask-image mask supports a wide range of image types, which can be url() static image resources, and formats including JPG, PNG and SVG are supported; it can also be dynamically generated images. For example, pictures drawn using various CSS3 gradients. Grammatically, it supports various CSS3 gradients, as well as the url() function, image() function, and even element() function. It also supports multiple backgrounds, so in theory, we can use mask-image to mask any graphics we want, which is very powerful.

Seeing is believing, we use a large number of cases to demonstrate the power of mask-image.

First of all, the original images used in all the following cases are as follows:

Let’s first show the most basic png image mask display.

CSS code is as follows:

.mask-image {
    width: 250px; height: 187.5px;
    -webkit-mask-image: url(loading.png);
    mask-image: url(loading.png);
}

HTML code is as follows:

<img  class="mask-image lazy" src="/static/imghwm/default1.png" data-src="ps1.jpg" alt="Detailed explanation of CSS mask-image property" >

The final effect is as shown below:

From the most basic case above, we can see that the so-called mask means that the original image only displays the non-transparent part of the masked image. For example, in this case, the colored part of the loading ring is the outer ring, so in the end we see that the effect is the original picture, with only the rings exposed one by one. And the semi-transparent areas are also accurately masked and displayed.

Therefore, we rarely use jpg images as mask images, because jpg images must be completely opaque, and the final effect is that nothing can be seen in the original image.

In addition, if loading.png fails to load, the original image will not be displayed directly in Firefox and Chrome browsers.

Then let’s look at an SVG graphic masking effect display.

css code is as follows:

.mask-image {
    width: 250px; height: 187.5px;
    -webkit-mask-image: url(star.svg);
    mask-image: url(star.svg);
}

html code is as follows:

<img  class="mask-image lazy" src="/static/imghwm/default1.png" data-src="ps1.jpg" alt="Detailed explanation of CSS mask-image property" >

最终的效果和上图相似。

上面是将svg作为背景图来实现的,现在我们再使用SVG图形中元素作为遮罩元素来实现它。

CSS 代码如下:

.mask-image {
    width: 250px; height: 187.5px;
    -webkit-mask-image: url(#mask);
    mask-image: url(#mask);
    /* Firefox外链也支持 */
    /* mask-image: url(ellipse-rect.svg#mask); */
}

html代码如下:


       
        
            
            
            
            
            
            
        
       

<img  class="mask-image lazy" src="/static/imghwm/default1.png" data-src="ps1.jpg" alt="Detailed explanation of CSS mask-image property" >

再使用SVG元素内联SVG的,看下面的实现。

CSS 代码:

.mask-image {
    width: 250px; height: 187.5px;
    -webkit-mask-image: url(#mask);
    mask: url(#mask);
    mask-image: url(#mask);
    /* Firefox外链也支持 */
    /* mask-image: url(ellipse-rect.svg#mask); */
}

SVG实现的代码:

<svg>
    <defs>   
        <mask>
            <!-- 柔化边缘 www.xttblog.com-->
            <ellipse></ellipse>
            <rect></rect>
            <!-- 主体遮罩 -->
            <ellipse></ellipse>
            <rect></rect>
        </mask>
    </defs>   
</svg>
<svg>
    <image></image>
</svg>

无论是clip-path还是这里的mask,外链SVG特性的支持一定是比内联SVG弱的。既然Chrome浏览器连普通HTML的内联SVG的都不支持,自然肯定不支持这里的外链SVG文件元素的遮罩支持。

那之前表现良好的Firefox浏览器呢?

比较幸运,Firefox浏览器最近支持了任意元素外链SVG文件的,为什么说最近呢?我看了下我现在的Firefox,显示最新版,版本是56,然后Firefox支持任意元素可以使用外链SVG 元素作为遮罩是版本55开始了。

至于上面的,任意元素内联SVG 的支持,Firefox很早就已经支持。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

hover选择器如何使用

css3的新单位使用详解

CSS做出图片背景填充的六边形

The above is the detailed content of Detailed explanation of CSS mask-image property. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Anchor Positioning Just Don't Care About Source OrderAnchor Positioning Just Don't Care About Source OrderApr 29, 2025 am 09:37 AM

The fact that anchor positioning eschews HTML source order is so CSS-y because it's another separation of concerns between content and presentation.

What does margin: 40px 100px 120px 80px signify?What does margin: 40px 100px 120px 80px signify?Apr 28, 2025 pm 05:31 PM

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

What are the different CSS border properties?What are the different CSS border properties?Apr 28, 2025 pm 05:30 PM

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

What are CSS backgrounds, list the properties?What are CSS backgrounds, list the properties?Apr 28, 2025 pm 05:29 PM

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

What are CSS HSL Colors?What are CSS HSL Colors?Apr 28, 2025 pm 05:28 PM

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

How can we add comments in CSS?How can we add comments in CSS?Apr 28, 2025 pm 05:27 PM

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

What are CSS Selectors?What are CSS Selectors?Apr 28, 2025 pm 05:26 PM

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

Which type of CSS holds the highest priority?Which type of CSS holds the highest priority?Apr 28, 2025 pm 05:25 PM

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function