如何使用滤镜:使用剪辑路径模糊?

时间:2017-06-26 18:37:59

标签: css3 css-transforms shadow clip-path css-filters

我试图为clip-path六角形添加阴影。 由于通常box-shadow(和filter:drop-shadow())不能使用剪辑路径,因此我尝试使用较大的伪元素伪造效果。 该方法取自here,并在一个更简单的例子中正常工作:

enter image description here



body {
  background-color: gray;
}

.rectangle {
  margin: 10%;
  position: absolute;
  background: white;
  width: 80%;
  padding-top: 25%;
}

.rectangle::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  filter: blur(10px) brightness(20%);
  transform: scale(1.1);
  z-index: -1;
  background-color: black;
}

<div class="rectangle">
</div>
&#13;
&#13;
&#13;

然而,使用与剪辑路径六边形完全相同的方法失败。 这幅粗略的草图显示了预期的效果:

enter image description here

相反,我得到:

enter image description here

&#13;
&#13;
body {
  background-color: gray;
}

.hexagon {
  width: 20%;
  padding-top: 25%;
  -webkit-clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  -webkit-shape-outside: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  position: absolute;
  background: rgb(0, 229, 154);
  margin: 10%;
}

.hexagon::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  -webkit-filter: blur(5px);
  -moz-filter: blur(5px);
  -o-filter: blur(5px);
  -ms-filter: blur(5px);
  filter: blur(10px) brightness(20%);
  transform: scale(2.5);
  z-index: -1;
  background-color: black;
}
&#13;
<div class="hexagon">
</div>
&#13;
&#13;
&#13;

两个问题:

  1. 我怎样才能做到这一点?
  2. 为剪辑路径元素伪造阴影的更好方法是什么?

1 个答案:

答案 0 :(得分:4)

你需要相反的布局。

容器(在本例中为基本元素)必须应用过滤器,内部部分(在本例中为伪)必须具有剪辑属性:

&#13;
&#13;
body {
  background-color: gray;
}

.hexagon {
  width: 20%;
  padding-top: 25%;
  filter: drop-shadow(10px 10px 10px red);
  position: absolute;
  margin: 10%;
}

.hexagon::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform: scale(2.5);
  z-index: -1;
    -webkit-clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  -webkit-shape-outside: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
  background: rgb(0, 229, 154);
}
&#13;
<div class="hexagon">
</div>
&#13;
&#13;
&#13;