0

want to ask. How do I stick my nav bar at the top when scrolling down? I have searched for some tutorial but I don't know how to combine the code together with mine. When I added the entire nav bar will go out of place.

Below is my style code for the nav bar:

<style>
.navbar {
 overflow: hidden;
 background-color:#333;
 }

.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.dropdown {
float: left;
overflow: hidden;
}

.dropdown .dropbtn {
font-size: 16px;  
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}

.dropdown-content {
 display: none;
 position: absolute;
 background-color: #f9f9f9;
 min-width: 160px;
 box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
 z-index: 1;
 }

 .dropdown-content a {
 float: none;
 color: black;
 padding: 12px 16px;
 text-decoration: none;
 display: block;
 text-align: left;
 }

 .dropdown-content a:hover {
  background-color: #ddd;
  }

  .dropdown:hover .dropdown-content {
  display: block;
  }
  </style>
Ng Wei Shen
  • 75
  • 11

2 Answers2

3

Here is a related question which might be able to help: Make a nav bar stick to the top when scrolling with css

Basically you need to set something like this:

.navbar-fixed {
    top: 0;
    z-index: 100;
    position: fixed;
    width: 100%;
}
Nathan
  • 90
  • 7
-2

It sounds like you're looking for a Sticky Navbar ("sticks" to the top of the viewport). Are you using Bootstrap? If so check out Bootstrap's NavBar Documentation.

DaveVanFleet
  • 573
  • 3
  • 10