CSS为有角度的div添加阴影

问题描述 投票:3回答:3

我有以下代码:

.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的下边缘添加一个插入阴影。我怎样才能做到这一点?

html css shadow
3个回答
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>

2
投票

你可以使用filter: drop-shadow。请参阅文档:https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/drop-shadow

.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>

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>
© www.soinside.com 2019 - 2024. All rights reserved.