Is a see-through child div possible?

后端 未结 5 2057
遥遥无期
遥遥无期 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

    This seems like a perfect job for pseudo-elements. So this solution is an upgrade of #2 suggestion in the question, but instead of using the element itself, it uses :after:

    #grandparentImage {
      background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/%D0%94%D0%B7%D0%B5%D0%BC%D0%B1%D1%80%D0%BE%D0%BD%D1%8F._%D0%9F%D0%B5%D1%80%D0%B2%D1%8B%D0%B5_%D0%BB%D1%83%D1%87%D0%B8_%D1%81%D0%BE%D0%BB%D0%BD%D1%86%D0%B0.jpg/800px-%D0%94%D0%B7%D0%B5%D0%BC%D0%B1%D1%80%D0%BE%D0%BD%D1%8F._%D0%9F%D0%B5%D1%80%D0%B2%D1%8B%D0%B5_%D0%BB%D1%83%D1%87%D0%B8_%D1%81%D0%BE%D0%BB%D0%BD%D1%86%D0%B0.jpg) no-repeat;
      background-size: cover;
      position: relative;
      height: 500px;
      overflow: hidden;
      z-index: 1;
    }
    
    #childCropper {
      border: 2px dashed #ccc;
      position: absolute;
      top: 50px;
      left: 50px;
      height: 200px;
      width: 200px;
    }
    
    #childCropper:after {
      content: "";
      width: 100%;
      height: 100%;
      border: 1000px solid rgba(0, 0, 0, 0.5);
      position: absolute;
      top: -1000px;
      left: -1000px;
      z-index: -1;
    }

    Note: There will be no need for the #parentOverlay element anymore. Also this solution requires the grand-parent element to have an overflow: hidden property and a z-index (why?).

提交回复
热议问题