デモで学ぶ ハンバーガーメニューの作り方
三本線から×への変形、背景付きのドロワーメニュー、PCとスマホの表示切替、背景・Escキー・リンク選択による閉じ方までを4段階で解説します。各デモと同じ見た目・動きを再現できるHTML・CSS・JavaScriptを掲載しています。
HTML・CSS・JSコードプレビューツール に貼り付けて実装を確認できます。
やりたいことから探す
① メニューボタン — 三本線をクリックで×に変える
現在: 閉じている
<button class="menu-button" type="button"
aria-label="メニューを開く" aria-expanded="false">
<span></span>
<span></span>
<span></span>
</button>
.menu-button {
width: 48px;
height: 48px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6px;
padding: 0;
border: 1px solid #cbd5e1;
border-radius: 10px;
background: #fff;
box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
cursor: pointer;
}
.menu-button span {
width: 26px;
height: 2px;
display: block;
border-radius: 2px;
background: #172033;
transition: transform 0.25s, opacity 0.25s;
}
.menu-button.is-open span:nth-child(1) {
transform: translateY(8px) rotate(45deg);
}
.menu-button.is-open span:nth-child(2) {
opacity: 0;
}
.menu-button.is-open span:nth-child(3) {
transform: translateY(-8px) rotate(-45deg);
}
const menuButton = document.querySelector('.menu-button');
menuButton.addEventListener('click', function() {
const isOpen = menuButton.className.indexOf('is-open') !== -1;
if (isOpen) {
menuButton.className = 'menu-button';
menuButton.setAttribute('aria-expanded', 'false');
menuButton.setAttribute('aria-label', 'メニューを開く');
} else {
menuButton.className = 'menu-button is-open';
menuButton.setAttribute('aria-expanded', 'true');
menuButton.setAttribute('aria-label', 'メニューを閉じる');
}
});
1本目を下へ8px、3本目を上へ8px動かしてから、それぞれ45度回転させます。中央の線は display: none ではなく opacity: 0 にするため、閉じるときも滑らかに戻ります。白いページへ貼り付けても見失わないよう、線は濃色、ボタンには薄い枠と影を指定しています。
② ドロワーメニュー — 背景を暗くして右から表示する
<header class="site-header">
<a class="site-logo" href="#">NOVE</a>
<button class="menu-button" type="button"
aria-label="メニューを開く" aria-expanded="false">
<span></span>
<span></span>
<span></span>
</button>
</header>
<button class="menu-overlay" type="button"
aria-label="メニューを閉じる"></button>
<nav class="menu-panel" aria-label="メインメニュー">
<p class="menu-title">MENU</p>
<a href="#">ホーム</a>
<a href="#">サービス</a>
<a href="#">お問い合わせ</a>
</nav>
.site-header {
min-height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
background: #fff;
border-bottom: 1px solid #e2e8f0;
}
.site-logo {
color: #172033;
font-size: 1.1rem;
font-weight: 800;
letter-spacing: 0.12em;
text-decoration: none;
}
.menu-button {
position: relative;
z-index: 30;
width: 48px;
height: 48px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6px;
padding: 0;
border: 1px solid #cbd5e1;
border-radius: 10px;
background: #fff;
box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
cursor: pointer;
}
.menu-button span {
width: 26px;
height: 2px;
display: block;
border-radius: 2px;
background: #172033;
transition: transform 0.25s, opacity 0.25s;
}
.menu-button.is-open span:nth-child(1) {
transform: translateY(8px) rotate(45deg);
}
.menu-button.is-open span:nth-child(2) {
opacity: 0;
}
.menu-button.is-open span:nth-child(3) {
transform: translateY(-8px) rotate(-45deg);
}
.menu-overlay {
position: fixed;
inset: 0;
z-index: 10;
padding: 0;
border: 0;
background: rgba(15, 23, 42, 0.55);
opacity: 0;
visibility: hidden;
cursor: pointer;
transition: opacity 0.25s, visibility 0.25s;
}
.menu-panel {
position: fixed;
top: 0;
right: 0;
z-index: 20;
width: min(82vw, 320px);
height: 100dvh;
padding: 96px 28px 32px;
box-sizing: border-box;
background: #fff;
box-shadow: -12px 0 30px rgba(15, 23, 42, 0.16);
transform: translateX(100%);
transition: transform 0.25s;
}
.menu-title {
margin: 0 0 18px;
color: #64748b;
font-size: 0.75rem;
font-weight: 800;
letter-spacing: 0.16em;
}
.menu-panel a {
display: block;
padding: 14px 0;
border-bottom: 1px solid #e2e8f0;
color: #172033;
font-weight: 700;
text-decoration: none;
}
.menu-overlay.is-open {
opacity: 1;
visibility: visible;
}
.menu-panel.is-open {
transform: translateX(0);
}
const menuButton = document.querySelector('.menu-button');
const menuOverlay = document.querySelector('.menu-overlay');
const menuPanel = document.querySelector('.menu-panel');
function changeMenu(openState) {
menuButton.className = openState
? 'menu-button is-open'
: 'menu-button';
menuOverlay.className = openState
? 'menu-overlay is-open'
: 'menu-overlay';
menuPanel.className = openState
? 'menu-panel is-open'
: 'menu-panel';
menuButton.setAttribute('aria-expanded', openState ? 'true' : 'false');
menuButton.setAttribute(
'aria-label',
openState ? 'メニューを閉じる' : 'メニューを開く'
);
}
menuButton.addEventListener('click', function() {
const isOpen = menuPanel.className.indexOf('is-open') !== -1;
changeMenu(!isOpen);
});
menuOverlay.addEventListener('click', function() {
changeMenu(false);
});
半透明の背景は opacity と visibility、白いパネルは transform で表示を切り替えます。パネルの背景・境界・影までコードに含めているため、貼り付け先が白背景でもパネルの位置と輪郭が分かります。ボタンの3本線も3つの span として記述しているので、スマホ表示で1本だけになることはありません。
③ レスポンシブ対応 — PCの横並びナビとスマホのボタンを切り替える
PC表示
スマホ表示
<header class="site-header">
<a class="site-logo" href="#">NOVE</a>
<nav class="desktop-nav" aria-label="PCメニュー">
<a href="#">ホーム</a>
<a href="#">サービス</a>
<a href="#">お問い合わせ</a>
</nav>
<button class="menu-button" type="button"
aria-label="メニューを開く" aria-expanded="false">
<span></span>
<span></span>
<span></span>
</button>
</header>
<nav class="mobile-nav" aria-label="スマホメニュー">
<a href="#">ホーム</a>
<a href="#">サービス</a>
<a href="#">お問い合わせ</a>
</nav>
.site-header {
min-height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
background: #fff;
border-bottom: 1px solid #e2e8f0;
}
.site-logo {
color: #172033;
font-size: 1.1rem;
font-weight: 800;
letter-spacing: 0.12em;
text-decoration: none;
}
.desktop-nav {
display: flex;
align-items: center;
gap: 24px;
}
.desktop-nav a,
.mobile-nav a {
color: #172033;
font-weight: 700;
text-decoration: none;
}
.menu-button {
width: 48px;
height: 48px;
display: none;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6px;
padding: 0;
border: 1px solid #cbd5e1;
border-radius: 10px;
background: #fff;
box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
cursor: pointer;
}
.menu-button span {
width: 26px;
height: 2px;
display: block;
border-radius: 2px;
background: #172033;
transition: transform 0.25s, opacity 0.25s;
}
.menu-button.is-open span:nth-child(1) {
transform: translateY(8px) rotate(45deg);
}
.menu-button.is-open span:nth-child(2) {
opacity: 0;
}
.menu-button.is-open span:nth-child(3) {
transform: translateY(-8px) rotate(-45deg);
}
.mobile-nav {
display: none;
padding: 8px 24px 18px;
background: #fff;
border-bottom: 1px solid #e2e8f0;
}
.mobile-nav a {
display: block;
padding: 12px 0;
border-bottom: 1px solid #e2e8f0;
}
.mobile-nav.is-open {
display: block;
}
@media (max-width: 800px) {
.desktop-nav {
display: none;
}
.menu-button {
display: flex;
}
}
const menuButton = document.querySelector('.menu-button');
const mobileNav = document.querySelector('.mobile-nav');
menuButton.addEventListener('click', function() {
const isOpen = mobileNav.className.indexOf('is-open') !== -1;
menuButton.className = isOpen
? 'menu-button'
: 'menu-button is-open';
mobileNav.className = isOpen
? 'mobile-nav'
: 'mobile-nav is-open';
menuButton.setAttribute('aria-expanded', isOpen ? 'false' : 'true');
});
PCでは横並びナビを表示し、メニューボタンを display: none にします。800px以下では逆に横並びナビを隠し、ボタンを display: flex で表示します。ボタン内の3つの span は flex-direction: column と gap: 6px で縦に並ぶため、3本が重なって1本に見える問題を防げます。
④ 閉じる操作 — 背景・Escキー・リンク選択をまとめて扱う
<header class="site-header">
<a class="site-logo" href="#">NOVE</a>
<button class="menu-button" type="button"
aria-label="メニューを開く" aria-expanded="false">
<span></span>
<span></span>
<span></span>
</button>
</header>
<button class="menu-overlay" type="button"
aria-label="メニューを閉じる"></button>
<nav class="menu-panel" aria-label="メインメニュー">
<button class="menu-close" type="button">閉じる</button>
<p class="menu-title">MENU</p>
<a href="#home">ホーム</a>
<a href="#service">サービス</a>
<a href="#contact">お問い合わせ</a>
</nav>
.site-header {
min-height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
background: #fff;
border-bottom: 1px solid #e2e8f0;
}
.site-logo {
color: #172033;
font-size: 1.1rem;
font-weight: 800;
letter-spacing: 0.12em;
text-decoration: none;
}
.menu-button {
position: relative;
z-index: 5;
width: 48px;
height: 48px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6px;
padding: 0;
border: 1px solid #cbd5e1;
border-radius: 10px;
background: #fff;
box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
cursor: pointer;
}
.menu-button span {
width: 26px;
height: 2px;
display: block;
border-radius: 2px;
background: #172033;
transition: transform 0.25s, opacity 0.25s;
}
.menu-button.is-open span:nth-child(1) {
transform: translateY(8px) rotate(45deg);
}
.menu-button.is-open span:nth-child(2) {
opacity: 0;
}
.menu-button.is-open span:nth-child(3) {
transform: translateY(-8px) rotate(-45deg);
}
.menu-overlay {
position: fixed;
inset: 0;
z-index: 10;
padding: 0;
border: 0;
background: rgba(15, 23, 42, 0.55);
opacity: 0;
visibility: hidden;
cursor: pointer;
transition: opacity 0.25s, visibility 0.25s;
}
.menu-panel {
position: fixed;
top: 0;
right: 0;
z-index: 20;
width: min(82vw, 320px);
height: 100dvh;
padding: 28px;
box-sizing: border-box;
background: #fff;
box-shadow: -12px 0 30px rgba(15, 23, 42, 0.16);
transform: translateX(100%);
transition: transform 0.25s;
}
.menu-close {
display: block;
margin: 0 0 36px auto;
padding: 8px 12px;
border: 1px solid #cbd5e1;
border-radius: 8px;
background: #fff;
color: #172033;
cursor: pointer;
}
.menu-title {
margin: 0 0 18px;
color: #64748b;
font-size: 0.75rem;
font-weight: 800;
letter-spacing: 0.16em;
}
.menu-panel a {
display: block;
padding: 14px 0;
border-bottom: 1px solid #e2e8f0;
color: #172033;
font-weight: 700;
text-decoration: none;
}
.menu-overlay.is-open {
opacity: 1;
visibility: visible;
}
.menu-panel.is-open {
transform: translateX(0);
}
const menuButton = document.querySelector('.menu-button');
const menuOverlay = document.querySelector('.menu-overlay');
const menuPanel = document.querySelector('.menu-panel');
const menuClose = document.querySelector('.menu-close');
const menuLinks = menuPanel.querySelectorAll('a');
function changeMenu(openState) {
menuButton.className = openState
? 'menu-button is-open'
: 'menu-button';
menuOverlay.className = openState
? 'menu-overlay is-open'
: 'menu-overlay';
menuPanel.className = openState
? 'menu-panel is-open'
: 'menu-panel';
menuButton.setAttribute('aria-expanded', openState ? 'true' : 'false');
menuButton.setAttribute(
'aria-label',
openState ? 'メニューを閉じる' : 'メニューを開く'
);
}
menuButton.addEventListener('click', function() {
const isOpen = menuPanel.className.indexOf('is-open') !== -1;
changeMenu(!isOpen);
});
menuOverlay.addEventListener('click', function() {
changeMenu(false);
});
menuClose.addEventListener('click', function() {
changeMenu(false);
});
for (let linkNum = 0; linkNum < menuLinks.length; linkNum += 1) {
menuLinks[linkNum].addEventListener('click', function() {
changeMenu(false);
});
}
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
changeMenu(false);
}
});
ボタン・背景・メニュー内リンク・Escキーの処理を別々に書き散らさず、changeMenu(false)へ集約します。これにより、背景だけ消えてパネルが残るといった状態のずれを防げます。このカードのコピペ用コードはHTML・CSS・JavaScriptの3点をすべて含んでいます。

当サイトで公開しているWebデザインやUIの実装例は、一覧として以下記事に纏めています。

