我如何使用:: after在我的按钮上制作箭头?

时间:2017-08-03 19:23:36

标签: html css

我可以在右侧创建一个带箭头的按钮:

.next-button {
    font-family: 'Source Sans Pro', Arial, Helvetica, sans-serif;
    text-decoration: none;
    color: #fff;
    text-align: center;
    border: none;
    background-color: #2399e5;
    display: inline-block;
    height: 36px;
    line-height: 36px;
    padding: 0 1rem;
}

.next-point{
    vertical-align: top;
    width: 0;
    height: 0;
    display: inline-block;
    border-top: 18px solid transparent;
    border-bottom: 18px solid transparent;
    
    border-left: 18px solid #2399e5;
    border-right: 18px solid transparent;
}
<div>
<button class="next-button" type="button">Next</button><div class="next-point"></div>
</div>

...但是如果我尝试使用::之后它就会失效。以下是我尝试过的方法:

.next-button {
    font-family: 'Source Sans Pro', Arial, Helvetica, sans-serif;
    text-decoration: none;
    color: #fff;
    text-align: center;
    border: none;
    background-color: #2399e5;
    display: inline-block;
    height: 36px;
    line-height: 36px;
    padding: 0 1rem;
}

.next-button::after{
    content: " ";
    vertical-align: top;
    width: 0;
    height: 0;
    display: inline-block;
    border-top: 18px solid transparent;
    border-bottom: 18px solid transparent;
    
    border-left: 18px solid #2399e5;
    border-right: 18px solid transparent;
}
<div>
<button class="next-button" type="button">Next</button>
</div>

我已经好好地摆弄了这个问题,显然我并不完全理解如何使用::after。如何使用::after完成第一个代码段中的按钮外观,而不是创建单独的div?

2 个答案:

答案 0 :(得分:6)

伪元素是.next-button元素的子元素,因此它显示在按钮内。您可以使用绝对位置在按钮外部显示它,right等于减去伪元素宽度(宽度= 36px - &gt;右= -36px)。

.next-button {
  position: relative;
  font-family: 'Source Sans Pro', Arial, Helvetica, sans-serif;
  text-decoration: none;
  color: #fff;
  text-align: center;
  border: none;
  background-color: #2399e5;
  display: inline-block;
  height: 36px;
  line-height: 36px;
  padding: 0 1rem;
}

.next-button::after {
  position: absolute;
  content: "";
  top: 0;
  right: -36px;
  width: 0;
  height: 0;
  display: inline-block;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-left: 18px solid #2399e5;
  border-right: 18px solid transparent;
}
<div>
  <button class="next-button" type="button">Next</button>
</div>

另一个选择是将元素绝对放在右侧的元素内,并使用translateX(100%)将其推到外面。

.next-button {
  position: relative;
  font-family: 'Source Sans Pro', Arial, Helvetica, sans-serif;
  text-decoration: none;
  color: #fff;
  text-align: center;
  border: none;
  background-color: #2399e5;
  display: inline-block;
  height: 36px;
  line-height: 36px;
  padding: 0 1rem;
}

.next-button::after {
  position: absolute;
  content: "";
  top: 0;
  right: 0;
  transform: translateX(100%);
  width: 0;
  height: 0;
  display: inline-block;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-left: 18px solid #2399e5;
  border-right: 18px solid transparent;
}
<div>
  <button class="next-button" type="button">Next</button>
</div>

答案 1 :(得分:0)

一个更优雅的解决方案是仅靠这些行:

.arrow-clip{
      clip-path: polygon(0% 0%, 80% 0, 100% 50%, 80% 100%, 0% 100%);
      background-color: #2399e5;
      color: #fff;
      padding: 0.2rem 2rem;
      border: 0px;
}
<button class="arrow-clip">Next</button>

如果您想调整箭头的曲线,可以用一个简单的片段生成器进行搜索,我认为this很好。