Various ways to achieve the hollowing effect of CSS3 mask layer

Various ways to achieve the hollowing effect of CSS3 mask layer

This article introduces 4 methods to achieve mask layer hollowing. Share it with everyone and leave a note for yourself. The details are as follows:

Let’s take a look at the effect first

【Method 1: Screenshot simulation implementation】

Principle: First take a picture of the same position, create a mask layer, and then position the picture at the corresponding position.

Advantages: simple principle; good compatibility, compatible with IE6 and IE7; can hollow out multiple items at the same time.

Disadvantages: This method is only suitable for static pages, not for scrollable pages. It is also not suitable for pages whose content will change.

The code is as follows:

<div class="class1">
    <img src="images/000.jpg" alt=""/>
</div>

.class1{
    position: absolute;
    width:100%;
    height:100%;
    top: 0;
    left: 0;
    background-color: #000;
    opacity: 0.6;
    filter:alpha(opacity=60);
}
.class1 img{
    position: absolute;
    top:260px;
    left: 208px;
}

【Method 2: CSS3 shadow property implementation】

Principle: Use the shadow property of CSS3.

Advantages: easy to implement; suitable for any page and not restricted by the page.

Disadvantages: The compatibility is not very good, only compatible with IE9.

The code is as follows:

<div class="class2"></div>

.class2{
    position: absolute;
    width:170px;
    height:190px;
    top: 260px;
    left: 208px;
    box-shadow: rgba(0,0,0,.6) 0 0 0 100vh;
}

[Method 3: CSS border property implementation]

Principle: Use border properties. First, an empty box is positioned in the target area, and then filled with a border around it.

Advantages: easy to implement, good compatibility, compatible with IE6 and IE7; suitable for any page and not restricted by the page.

Disadvantages: The compatibility implementation process is relatively complicated.

The code is as follows:

<div class="class3"></div>
.class3{
      position: absolute;
      width:170px;
      height:190px;
      top: 0;
      left: 0;
      border-left-width:208px;
      border-left-style: solid;
      border-left-color:rgba(0,0,0,.6);
      border-right-width:970px;
      border-right-style: solid;
      border-right-color:rgba(0,0,0,.6);
      border-top-width:260px;
      border-top-style: solid;
      border-top-color:rgba(0,0,0,.6);
      border-bottom-width:253px;
      border-bottom-style: solid;
      border-bottom-color:rgba(0,0,0,.6);
}

【Method 4: SVG or canvas】

Principle: Use the drawing function of SVG or canvas.

Advantages: Multiple can be hollowed out at the same time.

Disadvantages: poor compatibility and relatively complicated implementation process.

I take SVG as an example, the code is as follows:

<svg style="position: absolute;" width="1366" height="700">
    <defs>
        <mask id="myMask">
            <rect x="0" y="0" width="100%" height="100%" style="stroke:none; fill: #ccc"></rect>
            <rect id="circle1" width="170" height="190" x='208' y="260" style="fill: #000" />
        </mask>
    </defs>
    <rect x="0" y="0" width="100%" height="100%" style="stroke: none; fill: rgba(0, 0, 0, 0.6); mask: url(#myMask)"></rect>
</svg>

This concludes this article on various ways to achieve the CSS3 mask layer hollowing effect. For more relevant CSS3 mask layer hollowing content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  CocosCreator Typescript makes Tetris game

>>:  Summary of common HTML elements including basic structure, document type, header, body, etc.

Recommend

Use jQuery to fix the invalid page anchor point problem under iframe

The application scenario is: the iframe page has n...

A summary of some of the places where I spent time on TypeScript

Record some of the places where you spent time on...

Seven ways to implement array deduplication in JS

Table of contents 1. Using Set()+Array.from() 2. ...

Example of how to enable Brotli compression algorithm for Nginx

Brotli is a new data format that can provide a co...

Vue.js manages the encapsulation of background table components

Table of contents Problem Analysis Why encapsulat...

Measured image HTTP request

Please open the test page in a mainstream browser...

MySQL series: redo log, undo log and binlog detailed explanation

Implementation of transactions The redo log ensur...

Install JDK8 in rpm mode on CentOS7

After CentOS 7 is successfully installed, OpenJDK...

Nginx uses reverse proxy to implement load balancing process analysis

Introduction Based on docker container and docker...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

MySQL data insertion efficiency comparison

When inserting data, I found that I had never con...

CSS Summary Notes: Examples of Transformations, Transitions, and Animations

1. Transition Transition property usage: transiti...