如何在CSS3中使用伪类创建自定义形状

时间:2016-07-06 23:37:28

标签: css3 css-shapes pseudo-class

我正在尝试使用Bootstrap创建一个看起来像这个图像的元素 enter image description here

这是我走了多远的屏幕截图

enter image description here

我从未参与伪类,我发现很难得到确切的形状。请看一下我的代码并帮我搞清楚。我在此处仅包含了第二个(屏幕截图右侧的一个)剪贴板代码。

HTML

<div class="col-xs-12 col-sm-6">
     <div class="clip">
          <div class="circle"></div>
     </div>

     <div class="pad">          
          <div class="paper"></div>    
     </div>
</div>

CSS

.clip, .circle{
    position: relative;
}

.clip::after, .clip::before, circle:after, .circle:before{
  display: block;
  position: absolute;
  content: "";
  z-index: 50;
}

.clip:before{
    top: 12.5px;
    left: 15%;
    width: 70%;
    border-bottom: solid 50px grey;
    border-left: solid 150px transparent;
    border-right: solid 150px transparent;

}

.clip:after{
  top: 60px;
  left: 15%;
  width: 70%;
  border-bottom: solid 55px grey;

  border-top-left-radius: 10px;
  border-top-right-radius: 10px;

  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}

.circle:before{
    top: 10px;
    left: 70%;
    width: 20%;
    height: 50px;
    border-radius: 50%;
    border-right: solid 150px yellow; 
}

3 个答案:

答案 0 :(得分:2)

因为没有SVG标签,我会选择伪&amp;渐变:

&#13;
&#13;
div {
  position:relative;
  float:left;
  margin:60px 60px 80px;
  width:180px;
  height:200px;
  border-radius:15px;
  background:white;
  box-shadow:/* draw inside part of border */0 0 0 20px #159E91, inset -1px -1px 1px;
}
div:before {/*to  draw outside part of  border with same radius inside/out */
  z-index:-1;
  border-radius:20px;
  content:'';
  border: 20px solid #159E91;
  position:absolute;
  top:-30px;
  left:-30px;
  right:-30px;
  bottom:-30px;
  box-shadow:0 -2px 2px rgba(30, 162, 149, 0.2), 0 0 2px white,  0 5px  5px -3px rgba(0,0,0,0.5)
}
div:after {/* draw gradient underneath clipper */
  content:'';
  position:absolute;
  top:0;
  border-radius: 0 15px 0 0;
  left:26px;
  width:152px;
  height:150px;
  background:
    linear-gradient(45deg, white 40%, rgba(255,255,255,0) 40% ),/* mask*/
    linear-gradient(-45deg, white  , transparent 70%),/* mask*/
    linear-gradient(to right   , rgba(0,0,0,0.25)  , rgba(0,0,0,0.15)),transparent ;
}
.clipper {/* hold clipper shape actually */
  display:block;
  width:128px;
  height:80px;
  margin: -52px auto 30px;
  position:relative;
  z-index:1;
  overflow:hidden;
}
.clipper b {/* show the clipper shape */
  border-radius:35px;
  position:absolute;
  height:150%;
  width:100%;
  box-shadow: 0 0 1px 1px gray;
  left:50%;
  top:-12px;
  transform-origin:0 0;
  transform:rotate(45deg);
  overflow:hidden;
  }
.clipper b:before {/* draw the hoe and paint around it */
  content:'';
  display:block;
  border-radius:100%;
  height:29px;
  width:29px;
  margin:20px;  
  box-shadow:inset -1px -1px   1px gray, 0 0 0 100px  #3B3B3B, inset 1px 1px 2px rgba(0,0,0,0.5);
}

/* to match fake picture's text */
.clipper ~ span {
  display:block;
  background:#353535;
  margin:10px 58px;
  padding:5px;
  position:relative;
  z-index:1;
}
.clipper ~ span:last-of-type {
  display:block;
  background:#353535;
  margin:10px 85px 10px 58px;
}
&#13;
<div>
  <span class="clipper"><b></b></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
&#13;
&#13;
&#13;

但这只是一个形状的CSS,其中图像或SVG可以用于设计。

您可以在此处播放:http://codepen.io/gc-nomade/pen/rLYYZx

答案 1 :(得分:0)

https://jsfiddle.net/ahe128/esmrLzuv/5/

我做了一些事情,但这真的很难,我会​​尝试完成这个:)

.clip,
.circle {
  position: relative;
}

.clip::after,
.clip::before,
circle:after,
.circle:before {
  display: block;
  position: absolute;
  content: "";
  z-index: 50;
}

.clip:before {
  top: 1rem;
  left: 10%;
  width: 20%;
  border-bottom: solid 50px grey;
  border-left: solid 150px transparent;
  border-right: solid 150px transparent;
}

.clip:after {
  top: 4.65rem;
  left: 10%;
  right:10%;
  width: 82%;
  border-bottom: solid 4.3rem grey;
  border-top-left-radius: 0.8rem;
  border-top-right-radius: 0.8rem;
  border-bottom-left-radius: 0.4rem;
  border-bottom-right-radius: 0.4rem;
}

.circle:before {
  top: 0.78rem;
  height: 1px;
  width:1px;
  border-radius: 50%;
  border: solid 25px white;
  z-index:100;
  left:47%
}

答案 2 :(得分:0)

最后.......我让它工作(除了对角线渐变)。但它尚未响应。我的目标是保持每个剪贴板的设计完好无损,并在小屏幕中将它们一个堆叠在另一个之下。有人可以指出我错过了它!

另外,如果在 Pure CSS 中有更好的方法,那么我很乐意看到它。

小提琴:https://jsfiddle.net/chandannadig/esmrLzuv/7/

enter image description here

/*Clip*/
.clip, .circle{
    position: relative;
}

.clip::after, .clip::before, circle:after, .circle:before{
  display: block;
  position: absolute;
  content: "";
}

.clip:before{
    z-index: 50;
    top: 1rem;
    left: 6.958rem;
    width: 29rem;
    border-bottom: solid 4rem grey;
    border-left: solid 11.5rem transparent;
    border-right: solid 11.5rem transparent;

}

.clip:after{
  top: 4.7rem;
  left: 6.958rem;
  width: 29rem;
  z-index: 50;
  border-bottom: solid 4rem grey;

  border-top-left-radius: 0.8rem;
  border-top-right-radius: 0.8rem;

  border-bottom-left-radius: 0.5rem;
  border-bottom-right-radius: 0.5rem;
}

.circle{
    position: absolute;
    z-index: 60;
    top: 0.4rem;
    left: 15.6rem;
    width: 12rem;
    height: 8rem;
    background: grey;
    border-radius: 50%;
}

.circle::before{
    z-index: 60;
    top: 1rem;
    left: 4.2rem;
    width: 3.5rem;
    height: 3.5rem;
    background: white;
    border-radius: 50%;
}
/*End of Clip*/