デモ
See the Pen nav 15 by logsuke (@design-list) on CodePen.
コード
<!-- Boxicons headタグ内へ -->
<link href='https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css' rel='stylesheet'>
<header class="header" id="header">
<nav class="nav">
<a href="#" class="logo">
<svg viewBox="0 0 496 512" width="100" title="meh-rolling-eyes">
<path
d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zM88 224c0-24.3 13.7-45.2 33.6-56-.7 2.6-1.6 5.2-1.6 8 0 17.7 14.3 32 32 32s32-14.3 32-32c0-2.8-.9-5.4-1.6-8 19.9 10.8 33.6 31.7 33.6 56 0 35.3-28.7 64-64 64s-64-28.7-64-64zm224 176H184c-21.2 0-21.2-32 0-32h128c21.2 0 21.2 32 0 32zm32-112c-35.3 0-64-28.7-64-64 0-24.3 13.7-45.2 33.6-56-.7 2.6-1.6 5.2-1.6 8 0 17.7 14.3 32 32 32s32-14.3 32-32c0-2.8-.9-5.4-1.6-8 19.9 10.8 33.6 31.7 33.6 56 0 35.3-28.7 64-64 64z" />
</svg>
logo
</a>
<div class="nav-menu" id="navMenu">
<ul class="nav-links">
<li class="nav-item">
<button>Section1</button>
<ul class="drop-down">
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
</ul>
</li>
<li class="nav-item">
<button>Section2</button>
<ul class="drop-down">
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
</ul>
</li>
<li class="nav-item">
<button>Section3</button>
<ul class="drop-down">
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
</ul>
</li>
<li class="nav-item">
<button>Section4</button>
<ul class="drop-down">
<li class="item"><a href=""><img src="https://placehold.jp/969ce8/ffffff/200x150.png" alt=""></a></li>
</ul>
</li>
</ul>
</div>
<div class="nav-toggle" id="navToggle">
<i id="navIcon" class='bx bx-menu'></i>
</div>
</nav>
</header>
/*=== Base ===*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: sans-serif;
}
:root {
--nav-height: 8rem;
--max-width: 1024px;
--container-padding: 0 4rem;
--gap: 2rem;
--bg-color: rgb(240, 240, 240);
}
/*=== Font ratio ===*/
html {
scroll-behavior: smooth;
font-size: 62.5%;
}
body {
font-size: 1.6rem;
}
ul {
list-style: none;
}
a {
text-decoration: none;
}
button {
border: none;
font-size: 1.6rem;
}
/* header */
header {
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 100;
background-color: var(--bg-color);
}
.nav svg {
width: 25px;
padding: 2px;
margin-right: 5px;
}
/* nav */
.nav {
width: 100%;
max-width: var(--max-width);
height: var(--nav-height);
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 auto;
padding: 0 3rem;
}
.logo,
.nav-toggle {
color: var(--text-color);
cursor: pointer;
}
.logo {
font-weight: bold;
font-size: 2rem;
z-index: 100;
display: flex;
justify-content: center;
align-items: center;
}
.nav-toggle {
font-size: 3rem;
display: flex;
align-items: center;
z-index: 200;
}
.nav-toggle i {
display: none;
}
.nav-links {
display: flex;
align-items: center;
justify-content: center;
gap: 2rem;
}
.nav-links>a:hover {
color: rgb(134, 170, 207);
}
.nav-item>a, .nav-item>button {
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
height: var(--nav-height);
border: none;
font-size: 1.6rem;
cursor: pointer;
}
.nav-item:hover {
color: rgb(134, 170, 207);
}
.nav-item:hover .drop-down {
opacity: 1;
pointer-events: auto;
}
.drop-down {
width: 100%;
position: absolute;
top: var(--nav-height);
left: 0;
background: var(--bg-color);
padding: 3rem;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
opacity: 0;
pointer-events: none;
transition: opacity .2s;
}
.item {
padding: 2rem 0;
position: relative;
}
.item a {
display: flex;
align-items: center;
justify-content: center;
background-color: var(--first-color);
overflow: hidden;
}
.item img {
width: 100%;
object-fit: cover;
transition: transform .4s;
}
.item:hover img {
opacity: .6;
transform: scale(1.2);
}
const navMenu = document.getElementById('navMenu'),
navToggle = document.getElementById('navToggle'),
navIcon = document.getElementById('navIcon'),
navLink = document.querySelectorAll('.nav-link')
navToggle?.addEventListener('click', function () {
if (navIcon.classList.contains('bx-menu')) {
navIcon.classList.replace('bx-menu', 'bx-x');
navMenu.classList.toggle('toggle');
} else {
navIcon.classList.replace('bx-x', 'bx-menu');
navMenu.classList.toggle('toggle');
}
})
navLink.forEach(function (li) {
li.addEventListener('click', function () {
navIcon.classList.replace('bx-x', 'bx-menu');
navMenu.classList.remove('toggle');
})
})