Shrink Navigation Menu on Scroll
How to » Shrink Navigation Menu on Scroll
Study in this chapter:
1. - How do I shrink a navigation menu on scroll?
2. - shrink navbar / header on scroll
3. - sticky / show navbar on scroll down
1. Create a sticky/fixed navbar */ #navbar
Style the navbar links
Style the logo
Links on mouse-over
Style the active/current link
Display some links to the right
Add responsiveness - on screens less than 560px wide, display the navbar vertically instead of horizontally
Internal Style - by using a <style> element in the <head> section
External Style - by using an external CSS file as style.css
Example:
CSS code - shrink navbar / header on scroll
Add CSS: We have styled the header /nav bar links with a background color, adding padding, etc.
* {box-sizing: border-box;}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
#navbar {
overflow: hidden;
background-color: #CCC;
padding: 30px 10px;
transition: 0.3s;
position: fixed;
width: 100%;
top: 0;
z-index: 99;
}
#navbar a {
float: left;
color: black;
text-align: center;
padding: 12px;
text-decoration: none;
font-size: 18px;
line-height: 25px;
border-radius: 4px;
}
#navbar #logo {
font-size: 35px;
font-weight: bold;
transition: 0.4s;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
#navbar a.active {
background-color: dodgerblue;
color: white;
}
#navbar-right {
float: right;
}
@media screen and (max-width: 560px) {
#navbar {
padding: 20px 10px !important;
}
#navbar a {
float: none;
display: block;
text-align: left;
}
#navbar-right {
float: none;
}
}
Related subjects:
Sidebar navigation with scroll bar
How to Create a Sticky Navbar
How to Create Accordion HTML Templates
Shrink Navigation Menu on Scroll - Entire HTML code
How to make the navigation bar disappear after scrolling?. Entire HTML code - example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}#navbar {
overflow: hidden;
background-color: #CCC;
padding: 30px 10px;
transition: 0.3s;
position: fixed;
width: 100%;
top: 0;
z-index: 99;
}#navbar a {
float: left;
color: black;
text-align: center;
padding: 12px;
text-decoration: none;
font-size: 18px;
line-height: 25px;
border-radius: 4px;
}#navbar #logo {
font-size: 35px;
font-weight: bold;
transition: 0.4s;
}#navbar a:hover {
background-color: #ddd;
color: black;
}#navbar a.active {
background-color: dodgerblue;
color: white;
}#navbar-right {
float: right;
}@media screen and (max-width: 560px) {
#navbar {
padding: 20px 10px !important;
}
#navbar a {
float: none;
display: block;
text-align: left;
}
#navbar-right {
float: none;
}
}
</style>
</head>
<body><div id="navbar">
<a href="#default" id="logo">CompanyLogoName</a>
<div id="navbar-right">
<a class="active" href="#home">Home</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
</div><div style="margin-top:210px;padding:15px 15px 2500px;font-size:30px">
<p><b>This example demonstrates how to shrink a navigation bar when the user starts to scroll the page.</b></p>
<p>Scroll down this frame to see the effect!</p>
<p>Scroll to the top to remove the effect.</p>
<p><b>Note:</b> We have also made the navbar responsive, resize the browser window to see the effect.</p>
<p>Lorem ipsum dolor dummy text to enable scrolling, sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><script>
// When the user scrolls down 80px from the top of the document, resize the navbar's padding and the logo's font size
window.onscroll = function() {scrollFunction()};function scrollFunction() {
if (document.body.scrollTop > 80 || document.documentElement.scrollTop > 80) {
document.getElementById("navbar").style.padding = "10px";
document.getElementById("logo").style.fontSize = "20px";
} else {
document.getElementById("navbar").style.padding = "30px 10px";
document.getElementById("logo").style.fontSize = "35px";
}
}
</script></body>
</html>
Tags: How do I shrink a navigation menu on scroll? How do I change my navigation style on scroll? How do I make the navigation bar disappear after scrolling? How do you slide down a bar on scroll? shrink navbar / header on scroll vertical scrolling menu sticky left navigation bar on scroll navbar scroll to section sticky / show navbar on scroll down
Shrink Navigation Menu on Scroll
"Execute" the program in the editor then Shrink Navigation Menu on Scroll. Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0px;
margin-top: 60px;
padding: 0px;
}
nav {
position: fixed;
top: 0px;
width: 100%;
background-color: rgb(39, 39, 39);
overflow: auto;
height: auto;
transition: 0.5s;
padding: 50px 10px;
}
.links {
display: inline-block;
text-align: center;
padding: 14px;
color: rgb(178, 137, 253);
text-decoration: none;
font-size: 17px;
}
.links:hover {
background-color: rgb(100, 100, 100);
}
.selected {
background-color: rgb(0, 18, 43);
}
.sample-content {
height: 150vh;
}
.company-logo {
font-size: 30px;
color: white;
position: absolute;
right: 30px;
transition: 0.3s;
}
</style>
</head>
<body>
<nav>
<a class="company-logo">Logo_Name</a>
<a class="links selected" href="#">Home</a>
<a class="links" href="#"> Login</a>
<a class="links" href="#"> Register</a>
<a class="links" href="#"> Contact Us</a>
<a class="links" href="#">More Info</a>
</nav>
<div class="sample-content">
<h1>Here are some headers</h1>
<h2>Here are some headers</h2>
<h3>Here are some headers</h3>
<h4>Here are some headers</h4>
<h1>Here are some headers</h1>
<h2>Here are some headers</h2>
<h3>Here are some headers</h3>
<h4>Here are some headers</h4>
</div>
<script>
window.onscroll = scrollShowNav;
function scrollShowNav() {
if (
document.body.scrollTop > 20 ||
document.documentElement.scrollTop > 20
)
{
document.getElementsByTagName("nav")[0].style.padding = "10px 10px";
document.querySelector(".company-logo").style.fontSize = "20px";
} else {
document.getElementsByTagName("nav")[0].style.padding = "40px 50px";
document.querySelector(".company-logo").style.fontSize = "50px";
}
}
</script>
</body>
</html>
Summary of description
shrink navbar / header on scroll vertical scrolling menu sticky left navigation bar on scroll navbar scroll to section sticky / show navbar on scroll down
Shrink Navigation Menu on Scroll - how to
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.