Creating a responsive navbar
The user wants code for a responsive navbar that hides on scroll down and reappears on scroll up, using HTML, JS, and CSS. I think they’re looking for a simple example with a brief explanation. I should create a navbar that has a hamburger icon for mobile devices and behavior that triggers with scrolling. This involves using a fixed or sticky header and monitoring the scroll position. I’ll need three code snippets: HTML, CSS, and JS, while ensuring it works well on mobile too.Implementing scroll detection for navbar
I want to incorporate safe scroll detection so the navbar hides or shows based on the scroll position. I’ll use a function that compares the current scroll position with the last one, hiding the navbar on scroll down and showing it on scroll up.
I need to ensure that the navbar is visible when at the top of the page. I’ll outline the HTML structure and CSS for responsiveness, including a smooth transition for hiding the navbar. Providing a clear copy-and-paste example along with concise comments will help the user. Let's put it all together! Below is a minimal, copy‑paste‑ready example of a responsive navbar that:
Is fixed at the top
Collapses into a hamburger menu on small screens
Hides on scroll down and reappears on scroll up
1. HTML
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Hide-on-Scroll Navbar</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header id="navbar">
<nav class="nav">
<div class="nav__brand">MySite</div>
<button class="nav__toggle" id="navToggle" aria-label="Toggle menu">
<span class="nav__toggle-line"></span>
<span class="nav__toggle-line"></span>
<span class="nav__toggle-line"></span>
</button>
<ul class="nav__links" id="navLinks">
<li><a href="#home">Home</a></li>
<li><a href="#features">Features</a></li>
<li><a href="#pricing">Pricing</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</header>
<!-- Dummy content to enable scrolling -->
<main>
<section id="home" style="height: 100vh; padding: 2rem;">
<h1>Home</h1>
<p>Scroll down to see the navbar hide, then scroll up to show it again.</p>
</section>
<section id="features" style="height: 100vh; padding: 2rem;">
<h1>Features</h1>
</section>
<section id="pricing" style="height: 100vh; padding: 2rem;">
<h1>Pricing</h1>
</section>
<section id="about" style="height: 100vh; padding: 2rem;">
<h1>About</h1>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
2. CSS (styles.css)
css Copy code
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
/* Navbar container */
#navbar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
background: #ffffff;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
transition: transform 0.25s ease-in-out;
}
/* Class used to hide navbar on scroll down */
#navbar.nav--hidden {
transform: translateY(-100%);
}
/* Nav layout */
.nav {
max-width: 1100px;
margin: 0 auto;
padding: 0.75rem 1rem;
display: flex;
align-items: center;
justify-content: space-between;
}
.nav__brand {
font-size: 1.25rem;
font-weight: 600;
}
/* Links (desktop) */
.nav__links {
list-style: none;
display: flex;
gap: 1.5rem;
}
.nav__links a {
text-decoration: none;
color: #333;
font-size: 0.95rem;
font-weight: 500;
}
.nav__links a:hover {
color: #0077ff;
}
/* Hamburger button (hidden on desktop) */
.nav__toggle {
display: none;
flex-direction: column;
justify-content: space-between;
width: 24px;
height: 18px;
background: none;
border: none;
cursor: pointer;
padding: 0;
}
.nav__toggle-line {
width: 100%;
height: 2px;
background-color: #333;
border-radius: 4px;
}
/* Responsive styles */
@media (max-width: 768px) {
.nav__toggle {
display: flex;
}
.nav__links {
position: absolute;
top: 100%;
left: 0;
right: 0;
flex-direction: column;
gap: 0;
background: #ffffff;
max-height: 0;
overflow: hidden;
transition: max-height 0.25s ease-out;
border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}
.nav__links li {
border-top: 1px solid rgba(0, 0, 0, 0.05);
}
.nav__links a {
display: block;
padding: 0.9rem 1rem;
}
/* Shown when toggled in JS */
.nav__links.nav__links--open {
max-height: 240px; /* adjust if you add more items */
}
}
/* Add padding-top to body so content isn't hidden behind fixed navbar */
main {
padding-top: 60px;
}
3. JavaScript (script.js)
javascript Copy code
// Handle hamburger menu (mobile)
const navToggle = document.getElementById('navToggle');
const navLinks = document.getElementById('navLinks');
navToggle.addEventListener('click', () => {
navLinks.classList.toggle('nav__links--open');
});
// Hide / show navbar on scroll
const navbar = document.getElementById('navbar');
let lastScrollY = window.scrollY;
window.addEventListener('scroll', () => {
const currentScrollY = window.scrollY;
// Scroll down & past a small threshold => hide
if (currentScrollY > lastScrollY && currentScrollY > 80) {
navbar.classList.add('nav--hidden');
} else {
// Scrolling up => show
navbar.classList.remove('nav--hidden');
}
lastScrollY = currentScrollY;
});
You can place these in index.html, styles.css, and script.js and open the HTML file in a browser to see the behavior.