Sticky Navigation Menu with Smooth Scrolling

Sticky Navigation Menu With Smooth Scrolling
Code Snippet:Sticky Nav with Smooth Scrolling and Progress Bar - CSS
Author: Josetxu
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 924
License: MIT
Edit Code online: View on CodePen
Read More

This HTML & CSS code snippet helps you to create a sticky navigation menu with smooth scrolling to sections on click. The menu highlights when you hover over its links. Similarly, it shows a scrolling progress bar inside the menu’s items on the scroll event.

Whether you are working on a landing page or developing a single-page web app, this navigation menu is quite suitable for your needs. It is purely designed in CSS including its functionality. So, you can highly customize it according to your project’s design requirements with additional CSS.

How to Create a Sticky Navigation Menu with Smooth Scrolling

1. First, create the basic HTML structure for your webpage. You will need sections for different content and a navigation menu. Copy the following HTML code and replace the content inside <section> tags with your actual webpage content.

<header>
	<nav>
		<a href="#css">CSS</a>
		<a href="#html">HTML</a>
		<a href="#js">JS</a>
		<a href="#php">PHP</a>
		<a href="#sql">SQL</a>
	</nav>
</header>

<div class="scroller">
	<span></span>
	<span></span>
</div>

<section id="css">
	<h2>CSS</h2>
	<p>Cascading Style Sheets</p>
</section>

<section id="html">
	<h2>HTML</h2>
	<p>HyperText Markup Language</p>
</section>

<section id="js">
	<h2>JS</h2>
	<p>Client-Side Scripting Language</p>
</section>

<section id="php">
	<h2>PHP</h2>
	<p>Hypertext Preprocessor</p>
</section>

<section id="sql">
	<h2>SQL</h2>
	<p>Structured Query Language</p>
</section>

2. In your CSS file (styles.css), add the following code to make the navigation menu stylish and functional. You can customize the color scheme for the navigation menu by updating the color values defined in the root variables.

@import url('https://fonts.googleapis.com/css2?family=Varela+Round&display=swap');

:root {
	--black: #373737;
	--sect1: #ff9900;
	--sect2: #2196F3;
	--sect3: #e63232;
	--sect4: #8BC34A;
	--sect5: #8946ff;
}

* { 
	box-sizing: border-box; 
}

.cd__intro{
   margin-top: 65px;
}

html {
	scroll-behavior: smooth;
}

body {
	margin: 0;
	padding: 0;
	transition: all 0.5s ease 0s;
	font-family: 'Varela Round', cursive;
}

body:after {
	content: "Progress Bar only in Chrome";
	position: fixed;
	bottom: 0;
	width: 100%;
	text-align: center;
	color: var(--black);
	font-size: 1rem;
	padding: 20px 0;
}

header {
	width: 100%;
}

nav {
	top: 0;
	left: 0;
	position: fixed;
	width: 100%;
	height: 65px;
	background: linear-gradient(0deg, #fff0 0px, var(--black) 1px 100%);
	text-align: center;
	display: flex;
	transition: all 0.5s ease 0s;
}

nav a {
	color: #fff;
	text-decoration: none;
	padding: 16px 10px;
	position: relative;
	z-index: 1;
	width: 100%;
	font-size: 1.6rem;
	border: 2px solid #000;
	border-top-width: 0px;
	border-bottom-width: 4px;
}

nav a:first-child {
	border-left-width: 0;
}

nav a:last-child {
	border-right-width: 0;
}

nav a:hover {
	background: linear-gradient(90deg, var(--clr) 0 50%, var(--black) 0 100%);
	background-size: 200%;
	background-position: 100% 0;
	animation: colorize 0.5s ease 0s;
	animation-fill-mode: forwards;
	transition: all 0.5s ease 0s;
}

@keyframes colorize {
	100% { background-position: 0 0; }
}

nav a:nth-child(1):hover {
	--clr: var(--sect1);
}
nav a:nth-child(2):hover {
	--clr: var(--sect2);
}
nav a:nth-child(3):hover {
	--clr: var(--sect3);
}
nav a:nth-child(4):hover {
	--clr: var(--sect4);
}
nav a:nth-child(5):hover {
	--clr: var(--sect5);
}


nav:after {
	content: "";
	position: absolute;
	bottom: 0;
	left: 0;
	width: 20vw;
	width: 20%;
	height: 100%;
	z-index: -1;
	background: linear-gradient(90deg, var(--sect1) 0 calc(20vw - 3px), var(--sect2) 0 calc(40vw - 6px), var(--sect3) 0 calc(60vw - 10px), var(--sect4) 0 calc(80vw - 13px), var(--sect5) 0 100vw);
	animation: progress linear;
	animation-timeline: scroll(root);
}

@keyframes progress {
	100% { width: 100%; }
}



/*** SECTIONS ***/
section {
	height: 100vh;
	justify-content: center;
	align-items: center;
	font-size: 4rem;
	color: #fff;
	padding-top: 30px;
	border-bottom: 4px solid #000;
	display: flex;
	flex-wrap: wrap;
	align-content: center;
}

section h2, section p {
	margin: 0;
	width: 100%;
	text-align: center;
}

section p {
	font-size: 1.25rem;
}

#css {
	background-color: var(--sect1);
}

#html {
	background-color: var(--sect2);
}

#js {
	background-color: var(--sect3);
}

#php {
	background-color: var(--sect4);
}

#sql {
	background-color: var(--sect5);
}



/*** SCROLLER ***/
.scroller {
	position: fixed;
	width: 30px;
	height: 50px;
	background: radial-gradient(ellipse at 50% 33%, #fff0 0 3px, #fff 4px 100%);
	top: calc(50vh - 15px);
	right: 20px;
	border-radius: 20px;
}

.scroller:before {
	content: "";
	position: absolute;
	width: 30px;
	height: 50px;
	background: radial-gradient(circle at 50% 33%, #fff0 0 6px, #fff 7px 100%);
	border-radius: 20px 20px;
	left: 0;
	bottom: 0;
}

.scroller span {
	position: absolute;
	height: 30px;
	border-left: 5px dotted #fff;
	top: -50px;
	left: 13px
}

.scroller span + span {
	top: inherit;
	top: 70px;
}

.scroller span:before {
	content: "";
	position: absolute;
	width: 8px;
	height: 8px;
	border: 4px solid #fff;
	border-color: #fff #fff #fff0 #fff0;
	border-radius: 4px;
	transform: rotate(-45deg);
	left: -11px;
	top: -13px;
}

.scroller span + span:before {
	border-color: #fff0 #fff0 #fff #fff ;
	top: 28px;
}

.scroller:after {
	content: "Scroll";
	position: absolute;
	left: -5rem;
	font-size: 1.65rem;
	bottom: 0.5rem;
	color: #fff;
	font-weight: bold;
}

@media screen and (max-width: 580px) {
	nav a { font-size: 1.25rem; line-height: 2rem; }
	.scroller:after { content: "";}
}

That’s all! Hopefully, you’ve successfully created a top fixed navigation menu with smooth scrolling for your website. Feel free to customize the colors, styles, and content to match your project’s design. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X