|
-
Oct 23rd, 2025, 10:57 AM
#1
[CSS3] Responsive Navigation Menu
This code will display depending on the screen resolution. For larger screens, 678px or greater, it will display a title along with the menu items. For anything smaller, it will display a site title along with a hamburger menu icon, and a full-screen navigation is displayed when the hamburger menu icon is clicked.
This does not require any JavaScript, but does require the :has pseudo-class (caniuse).
HTML:
HTML Code:
<header class="global-navigation">
<nav>
<h1>Site Title</h1>
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<input id="global-navigation-toggle" type="checkbox" class="toggle-mobile-menu" hidden>
<label for="global-navigation-toggle" class="icon-hamburger" aria-label="Open menu"></label>
</nav>
<aside>
<section>
<h2>Navigation</h2>
<label for="global-navigation-toggle" aria-label="Close menu">×</label>
</section>
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li>
<label for="global-navigation-toggle" aria-label="Close menu">Close</label>
</li>
</ul>
</aside>
</header>
CSS:
Code:
/* theme colors, change once here */
:root {
--nav-background-color: #555555;
--nav-color: #fefefa;
--nav-link-active: #ffdc31;
--aside-background-color: #fefefa;
--aside-color: #555555;
--aside-active-background-color: rgba(85, 85, 85, 0.94);
--site-title-size: 2rem;
}
/* normalization */
body, html {
font-family: Arial, sans-serif;
font-size: 16px;
margin: 0;
padding: 0;
}
/* icon */
.icon-hamburger {
width: 28px; height: 20px; border: 0; background: none; cursor: pointer;
background-image:
linear-gradient(currentColor, currentColor),
linear-gradient(currentColor, currentColor),
linear-gradient(currentColor, currentColor);
background-repeat: no-repeat;
background-size: 100% 2px, 100% 2px, 100% 2px;
background-position: 0 0, 0 9px, 0 18px;
}
/* desktop view */
.global-navigation {
background-color: var(--nav-background-color);
color: var(--nav-color);
}
.global-navigation > nav {
align-items: center;
display: flex;
margin: 0 auto;
max-width: clamp(768px, 80vw, 1400px);
padding: 1rem clamp(1.25rem, 2vw, 10rem);
}
.global-navigation > nav > h1 {
font-size: var(--site-title-size);
}
.global-navigation > nav > label {
display: none;
color: var(--nav-color);
}
.global-navigation > nav > ul {
align-items: center;
display: flex;
flex-grow: 1;
justify-content: flex-end;
list-style-type: none;
}
.global-navigation > nav > ul > li:not(:last-of-type) {
margin-right: 1.5rem;
}
.global-navigation > nav > ul > li > a {
border-bottom: none;
color: var(--nav-color);
opacity: 1;
text-decoration: none;
transition: opacity 0.3s ease;
}
.global-navigation > nav > ul > li > a.active {
color: var(--nav-link-active);
}
.global-navigation > nav > ul:hover > li > a {
border-bottom: none;
opacity: 0.5;
}
.global-navigation > nav > ul > li:hover > a {
border-bottom: 1px solid;
opacity: 1;
}
.global-navigation > aside {
display: none;
}
/* mobile view */
@media (max-width: 767.98px)
{
.global-navigation > nav {
justify-content: space-between;
}
.global-navigation > nav > ul {
display: none;
}
.global-navigation > nav > label {
display: initial;
}
.global-navigation > aside {
background-color: var(--aside-background-color);
color: var(--aside-color);
display: flex;
flex-direction: column;
height: 100vh;
inset: 0;
opacity: 0;
pointer-events: none;
position: fixed;
transform: translateX(100%);
transition: transform .25s ease, opacity .2s ease, visibility 0s linear .25s;
visibility: hidden;
width: 100vw;
z-index: 999;
}
.global-navigation:has(.toggle-mobile-menu:checked) > aside {
transform: translateX(0);
transition: transform .25s ease, opacity .2s ease;
opacity: 1;
pointer-events: auto;
visibility: visible;
}
.global-navigation > aside > section {
align-items: center;
background-color: var(--nav-background-color);
color: var(--nav-color);
display: flex;
justify-content: space-between;
padding: 1rem clamp(1.25rem, 2vw, 10rem);
}
.global-navigation > aside > section > h2 {
flex-grow: 1;
font-size: var(--site-title-size);
text-align: center;
}
.global-navigation > aside > section > label {
cursor: pointer;
font-size: var(--site-title-size);
}
.global-navigation > aside > ul {
flex-grow: 1;
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
overflow-y: auto;
}
.global-navigation > aside > ul > li > a,
.global-navigation > aside > ul > li:last-child > label {
display: block;
padding: 0.875rem clamp(1.25rem, 2vw, 10rem);
text-decoration: none;
color: var(--aside-color);
line-height: 44px;
min-height: 44px;
transition: background-color .2s ease, color .2s ease, opacity .2s ease;
border-bottom: 1px solid rgba(0,0,0,.06);
position: relative;
}
.global-navigation > aside > ul > li > a.active {
background: linear-gradient(0deg, var(--aside-active-background-color), var(--aside-active-background-color));
color: var(--nav-link-active);
font-weight: bold;
}
.global-navigation > aside > ul > li > a.active::before {
content: "";
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 0.25rem;
background: var(--nav-link-active);
}
.global-navigation > aside > ul > li > a:hover,
.global-navigation > aside > ul > li:last-child > label:hover {
background: linear-gradient(0deg, var(--aside-active-background-color), var(--aside-active-background-color));
color: var(--nav-link-active);
}
.global-navigation > aside > ul > li:last-child {
margin-top: auto;
}
.global-navigation > aside > ul > li:last-child > label {
cursor: pointer;
}
}
Demo: https://jsfiddle.net/9odm4q27/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|