CSS:在纯CSS箭头上创建边框

时间:2015-03-19 08:42:07

标签: html css css3 css-shapes

我有这段代码:



.multiply-button {
  display: table;
  background: rgba(0, 0, 0, 0);
  border: none;
  color: white;
  padding: 0;
}
.multiply-button-content {
  display: table-cell;
  background: green;
  padding: 10px 9px;
  border: solid 1px black;
  border-right: none !important;
}
.multiply-button-arrow {
  display: table-cell;
  width: 0px;
  height: 0px;
  border-style: solid;
  border-width: 20px 0 20px 12px;
  border-color: transparent transparent transparent green;
}

<button id="multiply-button" class="multiply-button">
  <div class="multiply-button-content">Multiply</div>
  <div class="multiply-button-arrow"></div>
</button>
&#13;
&#13;
&#13;

我需要在这个&#34;箭头&#34;按钮。我可以很容易地边界矩形部分(我已经做过了),但如何在三角形部分上制作这个边框?

5 个答案:

答案 0 :(得分:4)

以下应该做你需要的事情

.multiply-button {
  display: table;
  background: rgba(0, 0, 0, 0);
  border: none;
  color: white;
  padding: 0;
}
.multiply-button-content {
  display: table-cell;
  background: green;
  padding: 0 9px;
  border: solid 1px black;
  border-right: none !important;
  position: relative;
  vertical-align:middle;
  height: 40px; /* double the border width */
  box-sizing: border-box;
}
.multiply-button-content:after,
.multiply-button-content:before {
  left: 100%;
  top: 50%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  border-width: 20px 0 20px 12px;
  margin-top: -20px;
}
.multiply-button-content:after {
  border-color: rgba(0, 128, 0, 0);
  border-left-color: #008000;
  margin-left: -1px;
}
.multiply-button-content:before {
  border-color: rgba(0, 0, 0, 0);
  border-left-color: #000000;
}
<button id="multiply-button" class="multiply-button">
  <div class="multiply-button-content">Multiply</div>
</button>

This is a useful tool

答案 1 :(得分:3)

div{
  position: relative;
  background-color: #008000;
  padding: 0px 16px;
  height: 40px;
  line-height: 40px;
  display: inline-block;
  color: white;
  border: 2px solid black;
  border-right: none;
  z-index:1;
}

div:before{
  content: '';
  position: absolute;
  left: 100%;
  z-index:-1;
  width: 28px;
  height: 28px;
  background-color: #008000;
  border-right: 2px solid black;
  border-bottom: 2px solid black;
  transform: rotate(-45deg) translate(-14px,-7px);
}
<div>Multiply</div>

答案 2 :(得分:2)

或者更简单:

只有一个伪元素的CSS

    .multiply-button {
       background: rgba(0, 0, 0, 0);
       border: none;
       width: 100px;
       color: #FFF;
       padding: 0;
       overflow: hidden;
    }

    .multiply-button-content {
       display: block;
       position: relative;
       background: #008000;
       width: 60px;
       padding: 10px 9px;
       border: solid 1px #000;
       border-right: none !important;
    }

    .multiply-button-content:before {
      content: "";
      position: absolute;
      width: 36px;
      height: 31px;
      z-index: -1;
      top: 0;
      right: -13px;
      border: 1px solid #000;
      background: #008000;
      transform: rotate(45deg);
    }
    <button id="multiply-button" class="multiply-button">
        <div class="multiply-button-content">Multiply</div>
    </button>

答案 3 :(得分:1)

由于只需要一个伪元素来制作'点',你可以使用另一个伪元素来制作一个边框(使其大小稍大)。

例如;

div {
  height: 30px;
  border: 2px solid black;
  display: inline-block;
  border-right: 2px solid transparent;
  line-height: 30px;
  text-align: center;
  position: relative;
  background: tomato;
  color: white;
}
div:before {
  content: "";
  position: absolute;
  border: 17px solid transparent;
  border-left: 17px solid black;
  right: -35px;
  top: -2px;
  z-index: 6;
}
div:after {
  content: "";
  position: absolute;
  border: 15px solid transparent;
  border-left: 15px solid tomato;
  right: -31px;
  top: 0;
  z-index: 8;
}
<div>Arrow, Please!</div>

答案 4 :(得分:0)

您可以使用:before:after伪选择器实现此目的。研究并调整以下示例。

&#13;
&#13;
.multiply-button {
  display: inline;
  background: rgba(0, 0, 0, 0);
  border: none;
  color: white;
  padding: 0;
  position: realtive;
}
.multiply-button-content {
  display: table-cell;
  background: green;
  padding: 10px 9px;
  border: solid 1px black;
  border-right: none !important;
  width: 100px;
  position: relative;
}
.multiply-button-arrow {
  width: 0;
  height: 0px;
  border-style: solid;
  border-width: 20px 0 20px 12px;
  border-color: transparent transparent transparent black;
  position: absolute;
  top: -2px;
  right:-12px;
}

.multiply-button-arrow:before {
  border-style: solid;
  border-width: 20px 0 20px 12px;
  border-color: transparent transparent transparent green;
  position: absolute;
  right: 1px;
  top: -20px;
  content: "";
}
&#13;
<button id="multiply-button" class="multiply-button">
  <div class="multiply-button-content">
    
    <div class="multiply-button-arrow"></div>
    Multiply</div>
  
</button>
&#13;
&#13;
&#13;