0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ハンバーガーメニューのスタンダード(CSSとJS)

0
Last updated at Posted at 2026-06-24

はじめまして。タクと申します。
現在スクールでフロントエンド分野を学んでいます。

今回が初投稿です。よろしくお願いいたします。
最初はスクールの課題で作成したhtmlサイトのCSSとJavaScritを使用したシンプルなハンバーガーメニューのコードになります。

レスポンシブデザインの定番ですね。

CSS

/* ヘッダー
------------------------------- */
nav {
    position: fixed;
    top: 0;
    right: -100%;
    bottom: 0;
    left: 100%;
    background-color: rgba(255, 255, 255, 0.85);
    transition: all 0.3s ease-in-out;
}

.open nav {
    translate: -100% 0;
}

.main-nav {
    /* background-color: pink; */
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 2.5rem;
    list-style: none;
    text-align: center;
    height: 100%;
}

.main-nav a {
    font-size: 2rem;
    color: dimgrey;
    font-weight: bold;
    display: block;
}

.main-nav a:hover {
    color: #0bd;
}

.nav-btn {
    position: fixed;
    top: 17px;
    right: 20px;
    /* background-color: pink; */
    width: 44px;
    height: 44px;
    padding: 8px;
    background-color: beige;
    border-radius: 22px;
}

.nav-btn-line {
    display: block;
    height: 1px;
    width: 30px;
    background-color: #432;
    position: relative;
    margin: auto;
    transition: all 0.3s ease-in-out;
}

.open .nav-btn-line {
    background-color: transparent;
}

.nav-btn-line::before,
.nav-btn-line::after {
    content: '';
    display: block;
    height: 100%;
    width: 100%;
    background-color: #432;
    position: absolute;
    transition: inherit;
    /* inherit...先祖要素から値を継承 */
}

.nav-btn-line::before {
    top: -10px;
}

.open .nav-btn-line::before {
    rotate: 45deg;
    top:0
}

.nav-btn-line::after {
    top: 10px;
}

.open .nav-btn-line::after {
    rotate: -45deg;
    top: 0;
}


.visually-hidden {
    position: fixed !important;
    /* keep it on viewport */
    top: 0px !important;
    left: 0px !important;
    /* give it non-zero size, VoiceOver on Safari requires at least 2 pixels
     before allowing buttons to be activated. */
    width: 4px !important;
    height: 4px !important;
    /* visually hide it with overflow and opacity */
    opacity: 0 !important;
    overflow: hidden !important;
    /* remove any margin or padding */
    border: none !important;
    margin: 0 !important;
    padding: 0 !important;
    /* ensure no other style sets display to none */
    display: block !important;
    visibility: visible !important;
}

Javascript

const navBtn = document.getElementById('nav-btn');
const bodyElm = document.querySelector('body');
navBtn.addEventListener('click', () => {
bodyElm.classList.toggle('open');
});

const navElm = document.querySelector('nav');
navElm.addEventListener('click', (event) => {
console.log(event.target.href);
if (event.target.href) {
bodyElm.classList.remove('open');
}
});

HTML

<header class="header wrapper">
    <h1 class="header-logo">
        <a href="index.html"><img src="images/header-logo.png" alt=""></a>
    </h1>
    <nav>
        <ul class="main-nav">
        <li><a href="index.html#works">Works</a></li>
        <li><a href="index.html#about">About</a></li>
        </ul>
    </nav>

    <button type="button" class="nav-btn" id="nav-btn">
        <span class="nav-btn-line">
        <span class="visually-hidden">メニュー開閉</span>
        </span>
    </button>
</header>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?