有边界的5角CSS元素

时间:2016-10-26 16:53:21

标签: html css css3 css-shapes

我需要创建一个这样的元素:

generated POM

<span>元素中的5个角,边框和文字。我怎样才能达到这个结果?我只设法用4个角创建普通元素。

&#13;
&#13;
.pin {
  position: absolute;
  top: 7px;
  right: 7px;
  display: inline-block;
  min-height: 34px;
  min-width: 42px;
  vertical-align: top;
  padding: 0px 4px;
  color: #fff;
  text-align: center;
  line-height: 1.2;
  text-transform: uppercase;
  border-radius: 4px;
  box-shadow: inset 0px 0px 0px 1px rgba(255, 255, 255, 0.28);
}
.pin__green {
  background-color: #689f39;
  border: 1px solid #59902b;
}
.pt {
  font-size: 13px;
  font-family: 'PT Sans';
  display: block;
  margin-top: 10px;
}
&#13;
<span class="pin pin__green">
  <span class="pt">
    <span>2</span>
    <span>new</span>
  </span>
</span>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

我创建了一个单元素解决方案。基本上你使用伪元素::before来创建提示(与主要元素相同的样式但旋转45°)和::after以隐藏左边不需要的盒子阴影。

一些额外的提示:不要嵌套<span>元素,并使用0代替0px来获取CSS零值。

.pin {
  position: relative;
  display: inline-block;
  height: 34px;
  min-width: 42px;
  padding: 0 10px 0 2px;
  margin-left: 20px;
  color: #fff;
  font-size: 13px;
  font-family: Arial;
  text-align: center;
  line-height: 34px;
  text-transform: uppercase;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
}
.pin-green {
  background-color: green;
  border: 1px solid green;
  border-left: 0 solid;
}
/* the tip */

.pin::before {
  content: "";
  position: absolute;
  left: -12px;
  top: 3px;
  height: 25px;
  width: 25px;
  background-color: green;
  border: 1px solid green;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
  z-index: -1;
  transform: rotate(45deg);
}
/* hide the left box-shadow */

.pin::after {
  content: "";
  position: absolute;
  left: 0;
  top: 2px;
  height: 30px;
  width: 2px;
  background-color: green;
}
<div class="pin pin-green">2 new</div>

答案 1 :(得分:2)

<强> SVG

使用svg创建这种形状要容易得多:
text元素具有 tspan 元素而不是span元素

&#13;
&#13;
body {
  background-color: #ddd;
}
.greenButtonText {
  font-size: 25px;
  fill: white;
}
&#13;
<svg viewBox="-5 -5 110 60" width="60px">
  <path stroke-linejoin="round" stroke-width="5" stroke="#689f39" fill="#689f39" d="M0,25 25,0 100,0 100,50 25,50z" />
  <path stroke-linejoin="round" stroke-width="2" stroke="#92C567" fill="none" d="M0,25 25,0 100,0 100,50 25,50z" />
  <text class="greenButtonText" x="25" y="32">
    <tspan>2</tspan>
    <tspan font-size="20">NEW</tspan>
  </text>
</svg>
&#13;
&#13;
&#13;

更大的副本:

&#13;
&#13;
body {
  background-color: #ddd;
}
.greenButtonText {
  font-size: 25px;
  fill: white;
}
&#13;
<!---Copy for display only--->
<svg viewBox="-5 -5 110 60" width="150px">
  <path stroke-linejoin="round" stroke-width="5" stroke="#689f39" fill="#689f39" d="M0,25 25,0 100,0 100,50 25,50z" />
  <path stroke-linejoin="round" stroke-width="1" stroke="#92C567" fill="none" d="M0,25 25,0 100,0 100,50 25,50z" />
  <text class="greenButtonText" x="25" y="32">
    <tspan>2</tspan>
    <tspan font-size="20">NEW</tspan>
  </text>
</svg>
&#13;
&#13;
&#13;