【问题标题】:How to have a shadow inside a CSS mask?如何在 CSS 蒙版内有阴影?
【发布时间】:2021-01-23 21:29:19
【问题描述】:

我可以在由 SVG 蒙版制作的切口内添加阴影吗?

这是我正在使用的代码

index.html

<div class="content">
    <section id="home-section">
    </section>
    ...
</div>

styles.css

#home-section {
    background: url(img/bg.png) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    -webkit-mask: url(img/mask.svg) center/contain no-repeat;
    -webkit-mask-size: auto;
}

这是我得到的结果

但这是我想要的结果(我已经在 Illustrator 中制作了):

这是 SVG:https://drive.google.com/file/d/1O7nGJHeYkgw9Al9BojSVI7f8zKzv4fbu/view?usp=sharing

【问题讨论】:

  • 分享您正在使用的 SVG,以便我们给您准确的答案
  • 在您的问题中将 svg 共享为代码。如果您知道如何清洁它。

标签: html css svg shadow css-mask


【解决方案1】:

drop-shadow 过滤器可以在这里为您提供帮助,如果您考虑如下屏蔽:

.box {
    height:300px;
    background:url(https://picsum.photos/id/1072/800/600) center/cover;
   overflow:hidden;
}

.box div {
  filter:drop-shadow(0 0 5px black);
  height:100%;
}

.box div::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:lightblue;
  -webkit-mask:
   /* your mask here */
      url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 13 13"><text  x="0" y="12" font-family="Arial, Helvetica, sans-serif" >A</text></svg>') left/33% auto,
      url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 13 13"><text  x="0" y="12" font-family="Arial, Helvetica, sans-serif" >B</text></svg>') center/33% auto,
     linear-gradient(#fff 0 0) right 10% top 50%/30% 80%,
    /* end of your mask*/
    linear-gradient(#fff 0 0);
   -webkit-mask-composite:destination-out;
   mask-composite:exclude;
   -webkit-mask-repeat:no-repeat;
}
<div class="box">
  <div></div>
</div>

使用您的掩码,代码将是:

.box {
    height:300px;
    background:url(https://picsum.photos/id/1072/800/600) center/cover;
   overflow:hidden;
}

.box div {
  filter:drop-shadow(0 0 5px black);
  height:100%;
}

.box div::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:lightblue;
  -webkit-mask:
    url('https://i.ibb.co/JspDMsM/thebirds-mask3.png') center/contain no-repeat,
    linear-gradient(#fff 0 0);
   -webkit-mask-composite:destination-out;
   mask-composite:exclude;
   -webkit-mask-repeat:no-repeat;
}
<div class="box">
  <div></div>
</div>

【讨论】:

  • 你为什么使用线性渐变和“mask-composite: exclude”?最后一个对我没有影响
  • @AsaphSouzaDiniz 它取决于掩码。我使用 exclude 在最后一层(线性渐变层)内创建孔。如果您的蒙版默认情况下已经创建了孔,那么您不需要它。由于我无法访问您的 SVG,因此我无法知道此类信息;)
  • @AsaphSouzaDiniz 我已经用你的面具更新了代码
猜你喜欢
  • 2018-05-19
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
相关资源
最近更新 更多