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

Wil*_*lyC 4 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;
}
Run Code Online (Sandbox Code Playgroud)
<div>
<button class="next-button" type="button">Next</button><div class="next-point"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

...但是如果我尝试使用::之后它就会失效.这是我尝试过的方式:

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div>
<button class="next-button" type="button">Next</button>
</div>
Run Code Online (Sandbox Code Playgroud)

我已经摆弄了很长一段时间,显然我并不完全明白如何使用::after.如何使用::after而不是创建单独的div 来完成第一个代码段中的按钮外观?

Ori*_*ori 6

伪元素是元素的子.next-button元素,因此它显示在按钮内部.您可以使用绝对位置在按钮外部显示它,并且right等于减去伪元素宽度(宽度= 36px - >右= -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;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <button class="next-button" type="button">Next</button>
</div>
Run Code Online (Sandbox Code Playgroud)

另一种选择是将元素绝对放置在右侧的元素内,并用于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;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <button class="next-button" type="button">Next</button>
</div>
Run Code Online (Sandbox Code Playgroud)