【CSS】三角アイコンと矢印アイコンをつくる

デザオ
画像は使わないんですか?
デザコト
CSSだけで簡単にできますよー

三角アイコン

疑似要素「:before」でテキストの横に三角アイコンをつけています。

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_s">三角アイコン</a>

CSS

/* ---------- 01 三角アイコン ---------- */
.arrow_s {
  position: relative;
  display: inline-block;
  padding-left: 12px;
}
.arrow_s:before {
  content: '';
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 6px 0 6px 8px;
  border-color: transparent transparent transparent #2b2a27;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

三角アイコン(丸背景あり)

疑似要素「:before」で丸背景をつけています。
「:before」で丸背景、「:after」で三角で、三角アイコン(丸背景あり)になります。

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_s_b">三角アイコン(丸背景あり)</a>

CSS

/* ---------- 02 三角アイコン(丸背景あり) ---------- */
.arrow_s_b {
  position: relative;
  display: inline-block;
  padding-left: 22px;
}
.arrow_s_b:before {
  content: '';
  width: 18px;
  height: 18px;
  background: #2b2a27;
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}
.arrow_s_b:after {
  content: '';
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 4px 0 4px 6px;
  border-color: transparent transparent transparent #fff;
  position: absolute;
  top: 0;
  left: 6px;
  bottom: 0;
  margin: auto;
}

矢印アイコン(右向き)

疑似要素「:before」を使って矢印を作ります。
三角アイコンとは違って少し難しいですが慣れてしまえばいろんな形になるのでおすすめです。

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_r">矢印アイコン(右向き)</a>

CSS

/* ---------- 03 矢印アイコン(右向き) ---------- */
.arrow_r {
  position: relative;
  display: inline-block;
  padding-left: 20px;
}
.arrow_r:before {
  content: '';
  width: 6px;
  height: 6px;
  border: 0;
  border-top: solid 2px #2b2a27;
  border-right: solid 2px #2b2a27;
  transform: rotate(45deg);
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

矢印アイコン(左向き)

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_l">矢印アイコン(左向き)</a>

CSS

/* ---------- 04 矢印アイコン(左向き) ---------- */
.arrow_l {
  position: relative;
  display: inline-block;
  padding-left: 20px;
  color: #2b2a27;
  text-decoration: none;
}
.arrow_l:before {
  content: '';
  width: 6px;
  height: 6px;
  border: 0;
  border-top: solid 2px #2b2a27;
  border-left: solid 2px #2b2a27;
  transform: rotate(-45deg);
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

矢印アイコン(上向き)

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_o">矢印アイコン(上向き)</a>

CSS

/* ---------- 05 矢印アイコン(上向き) ---------- */
.arrow_o {
  position: relative;
  display: inline-block;
  padding-left: 20px;
}
.arrow_o:before {
  content: '';
  width: 6px;
  height: 6px;
  border: 0;
  border-top: solid 2px #2b2a27;
  border-right: solid 2px #2b2a27;
  transform: rotate(-45deg);
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

矢印アイコン(下向き)

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_u">矢印アイコン(下向き)</a>

CSS

/* ---------- 06 矢印アイコン(下向き) ---------- */
.arrow_u {
  position: relative;
  display: inline-block;
  padding-left: 20px;
}
.arrow_u:before {
  content: '';
  width: 6px;
  height: 6px;
  border: 0;
  border-bottom: solid 2px #2b2a27;
  border-right: solid 2px #2b2a27;
  transform: rotate(45deg);
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}

矢印アイコン(丸背景あり)

疑似要素「:before」で丸背景をつけています。
「:before」で丸背景、「:after」で矢印で、矢印アイコン(丸背景あり)になります。

コードを見る(HTML・CSS)

HTML

<a href="#" class="arrow_r_b">矢印アイコン(丸背景あり)</a>

CSS

/* ---------- 07 矢印アイコン(丸背景あり) ---------- */
.arrow_r_b {
  position: relative;
  display: inline-block;
  padding-left: 22px;
}
.arrow_r_b:before {
  content: '';
  width: 18px;
  height: 18px;
  background: #2b2a27;
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  margin: auto;
}
.arrow_r_b:after {
  content: '';
  width: 6px;
  height: 6px;
  border: 0;
  border-top: solid 2px #fff;
  border-right: solid 2px #fff;
  transform: rotate(45deg);
  position: absolute;
  top: 0;
  left: 4px;
  bottom: 0;
  margin: auto;
}

メモ

三角をつくるのにとても便利なサイトさんはこちらです。

さいごに

デザオ
色も変えれますか?
デザコト
色も大きさも簡単に変えられますよー

ふたりの小言

関連記事

デザコトauthor

あ、いいな、と思うWebデザインを紹介しています。デザインの参考に。やさしいデザインが多いです。Webデザインギャラリー『デザインのこと - Web design gallery』を運営しています。