【问题标题】:Button with two right arrows带有两个右箭头的按钮
【发布时间】:2018-06-14 21:44:49
【问题描述】:

我需要的是实现带有两个右箭头的按钮。一个箭头切割按钮左侧的一块,一个箭头延伸按钮右侧(请参见附图)。

我现在成功的只是添加右箭头(见下图)。

以下是我目前拥有的 css。

  .arrow-button {
    width: 178px;
    height: 82px;
    position: relative;
    object-fit: contain;
    margin-right: 20px;
    background-color: #be1a20;
  }
  .arrow-button:before {
    content:"";
    position: absolute;
    left: 100%;
    top: 26px;
    width: 0;
    height: 0;
    border-top: 13px solid transparent;
    border-left: 13px solid #be1a20;
    border-bottom: 13px solid transparent;
 }
 
 .arrow-label {
    font-family: Montserrat;
    font-size: 13px;
    font-weight: 500;
    font-style: normal;
    font-stretch: normal;
    line-height: normal;
    letter-spacing: normal;
    text-align: center;
    color: #ffffff;
    margin: auto;
  }
  .layer {
    font-family: Montserrat;
    font-size: 24px;
    font-weight: bold;
    font-style: normal;
    font-stretch: normal;
    line-height: normal;
    letter-spacing: normal;
    text-align: center;
    color: #ffffff;
    margin: auto;
  }
<div class="arrow-button">
        <div class="row">
          <label class="arrow-label">
            SENT
          </label>
        </div>
        <div class="row">
          <label class="layer">
            10
          </label>
        </div>
      </div>

【问题讨论】:

  • 我们可以假设背景颜色总是相同的吗?
  • @Roberrrt 是的,总是一样的。
  • 在那种情况下,@dfsq 已经回答了我们。

标签: html css css-shapes


【解决方案1】:

与您使用右箭头所做的类似,使用 :before 和 :after:

.arrow-button {
  width: 178px;
  height: 82px;
  position: relative;
  object-fit: contain;
  margin-right: 20px;
  background-color: #be1a20;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.arrow-button:before,
.arrow-button:after {
  content: "";
  position: absolute;
  left: 0;
  top: 26px;
  width: 0;
  height: 0;
  border-top: 13px solid transparent;
  border-left: 13px solid white;
  border-bottom: 13px solid transparent;
}

.arrow-button:after {
  left: 100%;
  border-left: 13px solid #be1a20;
}

.arrow-label {
  font-family: Montserrat;
  font-size: 13px;
  font-weight: 500;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: normal;
  text-align: center;
  color: #ffffff;
  margin: auto;
}

.layer {
  font-family: Montserrat;
  font-size: 24px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: normal;
  text-align: center;
  color: #ffffff;
  margin: auto;
}
<div class="arrow-button">
  <div class="row">
    <label class="arrow-label">SENT</label>
  </div>
  
  <div class="row">
    <label class="layer">10</label>
  </div>
</div>

所以从左到右的唯一区别是leftborder-left样式你需要重新定义:

.arrow-button:after {
  left: 100%;
  border-left: 13px solid #be1a20;
}

注意,我用 flexbox 垂直对齐文本,但你不必这样做,这与箭头无关。

【讨论】:

  • 非常感谢!!我怎么会错过我可以在...之后做到这一点 :) 注意:对齐也更好!
【解决方案2】:

您可以使用渐变来做到这一点,而无需额外的标记或伪元素。您还可以拥有透明度:

.button {
 width:200px;
 height:100px;
 display:inline-flex;
 justify-content:center;
 align-items:center;
 padding:0 30px;
 color:#fff;
 background:
 linear-gradient(to bottom left, transparent 50%,red 50.5%) right calc(50% - 10px) /30px 20px no-repeat,
 linear-gradient(to top left, transparent 50%,red 50.5%) right calc(50% + 10px) /30px 20px no-repeat,
 
 linear-gradient(to bottom right, transparent 50%,red 50.5%) left calc(50% + 10px) /30px 20px no-repeat,
 linear-gradient(to top right, transparent 50%,red 50.5%) left calc(50% - 10px) /30px 20px no-repeat,
 
 linear-gradient(red,red) bottom left/30px calc(50% - 20px) no-repeat,
 linear-gradient(red,red) top left/30px calc(50% - 20px) no-repeat,
 linear-gradient(red,red) center/calc(100% - 60px) 100% no-repeat;
}

body {
 background:linear-gradient(to right,pink,yellow);
}
<div class="button">
  some content
</div>

你也可以使用 CSS 变量来动态化:

.button {
 width:200px;
 height:100px;
 display:inline-flex;
 color:#fff;
 justify-content:center;
 align-items:center;
 margin:5px;
 --w:30px;
 --h:20px;
 padding:0 var(--w);
 background:
 linear-gradient(to bottom left, transparent 50%,red 50.5%) right calc(50% - var(--h)/2) /var(--w) var(--h),
 linear-gradient(to top left, transparent 50%,red 50.5%) right calc(50% + var(--h)/2) /var(--w) var(--h),
 
 linear-gradient(to bottom right, transparent 50%,red 50.5%) left calc(50% + var(--h)/2) /var(--w) var(--h),
 linear-gradient(to top right, transparent 50%,red 50.5%) left calc(50% - var(--h)/2) /var(--w) var(--h),
 
 linear-gradient(red,red) bottom left/var(--w) calc(50% - var(--h)),
 linear-gradient(red,red) top left/var(--w) calc(50% - var(--h)),
 linear-gradient(red,red) center/calc(100% - 2*var(--w)) 100%;
 background-repeat:no-repeat;
}

body {
 background:linear-gradient(to right,pink,yellow);
}
<div class="button">
  Some content
</div>

<div class="button" style="width:80px;--w:20px;--h:20px;">
  Some content
</div>

<div class="button" style="width:120px;--w:40px;--h:20px;">
  Some content  
</div>
<div class="button" style="height:50px;--w:10px;--h:10px;">
  Some content
</div>

【讨论】:

  • 在 Chrome (66.0.3359.181) 中,这些似乎有很多线穿过左侧的箭头。
  • @Stewartside 因为渐变没有抗锯齿 ;) .. 为避免这种情况,您可以增加颜色停止的百分比 .. 您可以尝试 51% 而不是 50.5%
  • 很好的解决方案!
【解决方案3】:

给你:

  .arrow-button {
    width: 178px;
    height: 82px;
    position: relative;
    object-fit: contain;
    margin-right: 20px;
    background-color: #be1a20;
  }
  .arrow-button:before {
    content:"";
    position: absolute;
    left: 0;
    top: 26px;
    width: 0;
    height: 0;
    border-top: 13px solid transparent;
    border-left: 13px solid white;
    border-bottom: 13px solid transparent;
 }
 .arrow-button:after {
    content:"";
    position: absolute;
    left: 100%;
    top: 26px;
    width: 0;
    height: 0;
    border-top: 13px solid transparent;
    border-left: 13px solid #be1a20;
    border-bottom: 13px solid transparent;
 }
 .container {
    display: flex;
    justify-content:center;
    flex-direction:column;
    vertical-align: middle;
    height: inherit;
 }
 .arrow-label {
    font-family: Montserrat;
    font-size: 13px;
    font-weight: 500;
    font-style: normal;
    font-stretch: normal;
    line-height: normal;
    letter-spacing: normal;
    text-align: center;
    color: #ffffff;
  }
  .layer {
    font-family: Montserrat;
    font-size: 24px;
    font-weight: bold;
    font-style: normal;
    font-stretch: normal;
    line-height: normal;
    letter-spacing: normal;
    text-align: center;
    color: #ffffff;
  }
<div class="arrow-button">
        <div class="container">
          <label class="arrow-label">
            SENT
          </label>
          <label class="layer">
            10
          </label>
        </div>
      </div>

【讨论】: