1

I am currently trying to make my navbar sticky when I scroll down, however, it isn't working. I have tried to use W3 Schools way and have tried position: sticky on my #nav element. I want my navbar to stay at the top of the screen when scrolling.

#nav {
    width: 100%;
    margin: 0;
    padding-top: 5px;
    text-align: center;
    list-style: none;
    overflow: hidden;
    background-color:rgba(255, 255, 255, 0.171);
}
#nav li {
    display: inline-block;
    text-align: center;
    padding-top:2px;
    text-align: center;
    padding-bottom:8px;
    margin-left:20px;
}
#nav li a {
    display: block;
    text-decoration: none;
    text-align: center;
    font-weight: 600;
    text-align: center;
    color: black;
    font-family: 'Open Sans';
    margin-right:50px;
    margin-left: 35px;
    padding: 8px;
}
#nav li a:hover {
    color: rgb(29, 57, 179);
    text-align: center;
    background-color: rgba(226, 226, 226, 0.637);
    transition: ease-in-out;
    transition-duration: 0.2s;
}
<div id="nav">
        <li><a href="#">Home</i></a></li>
        <li><a href="#">About Us</a></li>
        <img width="50px" height="45px" text-align="center" src="file:///C:/Users/trist/Downloads/camera.svg">
        <li><a href="#">Our Services</a></li>
        <li><a href="#">Contact</a></li>
    </div>
Makyen
  • 31,849
  • 12
  • 86
  • 121
Tristan N
  • 37
  • 6
  • Try reading this: (https://stackoverflow.com/questions/43707076/position-sticky-not-working-css-and-html/47878455) – Ryan Wilson Jul 14 '20 at 01:11

2 Answers2

1

Try this when you scroll down you nav will remains at the top

body {
  height: 500vh;
}

#nav {
  position: fixed;
  width: 100%;
  margin: 0;
  padding-top: 5px;
  text-align: center;
  list-style: none;
  overflow: hidden;
  background-color: rgba(255, 255, 255, 0.171);
}

#nav li {
  display: inline-block;
  text-align: center;
  padding-top: 2px;
  text-align: center;
  padding-bottom: 8px;
  margin-left: 20px;
}

#nav li a {
  display: block;
  text-decoration: none;
  text-align: center;
  font-weight: 600;
  text-align: center;
  color: black;
  font-family: 'Open Sans';
  margin-right: 50px;
  margin-left: 35px;
  padding: 8px;
}

#nav li a:hover {
  color: rgb(29, 57, 179);
  text-align: center;
  background-color: rgba(226, 226, 226, 0.637);
  transition: ease-in-out;
  transition-duration: 0.2s;
}
<div id="nav">
  <li><a href="#">Home</i></a></li>
  <li><a href="#">About Us</a></li>
  <img width="50px" height="45px" text-align="center" src="file:///C:/Users/trist/Downloads/camera.svg">
  <li><a href="#">Our Services</a></li>
  <li><a href="#">Contact</a></li>
</div>
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
1

You can do this:

#nav {
  position: fixed;
  top: 0;
  z-index: 99;
}

You would just need to lose the opacity on the navbar and offset the container following the navbar from the top a bit or it'll look like it's overlapping/smashed together.

doctypecode
  • 101
  • 5