如何为形状正确添加阴影和渐变?

时间:2019-02-06 09:24:06

标签: html css css3 css-shapes linear-gradients

我要进行以下设计:

Sample image

我尝试使用:after:before,但是它不起作用。这是我当前的代码:

.design {
  background: #ea053a;
  display: inline-block;
  height: 155px;
  margin-left: 33px;
  margin-right: 40px;
  position: relative;
  width: 228px;
}

.design:before {
  border-top: 43px solid #ea053a;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  margin-right: 40px;
  content: "";
  height: 0;
  left: 0;
  position: absolute;
  top: 55px;
  margin-top: 100px;
  width: 128px;
}
<div class="design"></div>

fiddle

我如何使其与原始设计相同并具有以下两个属性?

box-shadow: 0 1px 10px 0 rgba(0, 0, 0, 0.2);
background-image: linear-gradient(to bottom, #ea053a, #d0021b);

5 个答案:

答案 0 :(得分:11)

这是一个带有偏斜变换和drop-shadow过滤器的想法。您只需要一些额外的元素即可正确获得渐变。技巧是反转偏斜以保持正确的渐变方向(如果我们处理纯色则不需要)

.box {
  width: 150px;
  height: 150px;
  position: relative;
  z-index:0;
  overflow: hidden;
  filter: drop-shadow(0 1px 10px rgba(0, 0, 0, 0.8));
}

.box span {
  position: absolute;
  z-index:-1;
  top: 0;
  width: 50%;
  height: 100%;
  overflow: hidden;
}

.box span:first-of-type {
  left: 0;
  transform: skewY(35deg);
  transform-origin: top right;
}

.box span:last-of-type {
  right: 0;
  transform: skewY(-35deg);
  transform-origin: top left;
}

.box span::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, blue , red );
}

.box span:first-of-type::before {
  transform: skewY(-35deg);
  transform-origin: top right;
}

.box span:last-of-type::before {
  transform: skewY(35deg);
  transform-origin: top left;
}

p {
 margin:0;
 color:#fff;
 font-size:45px;
 line-height:100px;
 text-align:center;
}
<div class="box">
  <span></span><span></span>
  <p>29</p>
</div>

这是我们如何处理左或右渐变。在这种情况下,我们不需要额外的元素,因为偏斜不会影响方向:

.box {
  width: 150px;
  height: 150px;
  position: relative;
  z-index:0;
  overflow: hidden;
  filter: drop-shadow(0 1px 10px rgba(0, 0, 0, 0.8));
}

.box:before,
.box:after{
  content:"";
  position: absolute;
  z-index:-1;
  top: 0;
  width: 50%;
  height: 100%;
  overflow: hidden;
  background:linear-gradient(to right,blue,red);
  background-size:200% 100%;
}

.box:before{
  left: 0;
  transform: skewY(35deg);
  transform-origin: top right;
}

.box:after{
  right: 0;
  transform: skewY(-35deg);
  transform-origin: top left;
  background-position:right;
}

p {
 margin:0;
 color:#fff;
 font-size:45px;
 line-height:100px;
 text-align:center;
}
<div class="box">
  <p>29</p>
</div>

这是任意渐变:

.box {
  width: 150px;
  height: 150px;
  position: relative;
  z-index:0;
  overflow: hidden;
  filter: drop-shadow(0 1px 10px rgba(0, 0, 0, 0.8));
}

.box span {
  position: absolute;
  z-index:-1;
  top: 0;
  width: 50%;
  height: 100%;
  overflow: hidden;
}

.box span:first-of-type {
  left: 0;
  transform: skewY(35deg);
  transform-origin: top right;
}

.box span:last-of-type {
  right: 0;
  transform: skewY(-35deg);
  transform-origin: top left;
}

.box span::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(35deg, blue , red );
  background-size:200% 100%;
}

.box span:first-of-type::before {
  transform: skewY(-35deg);
  transform-origin: top right;
}

.box span:last-of-type::before {
  transform: skewY(35deg);
  transform-origin: top left;
  background-position:right;
}

p {
 margin:0;
 color:#fff;
 font-size:45px;
 line-height:100px;
 text-align:center;
}
<div class="box">
  <span></span><span></span>
  <p>29</p>
</div>

由于每个元素都占宽度的50%,因此我们将背景设为200%,以使其大小作为主要容器,然后我们调整位置以创建一个背景的错觉。就像每个元素将显示主要背景的一半一样。

答案 1 :(得分:3)

您可以像我一样使用剪切路径。这是我的解决方法。

.design {
  background: #ea053a;
  -webkit-clip-path: polygon(50% 0%, 100% 0, 100% 75%, 50% 100%, 0% 75%, 0 0);
  clip-path: polygon(50% 0%, 100% 0, 100% 75%, 50% 100%, 0% 75%, 0 0);
  height: 155px;
  width: 155px;
}

.month {
  text-align:center;
  padding: 1rem 0 .25rem 0;
  color:#fff;
  font-weight:bold;
  font-size: 18px;
}

.day {
  text-align: center;
  font-size: 60px;
  font-weight:bold;
  color: #fff;
}
<div class="design">
  <div class="month">Diciembre</div>
  <div class="day">29</div>
</div>

答案 2 :(得分:0)

如果将CSS更改为以下较小更改,则可以实现预期的结果:

.design {
  background: #ea053a;
  display: inline-block;
  height: 100px;
  margin-left: 33px;
  margin-right: 40px;
  position: relative;
  width: 180px;
}

.design:before {
  border-top: 43px solid #ea053a;
  border-left: 90px solid transparent;
  border-right: 90px solid transparent;
  margin-right: 40px;
  content: "";
  height: 0;
  left: 0;
  position: absolute;
  top: 0px;
  margin-top: 100px;
  width: 0;
}

以下是上述CSS的工作:

.design {
  background: #ea053a;
  display: inline-block;
  height: 100px;
  margin-left: 33px;
  margin-right: 40px;
  position: relative;
  width: 180px;
}

.design:before {
  border-top: 43px solid #ea053a;
  border-left: 90px solid transparent;
  border-right: 90px solid transparent;
  margin-right: 40px;
  content: "";
  height: 0;
  left: 0;
  position: absolute;
  top: 0px;
  margin-top: 100px;
  width: 0;
}
<div class="design">

</div>

希望这会有所帮助。

My Fiddle

答案 3 :(得分:-1)

更改为(仅列出更改的行,其他内容保持不变):

.design:before {
  ...
  border-left: 114px solid transparent;
  border-right: 114px solid transparent;
  ...
  width: 0;
}

答案 4 :(得分:-1)

这是我为形状添加阴影和渐变的解决方案

.design {
  background: #ea053a;
  display: inline-block;
  height: 155px;
  margin-left: 33px;
  margin-right: 40px;
  position: relative;
  width: 228px;
  filter: drop-shadow(0 1px 10px rgba(0, 0, 0, 0.8));
 
}

.triangle {
  position: absolute;
  height: 100px;
  top: 155px;
  width: 228px;
  -webkit-clip-path: polygon(49% 44%, 0% 100%, 100% 100%);
  clip-path: polygon(49% 44%, 0% 100%, 100% 100%);
  background-color: #ea053a;
  transform: rotate(180deg);
  
}
<div class="design">
  <div class="triangle">
  </div>
</div>