我怎样才能正确地添加阴影和渐变到我的形状?

问题描述 投票:18回答:5

我想作如下设计:

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>

(Qazxswpoi)

我怎么能离开它一样的原始设计,并与以下两个属性?:

fiddle
html css css3 css-shapes linear-gradients
5个回答
11
投票

下面是歪斜变换和box-shadow: 0 1px 10px 0 rgba(0, 0, 0, 0.2); background-image: linear-gradient(to bottom, #ea053a, #d0021b); 过滤器的想法。你只需要一些额外的元素,以正确的有梯度。关键是要反转倾斜,以保持梯度方向正确(如果我们处理纯色不需要)

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%具有其大小为所述主容器然后我们进行调整,以一个背景的假象的位置。这就像每个元素将显示一半的主要背景。


3
投票

像我一样,你可以使用夹路径。这里是我的解决方案。

200%
.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;
}

0
投票

如果你改变你的CSS以下微小的变化,那么你可以达到你预料的结果:

<div class="design">
  <div class="month">Diciembre</div>
  <div class="day">29</div>
</div>

以下是上述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;
}
.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>


-1
投票

更改为(只列出了更改的行,保持一切不变化):

My Fiddle

-1
投票

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

.design:before {
  ...
  border-left: 114px solid transparent;
  border-right: 114px solid transparent;
  ...
  width: 0;
}
.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);
  
}
© www.soinside.com 2019 - 2024. All rights reserved.