【问题标题】:How to use filter:blur with clip-path?如何使用过滤器:模糊与剪辑路径?
【发布时间】:2017-06-26 18:37:58
【问题描述】:

我正在尝试为clip-path 六边形添加阴影。 由于通常的box-shadow(和filter:drop-shadow())不适用于clip-path,我试图在下面使用更大的伪元素来伪造效果。 该方法取自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>

但是,对剪辑路径六边形使用完全相同的方法会失败。 这个粗略的草图显示了预期的效果:

相反,我得到:

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

两个问题:

  1. 我怎样才能完成这项工作?
  2. 为剪辑路径元素伪造阴影的更好方法是什么?

【问题讨论】:

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


    【解决方案1】:

    你需要相反的布局。

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

    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);
    }
    <div class="hexagon">
    </div>

    【讨论】:

    • 感谢(再次)@vals 的帮助。看来我表达得不够好。我在问题中添加了一个草图以阐明所需的效果(六边形后面的阴影)。明白我的意思了吗?
    • 好吧,我刚刚复制了问题中的过滤器。但是,如果您需要阴影,只需设置它。我已经编辑了我的答案
    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2019-09-25
    • 1970-01-01
    相关资源
    最近更新 更多