今回は、擬似要素を使ってボタンの右端に矢印アイコン「→」を作成する方法について紹介します。
普段コーディングを担当していますが、ボタンの右端に矢印アイコンを実装する機会が多いので、この機会にまとめてみました。
コピペで使うことが出来ます。
【CSS】擬似要素を使ってボタンの右端に矢印アイコン「→」を作成する方法
完成形はこちら。
See the Pen
Untitled by ゆーじろー (@yuji64)
on CodePen.
それでは実際にコードを見ていきましょう。
HTML
<div class="button"> <a href="">詳しくはこちらへ</a> </div>
CSS
.button {
text-align: center;
}
.button a {
border: 2px solid blue;
display: inline-block;
max-width: 360px;
width: 100%;
padding: 10px 20px;
font-size: 16px;
border-radius: 10px;
text-align: center;
color: blue;
position: relative;
font-weight: bold;
text-decoration: none;
}
.button a::before {
background-color: blue;
position: absolute;
top: 50%;
right: 13px;
width: 15px;
height: 2px;
margin-top: 0px;
content: "";
}
.button a::after {
position: absolute;
top: 50%;
right: 13px;
width: 12px;
height: 12px;
margin-top: -6px;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
border-top: 2px solid blue;
border-right: 2px solid blue;
content: "";
display: inline-block;
vertical-align: middle;
}
aタグのbeforeとafterの擬似要素を使って矢印アイコンを作成します。
beforeで矢印アイコンの線を、そしてafterで矢印アイコンの三角の部分を実装します。
矢印アイコンの長さは自由に調整して下さい。
まとめ
以上が、擬似要素でボタンの右端に矢印アイコン「→」を作成する方法でした。



