背景图片中CSS中的负遮罩

时间:2020-03-17 21:24:53

标签: html css css-mask

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

example

我这里有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

1 个答案:

答案 0 :(得分:2)

新答案

基于更新,您可以执行以下操作。想法是考虑徽标的倒置版本,在该版本中,使透明部分不透明(并使非透明部分透明),然后应用多个蒙版层以获得所需的内容。

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

.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;
}
<div class="overlay"></div>

以下是一个相关问题,用于说明如何获取徽标的新版本:https://graphicdesign.stackexchange.com/q/63635

我们也可以使用mask-composite并保留原始徽标,这样可以更轻松地调整和更改位置。注意掩模层的顺序,这与第一个示例不同,在这里很重要:

.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;
}
<div class="overlay"></div>


旧答案

我将使用纯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;
}
<div class="overlay"></div>

要理解这个难题,这里是用于具有不同颜色的蒙版的渐变:

.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;
}
<div class="overlay"></div>


这是我将使用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;
}
<div class="overlay"></div>