【问题标题】:Negative mask in CSS within the background image背景图像中 CSS 中的负蒙版
【发布时间】:2020-03-17 21:24:53
【问题描述】:

我想使用 png 文件在 css 中创建一个蒙版,但方式不同。基本上我不希望掩码显示下面的内容,但我希望它剪切下面的内容并显示其他所有内容。所以就像负面面具。重要的事实是我想掩盖背景图像。这是我想要做的::

我这里有 3 层。第一个是视频 (html),然后是作为重复背景 (CSS 背景) 的虚线背景,然后是蒙版 - 需要是 png 图像,因为这将是徽标。我希望蒙版移除其下方的背景并显示视频。

.cont {
  width: 100%;
  height: 450px;
  position: relative;
  background: url("http://www.kamiennadlyna.pl/test/img/video-bg.jpg") no-repeat center;
}

.maska {
  width: 100%;
  height: 100%;
  background: url("http://www.kamiennadlyna.pl/test/img/mask.png") repeat;
  left: 0;
  bottom: 0;
  position: absolute;
  text-align: center;
  /*-webkit-mask-image: url("https://www.tucado.com/images/logo.png")*/
}
<div class="cont">

  <video autoplay muted loop id="myVideo">
          <source src="http://www.kamiennadlyna.pl/video.mp4" type="video/mp4" poster="http://www.kamiennadlyna.pl/test/img/video-bg.jpg">
        </video>

  <div class="maska">
  </div>

</div>

jsfiddle

【问题讨论】:

    标签: html css css-mask


    【解决方案1】:

    新答案

    根据更新,您可以执行以下操作。我们的想法是考虑你的标志的倒置版本,你使透明部分不透明(和不透明部分透明),然后应用多个遮罩层来获得你想要的。

    我从旧答案中保留了相同的想法。我正在考虑叠加层中心的徽标:

    .overlay {
      --h:200px; /* height of the logo*/
      --w:200px; /* width of the logo */
    
      height:300px;
      background:radial-gradient(farthest-side,black 50%,transparent 52%) 0 0/8px 8px;
     
      -webkit-mask:
          linear-gradient(#fff,#fff) top   /100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) bottom/100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) left  /calc(50.1% - var(--w)/2) 100%,
          linear-gradient(#fff,#fff) right /calc(50.1% - var(--w)/2) 100%,
          url(https://i.ibb.co/1zDbtJw/logo.png) center/var(--w) var(--h);
      mask:
          linear-gradient(#fff,#fff) top   /100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) bottom/100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) left  /calc(50% - var(--w)/2) 100%,
          linear-gradient(#fff,#fff) right /calc(50% - var(--w)/2) 100%,
          url(https://i.ibb.co/1zDbtJw/logo.png) center/var(--w) var(--h);
      -webkit-mask-repeat:no-repeat;
      mask-repeat:no-repeat;
    }
    
    body {
      background:url(https://i.picsum.photos/id/44/800/800.jpg) center/cover;
    }
    &lt;div class="overlay"&gt;&lt;/div&gt;

    这里有一个相关的问题来解释如何获得新版本的标志:https://graphicdesign.stackexchange.com/q/63635

    我们也可以和mask-composite一起玩,保留原来的logo,调整和改变位置会更容易。注意遮罩层的顺序,这与第一个示例不同:

    .overlay {
    
      height:300px;
      background:radial-gradient(farthest-side,black 50%,transparent 52%) 0 0/8px 8px;
    
      -webkit-mask:
          linear-gradient(#fff,#fff),
          url(https://i.ibb.co/cKBT5WQ/logo.png) center/200px 200px;
      -webkit-mask-repeat:no-repeat;
      -webkit-mask-composite:source-out;
      mask:
          linear-gradient(#fff,#fff),
          url(https://i.ibb.co/cKBT5WQ/logo.png) center/200px 200px;
      mask-repeat:no-repeat;
      mask-composite:exclude;
    }
    
    body {
      background:url(https://i.picsum.photos/id/44/800/800.jpg) center/cover;
    }
    &lt;div class="overlay"&gt;&lt;/div&gt;


    旧答案

    我会用纯 CSS 构建它而不需要图像:

    .overlay {
      height:300px;
      /* the stripes */
      background:repeating-linear-gradient(to right,blue 0 10px,transparent 10px 20px);
      /* the mask*/
      -webkit-mask:
          linear-gradient(#fff,#fff) top   /100% 50px,
          linear-gradient(#fff,#fff) bottom/100% 50px,
          linear-gradient( 235deg,transparent 10%,#fff 9%) calc(50% - 600px) 50%/1200px calc(100% - 100px),
          linear-gradient(-235deg,transparent 10%,#fff 9%) calc(50% + 600px) 50%/1200px calc(100% - 100px);
      -webkit-mask-repeat:no-repeat;
      mask:
          linear-gradient(#fff,#fff) top   /100% 50px,
          linear-gradient(#fff,#fff) bottom/100% 50px,
          linear-gradient( 235deg,transparent 10%,#fff 9%) calc(50% - 600px) 50%/1200px calc(100% - 100px),
          linear-gradient(-235deg,transparent 10%,#fff 9%) calc(50% + 600px) 50%/1200px calc(100% - 100px);
      mask-repeat:no-repeat;
    }
    
    body {
      background:url(https://i.picsum.photos/id/44/800/800.jpg) center/cover;
    }
    &lt;div class="overlay"&gt;&lt;/div&gt;

    要理解这里的谜题是用于不同颜色的蒙版的渐变:

    .overlay {
      height:300px;
      background:
          linear-gradient(blue,blue) top   /100% 50px,
          linear-gradient(red,red)   bottom/100% 50px,
          linear-gradient( 235deg,transparent 10%,green  9%) 
             calc(50% - 600px)   50%  /    1200px calc(100% - 100px),
             /*          ^ this half of this ^ */
          linear-gradient(-235deg,transparent 10%,purple 9%) 
             calc(50% + 600px) 50%/1200px calc(100% - 100px);
      background-repeat:no-repeat;
    }
    
    body {
      background:url(https://i.picsum.photos/id/44/800/800.jpg) center/cover;
    }
    &lt;div class="overlay"&gt;&lt;/div&gt;

    这是我将使用 CSS 变量轻松控制三角形的另一种语法:

    .overlay {
      --h:200px; /* height of the triangle*/
      --w:200px; /* width of the triangle */
    
      height:300px;
      /* the stripes */
      background:repeating-linear-gradient(to right,blue 0 10px,transparent 10px 20px);
      /* the mask*/
      -webkit-mask:
          linear-gradient(#fff,#fff) top   /100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) bottom/100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) left  /calc(50% - var(--w)/2) 100%,
          linear-gradient(#fff,#fff) right /calc(50% - var(--w)/2) 100%,
          linear-gradient(to top right,#fff 49%,transparent 50%) calc(50% - var(--w)/4) 50%/calc(var(--w)/2) var(--h),
          linear-gradient(to top left ,#fff 49%,transparent 50%) calc(50% + var(--w)/4) 50%/calc(var(--w)/2) var(--h);
      -webkit-mask-repeat:no-repeat;
      mask:
          linear-gradient(#fff,#fff) top   /100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) bottom/100% calc(50% - var(--h)/2),
          linear-gradient(#fff,#fff) left  /calc(50% - var(--w)/2) 100%,
          linear-gradient(#fff,#fff) right /calc(50% - var(--w)/2) 100%,
          linear-gradient(to top right,#fff 49%,transparent 50%) calc(50% - var(--w)/4) 50%/calc(var(--w)/2) var(--h),
          linear-gradient(to top left ,#fff 49%,transparent 50%) calc(50% + var(--w)/4) 50%/calc(var(--w)/2) var(--h);
      mask-repeat:no-repeat;
    }
    
    body {
      background:url(https://i.picsum.photos/id/44/800/800.jpg) center/cover;
    }
    &lt;div class="overlay"&gt;&lt;/div&gt;

    【讨论】:

    • 谢谢 Temani,我错误地将其解释为条纹背景。我有不同的背景要切断,但对于我使用条纹的例子。有没有办法使用图像来做到这一点?我会更新我的问题以便更好地理解。
    • @PiotrCiszewski 还是一样,只需将重复梯度替换为图像,蒙版也会这样做
    • 谢谢。很抱歉你在这方面做了这么多工作。三角形也是一个例子。这个想法是放置将切断背景的标志。我再次更新了这个问题。这就是为什么它需要是png图像。我明白你的解决方案了。
    • @PiotrCiszewski 现在人们在查看我的答案和您的 问题时会认为我疯了 :) ... 将根据您的要求进行编辑
    • @PiotrCiszewski 进行了另一次更新以添加第二种方法
    猜你喜欢
    • 2018-08-05
    • 2019-03-10
    • 2018-11-09
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多