Sticky Navbar Using HTML CSS And JavaScript
Hey, everyone today we will learn how to create a sticky navbar using HTML CSS, and javascript. It’s constructive to project a lot of websites using the Sticky navbar.
Title: Making a Sticky Navbar Utilizing HTML, CSS, and JavaScript
In modern internet design, a sticky navigation bar is a well-liked consumer interface factor that is still fastened on the prime of the web page whereas scrolling. This sticky conduct ensures that customers have quick access to the navigation menu, no matter their place on the webpage.
In this article, we are going to discover the way to create a sticky navbar utilizing HTML, CSS, and JavaScript, enhancing consumer expertise and offering a smooth and intuitive navigation system.
Earlier than we dive into the implementation particulars, let’s briefly perceive the idea behind a sticky navbar. The thought is to make use of CSS to initially place the navigation bar on the prime of the web page. Then, because the consumer scrolls, JavaScript comes into play so as to add or take away a CSS class, which updates the positioning of the navbar, making it keep on with the highest of the viewport.
To get began, let’s create the HTML construction for our navbar. The construction sometimes consists of an outer container, resembling a <header>
tag, with an inside container for the navigation hyperlinks, normally contained inside an unordered listing (<ul>
) and listing objects (<li>
).
<nav>
<div class="nav_content">
<div class="logo">
<a href="#">OnlineITtuts</a>
</div>
<ul class="nav_links">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Skills</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>
As soon as the HTML construction is in place, we will apply CSS types to create the specified look for our navbar. This consists of setting the background shade, textual content shade, font dimension, and other visible enhancements you need.
You can even experiment with numerous CSS properties to attain a visually interesting design that aligns together with your website’s total feel and appearance.
@import url('https://fonts.googleapis.com/css2?family=Pacifico&family=Poppins&family=Ubuntu:wght@400;500&display=swap');
*{
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Ubuntu",sans-serif;
}
a{
text-decoration: none;
}
nav{
position: fixed;
left: 0;
top: 0;
width: 100%;
padding: 20px;
}
nav.sticky{
background: #133337;
}
nav .nav_content{
height: 100%;
max-width: 1440px;
margin: auto;
display: flex;
justify-content: space-between;
align-items: center;
}
nav .nav_content .logo a{
font-size: 35px;
font-weight: 500;
color: #fff;
}
nav .nav_content .nav_links{
display: flex;
align-items: center;
}
nav .nav_links li{
list-style: none;
margin: 0 8px;
}
nav .nav_links a{
font-size: 18px;
font-weight: 500;
color: #fff;
/* background: red; */
padding: 10px 15px;
transition: all 0.3s ease;
}
nav .nav_links a:hover{
color: #979fa0;
}
To make the navbar sticky, we are going to make the most of JavaScript to detect the scrolling conduct of the consumer. When the consumer scrolls past a sure level, we are going to add a CSS class to the navbar factor, which can replace its positioning utilizing CSS properties resembling place: fastened;
.
Conversely, when the consumer scrolls again to the highest, we are going to take away the CSS class, reverting the navbar to its preliminary place.
const nav = document.querySelector('nav');
window.addEventListener("scroll",function(){
if(document.documentElement.scrollTop > 20){
nav.classList.add("sticky");
}
else{
nav.classList.remove("sticky");
}
})
After implementing the sticky navbar, it’s important to completely check its performance and look through completely different units and browsers. Ensure that the navbar stays responsive and visually interesting throughout numerous display sizes.
You May Also Like:
- Responsive Footer Design Using HTML and CSS
- How to Make Progress Bar in HTML and CSS
- Responsive Landing Page Using HTML and CSS
- Create a Responsive Timeline in HTML and CSS
- Responsive Sidebar Menu in HTML CSS and JavaScript
Moreover, think about refining the conduct and design as per consumer suggestions or your personal preferences.
Sticky Navbar Using HTML CSS And JavaScript
Sticky navbar or fixed navbar are the same thing, I’ve made the complete tutorial on it. Inside that, I’ve explained all of the things from scratch step by step. So, watch the complete video tutorial.
after watching the complete video tutorial, hope you’ve learned everything that you want to learn also you’ve got many ideas from it. Hope this tutorial is helpful and beneficial for you.
Conclusion:
In conclusion, making a sticky navbar utilizing HTML, CSS, and JavaScript can considerably improve consumer expertise in your website. It permits customers to navigate effortlessly without having to scroll again to the highest of the web page.
By implementing the steps outlined in this article, you’ll be able to obtain a chic and purposeful navigation system that contributes to seamless-looking expertise in your website guests.