
どうも、べ〜やんです。
今回は、:beforeと:afterでハンバーガーメニューを作る方法を紹介します。

目次
:beforeと:afterを使った方法
以前spanを使ったハンバーガーメニューの作り方も紹介しましたが、今回は:beforeと:afterを使ったハンバーガーメニューの作り方を紹介します。
またjQueryの指定については以前の記事を確認してください↓
今回紹介する:beforeと:afterで作る方法の方がHTMLの記述を減らせるので管理しやすいと思います。
作り方
<div class="icon"></div>
HTMLはこれだけです。
最初に2本目の棒を作ります。
.icon {
width: 60px;
height: 8px;
color: #333;
background: currentColor;
position: relative;
margin-top: 25px;
}
2本目の棒なのでmargin-top: 25px;
で高さに余裕を持たせています。
.icon:before {
content: "";
position: absolute;
top: -15px;
width: 100%;
height: 100%;
background: currentColor;
}
次は擬似要素:before
を指定します。
position: absolute;
とtop: -15px;
で最初に作った棒の上に位置を指定します。
width: 100%;
とheight: 100%;
で先ほどと同じ大きさになります。
.icon:after {
content: "";
position: absolute;
top: 15px;
width: 100%;
height: 100%;
background: currentColor;
}
次は擬似要素:after
を指定します。
position: absolute;
とtop: 15px;
で最初に作った棒の下に位置を指定します。
See the Pen BaaMKvY by beeyan (@orientado) on CodePen.
おわりに
今回は、:beforeと:afterでハンバーガーメニューを作る方法を紹介しました。
今回はCSSの指定のみ紹介しましたので、jQueryの指定についてはこちらを確認してください↓
