Hi I am creating a website and i want to make the navbar sticky but when I set the position to fixed a whitespace of about 40px is created on top of webpage.
I tried setting position to sticky and it works fine. I also inspected the page and found out that the whitespace is html tag.I have provided the code and images below.
const body = document.body;
let lastScroll = 0;
window.addEventListener('scroll', function() {
const currentScroll=(window.scrollY);
if(currentScroll<=0){
body.classList.remove('scroll-up');
}
if(currentScroll>lastScroll && !body.classList.contains('scroll-down')){
body.classList.remove('scroll-up');
body.classList.add('scroll-down');
}
if(currentScroll<lastScroll && body.classList.contains('scroll-down')){
body.classList.remove('scroll-down');
body.classList.add('scroll-up');
}
lastScroll=currentScroll;
});
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: 'Poppins', sans-serif;
background-color: #fff;
color: #fff;
}
nav{
background-color: transparent;
z-index: 1000; /* This will make sure the navbar is always on top of other elements */
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 100px;
text-transform: uppercase;
user-select: none;
transition: all 300ms ease-in-out;
position: fixed;
top:0;
width:100%
}
nav>a{
cursor: default;
}
nav li{
list-style: none;
display: inline;
}
nav a, .hero-btn{
color: #fff;
text-decoration: none;
padding: 10px 15px;
transition: 200ms;
}
nav a:hover{
color: grey;
}
.hero-btn:hover{
background-color: var(--btn-hvr-color);
}
.logo{
background-image: url(https://i.imgur.com/EKJALav.png);
background-size: cover;
height: 150px;
width: 200px;
}
main{
background: url('https://i.imgur.com/uMgayfk.png') no-repeat;
background-size: cover;
height:100vh;
background-attachment: fixed;
}
h1{
font-size: 70px;
font-family: "Roboto", sans-serif;
font-weight: 700;
font-style: normal;
}
.content{
margin-top: 50px;
margin-left: var(--text-margin);
display: flex;
flex-direction: column;
justify-content:flex-start;
flex-wrap: wrap;
gap: 15px;
}
.btn{
width: 150px;
height: 50px;
border: none;
outline: none;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
user-select: none;
cursor: pointer;
}
.hero-btn{
background-color: var(--primary-color);
font-family: 'Poppins', sans-serif;
font-size: 1rem;
font-weight: 600;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
transition: 300ms;
}
.highlited-txt{
color: #F8b122;
}
.content p{
text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
font-weight: 400; /* Increase as needed */
font-size: 1.1em; /* Increase as needed */
line-height: 1.5em; /* Adjust for readability */
}
}
.scroll-down nav{
transform: translate3d(0, -100%, 0);
}
.scroll-up nav a{
color: black;
transition: 300ms;
}
.scroll-up nav a:hover{
transition: 300ms;
color: rgb(101, 98, 98);
}
<main>
<nav>
<a href="https://www.google.com"><div class="logo">
</div></a>
<ul class="nav-links">
<li><a id="scroll-link" href="main">Home</a></li>
<li><a id="scroll-link" href="#about">About</a></li>
<li><a id="scroll-link" href="#products">Products</a></li>
<li><a id="scroll-link" href="#contact">Contact</a></li>
<li><a id="scroll-link" href="./blog.html">Blog</a></li>
</ul>
<!-- <div class="burger">
<div class="line line1"></div>
<div class="line line2"></div>
<div class="line line3"></div>
</div> -->
</nav>
<div class="content">
<h1>Powering Your <span class="highlited-txt">Future</span></h1>
<p>Elevate your energy strategy with our cutting-edge solar inverters and lithium batteries, designed <br>to deliver sustainable, reliable power for homes, businesses, and industrial applications.</p>
<a href="#" class="btn hero-btn">Learn More</a>
</div>
</main>
2
Answers
Add the height property to the nav element.
Add padding-top to the body or main element to account for the
navbar’s height.
see screenshot
[nav {
height: 60px; /* Adjust this value */
}
main {
padding-top: 60px; /* Adjust this value */
}]1