1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CSSだけで実装するハンバーガーメニュー(JavaScript不要)

1
Posted at

JavaScriptを使わずに、input type="checkbox" と label を組み合わせ、CSSだけで全画面メニューを開閉するハンバーガーメニューを実装してみました。
自分のアウトプットも兼ねてQiitaに投稿します!

この記事で学べること

  • JavaScriptなしでハンバーガーメニューを作る方法
  • checkbox + label のトグルテクニック
  • CSSだけで三本線を「×」に変形する方法

完成イメージ

  • JavaScript不要
  • 三本線がクリックで × に変形
  • メニューは画面全体にオーバーレイ

image.png
image.png
動作サンプルはこちら:
@codepen

実装例

HTML

<header class="header">
  <div class="header__inner">
    <!-- ハンバーガーメニュー -->
    <input type="checkbox" id="menu-toggle" class="header__menu-toggle" hidden>
    <label for="menu-toggle" class="header__hamburger">
      <span></span>
      <span></span>
      <span></span>
    </label>

    <!-- ヘッダーロゴ -->
    <div class="header__logo">
      <a href="/" class="header__logo-link">
        <span class="header__logo-text">LOGO</span>
      </a>
    </div>

    <!-- フルスクリーンメニュー -->
    <nav class="header__nav-content">
      <ul class="header__nav-list">
        <li class="header__nav-item"><a href="#" class="header__nav-link">Home</a></li>
        <li class="header__nav-item"><a href="#" class="header__nav-link">Registration</a></li>
        <li class="header__nav-item"><a href="#" class="header__nav-link">Login</a></li>
      </ul>
    </nav>
  </div>
</header>

CSS

/* ハンバーガー */
.header__hamburger {
  position: relative;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background-color: #2962ff;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.header__hamburger span {
  position: absolute;
  left: 4px;
  height: 2.5px;
  background: #ffffff;
  border-radius: 2px;
  transition: all 0.6s;
}

.header__hamburger span:nth-of-type(1) {
  top: 7px;
  width: 14px;
}

.header__hamburger span:nth-of-type(2) {
  top: 13px;
  width: 20px;
}

.header__hamburger span:nth-of-type(3) {
  top: 19px;
  width: 8px;
}

/* メニュー */
.header__nav-content {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  visibility: hidden;
  opacity: 0;
  transition: all 0.6s;
  z-index: 900;
  display: flex;
  justify-content: center;
  align-items: center;
}

.header__menu-toggle:checked ~ .header__nav-content {
  visibility: visible;
  opacity: 1;
}

/* クリック時 */
.header__menu-toggle:checked + .header__hamburger span {
  width: 20px;
}

.header__menu-toggle:checked + .header__hamburger span:nth-child(1) {
  transform: translateY(6px) rotate(45deg);
}
.header__menu-toggle:checked + .header__hamburger span:nth-child(2) {
  opacity: 0;
}
.header__menu-toggle:checked + .header__hamburger span:nth-child(3) {
  transform: translateY(-6px) rotate(-45deg);
}


.header {
  position: relative;
  width: 100%;
  background: #fff;
}

.header__inner {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 20px;
}

.header__logo-link {
  text-decoration: none;
}

.header__logo-text {
  color: #2962ff;
  font-weight: bold;
  font-size: 1.4rem;
}

.header__nav-list {
  list-style: none;
  text-align: center;
}

.header__nav-link {
  color: #333333;
  font-size: 1.2rem;
  text-decoration: none;
}

解説

1. input type="checkbox" をトグルスイッチとして使う

この checkbox は表示されないように hidden 属性が付いていますが、実際は ON/OFF を切り替えるためのスイッチ として動作します。

CSS の :checked セレクタを使うことで、checkbox の状態(チェックされているかどうか)を監視できるので、JavaScriptなしでも表示・非表示を切り替えられます。

2. label をクリックして ON/OFF

    <label for="menu-toggle" class="header__hamburger">
      <span></span>
      <span></span>
      <span></span>
    </label>

label の for="menu-toggle" は、input の id="menu-toggle" とリンクしています。
この label(ハンバーガーアイコン)をクリックすると、checkbox ON/OFF されます。

3. ~ セレクタでメニューを制御

.header__menu-toggle:checked ~ .header__nav-content {
  visibility: visible;
  opacity: 1;
}

~(兄弟セレクタ) を使うことで、checkbox と同じ階層にある .header__nav-content を操作します。
ハンバーガーアイコンをクリックしてchecked が ON になると、 .header__nav-content が表示されます。

4. ハンバーガーアイコンを「×」に変形

.header__menu-toggle:checked + .header__hamburger span {
  width: 20px;
}

3本線はそれぞれバラバラの長さのため、クリックをしたらまず長さを20pxに揃えます。

.header__menu-toggle:checked + .header__hamburger span:nth-child(1) {
  transform: translateY(6px) rotate(45deg);
}
.header__menu-toggle:checked + .header__hamburger span:nth-child(2) {
  opacity: 0;
}
.header__menu-toggle:checked + .header__hamburger span:nth-child(3) {
  transform: translateY(-6px) rotate(-45deg);
}

2本目の線を opacity: 0 で透明にして、1本目と3本目の線を回転させてクロスを作って×に変形させます。

まとめ

この実装はJavaScript不要で軽量かつシンプルです!
色・サイズをCSSだけでカスタマイズも可能です!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?