CSS为斜角div添加阴影

时间:2019-02-22 21:46:06

标签: html css shadow

我有以下代码:

.angled{
background-color: red;
height: 120px;
width: 100%;
  -webkit-clip-path: polygon(0 0, 1600px 0, 1600px 53%, 0 100%);
  clip-path: polygon(0 0, 1600px 0, 1600px 53%, 0 100%);
}
<div class="angled"></div>

我想在div的下边缘添加一个嵌入式阴影。我该如何实现?

3 个答案:

答案 0 :(得分:3)

您可以将倾斜的元素视为背景,在其中可以轻松应用插入阴影:

.angeled {
  position:relative;
  height: 120px;
  overflow:hidden;
  z-index:0;
}
.angeled::before {
  content:"";
  position:absolute;
  z-index:-1;
  top:-20px;
  left:-20px;
  right:-20px;
  bottom:0;
  transform-origin:left;
  transform:skewY(-3deg);
  background:red;
  box-shadow:0 -2px 20px green inset
}
<div class="angeled"></div>

您还可以使用渐变来创建它:

.angeled {
  position:relative;
  height: 120px;
  background:
    linear-gradient(red,red) top/100% 50%,
    linear-gradient(to bottom right,
      red calc(50% - 15px),green calc(50% - 1px),
      transparent 50%) bottom left/150% 50%;
  background-repeat:no-repeat;
}
<div class="angeled"></div>

答案 1 :(得分:2)

您可以使用filter: drop-shadow。查看文档:{​​{3}}

.angeled{
background-color: red;
height: 120px;
width: 100%;
  -webkit-clip-path: polygon(0 0, 1600px 0, 1600px 53%, 0 100%);
  clip-path: polygon(0 0, 1600px 0, 1600px 53%, 0 100%);
 
}

.outer{
   filter: drop-shadow(10px 10px 10px rgba(0,0,0,0.5));
}
<div class="outer">
<div class="angeled">
</div>
</div>

答案 2 :(得分:0)

如果您不打算使用多边形:)

div.wrapper {
  overflow: hidden;
  height: 150px;
}

div.poly {
  width: 100%;
  height: 100px;
  background-color: #F00;
  box-shadow: 0px 5px 15px rgba(0,0,0,.6);
  transform: rotate(-2deg) scale(1.2);
  transform-origin: 30px 0px;
}
<div class='wrapper'>
  <div class='poly'>
  </div>
</div>