A variety of methods to realize the hollowing effect of mask layer

Let's see the effect first


[method 1: screenshot simulation]

Principle: first cut a picture at the same position, create a mask layer, and then locate the picture at the corresponding position. The

Advantages: simple principle; Good compatibility, compatible with IE6 and IE7; Multiple hollows can be realized at the same time. The

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

The codes are as follows:

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

    position: absolute;
    top: 0;
    left: 0;
    background-color: #000;
    opacity: 0.6;
.class1 img{
    position: absolute;
    left: 208px;


[method 2: CSS3 shadow attribute implementation]

Principle: use the shadow attribute of CSS3. The

Advantages: easy to realize; It is suitable for any page and will not be limited by the page.

Disadvantages: the compatibility is not very good. It can only be compatible with IE9. The

The codes are as follows:

<div ></div>

    position: absolute;
    top: 260px;
    left: 208px;
    box-shadow: rgba(0,0,0,.6) 0  0  0  100vh;


[method 3: CSS border attribute implementation]

Principle: use border attributes. First locate an empty box in the target area, and then fill it with a border around it. The

Advantages: easy implementation, good compatibility, and compatibility to IE6 and IE7; It is suitable for any page and will not be limited by the page.

Disadvantages: the process of implementing compatibility is relatively complex. The

The codes are as follows:

<div class="class3"></div>
      position: absolute;
      top: 0;
      left: 0;
      border-left-style: solid;
      border-right-style: solid;
      border-top-style: solid;
      border-bottom-style: solid;


[method 4: SVG or canvas]

Principle: use the drawing function of SVG or canvas. The

Advantages: it can hollow out multiple at the same time. Disadvantages: poor compatibility and relatively complex implementation process. The

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

<svg style="position: absolute;" width="1366" height="700">
        <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" />
    <rect x="0" y="0" width="100%" height="100%" style="stroke: none; fill: rgba(0, 0, 0, 0.6); mask: url(#myMask)"></rect>

Tags: html

Posted by redrabbit on Wed, 01 Jun 2022 21:42:23 +0530