Is a see-through child div possible?

后端 未结 5 2056
遥遥无期
遥遥无期 2020-12-03 21:45

The image is the grandparent div, the black translucent overlay is the parent div, and the cropped section is the child div. User will see the grandparent image and

5条回答
  •  悲&欢浪女
    2020-12-03 22:14

    I'm guessing this is what you're looking for:

    overlay-mask {
      background-color: rgba(0,0,0,.65);
      clip-path: polygon(0% 0%, 75% 0%, 75% 25%, 25% 25%, 25% 75%, 75% 75%, 75% 0%, 100% 0%, 100% 100%, 0 100%);
      z-index: 1;
      pointer-events: none;
      /* rest is optional, you could use 
       * `position:absolute` to place it in a parent with `relative` 
       */
      position: fixed;
      top: 0; bottom: 0; left: 0; right: 0;
    }
    
    body {
      margin: 0;
      background: url("https://loremflickr.com/800/600") no-repeat center center /cover;
      min-height: 100vh;
    }

    It's a simple shape following the polygon of the dark area. Points position can be expressed in percentage, using calc() or even providing a custom by id (and use an external tool, like Adobe Illustrator to generate it.

    Current browser coverage: 87.99%.

    You can have any content under the mask. And, instead of using position:fixed, you could use position:absolute and place it in the desired container with position:relative, to apply to that container.


    Another method is to use s . Animating them is pretty straight forward using either smil animations or plain CSS keyframes.

    Example:

    #overlay-mask {
      z-index: 1;
      pointer-events: none;
      /* rest is optional, you could use 
       * `position:absolute` to place it in a parent with `relative` 
       */
      position: fixed;
      top: 0; bottom: 0; left: 0; right: 0;
      color: rgba(0,0,0,.65);
      width: calc(100% + 4px);
      height: calc(100% + 4px);
      left: -2px;
      top: -2px;
    }
    
    body {
      margin: 0;
      background: url("https://loremflickr.com/800/600") no-repeat center center /cover;
      min-height: 200vh;
    }
    h2 {color: white;}
    
    
    
    
        
            
            
            
        
        
        
    
    
    

    Scroll down...

提交回复
热议问题