Tips for creating a fixed navigation bar that stays at the top of the page when scrolling

Hi there! I've been working on a fun project and could use some assistance. I want to create a fixed navigation bar that stays at the top of the page when scrolling. Can someone please help me with this?

body {
width: 100%;
height: 100%;
margin:0px;
 
}


.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f2f2f2;

}

.navbar li {
float: left;

}

li a, .dropbtn {
display: inline-block;
color: #333;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.navbar li a:hover, .dropdown:hover .dropbtn {
background-color: #333;
color: #f2f2f2;
}

.navbar li.dropdown {
display: inline-block;
}

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

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

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

.navbar .show {display:block;}

.xavierslogo {
 width: 110px;
 height: 109px;
 margin-left: 20%;
 
}

.topbg {
 
 background-color: #333;
}
<header class="topbg">
<img src="xavierslogo.png" alt="St. Xavier's college logo" class="xavierslogo">
</header>


<ul class="navbar">
  <li><a href="#home">Home</a></li>
  <li><a href="#news">Text one</a></li>
   <li><a href="#news">Text Two</a></li>
  <li class="dropdown">
<a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Dropdown ▼</a>
<div class="dropdown-content" id="myDropdown">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
  <a href="#">Link 3</a>
</div>
  </li>

</ul>

<div class="main">
z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z

</div>

Answer №1

insert this code snippet into the specified class

.navbar {
    list-style-type: none;
     overflow: hidden;
   position:fixed;
}

body {
    width: 100%;
    height: 100%;
margin:0px;
 
}
.navbar {
    list-style-type: none;
     overflow: hidden;
   position:fixed;
}
.navbar li {
    float: left;
    
 

}

li a, .dropbtn {
    display: inline-
      block;
    color: #333;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    
}

.navbar li a:hover, .dropdown:hover .dropbtn {
    background-color: #333;
color: #f2f2f2;
}

.navbar li.dropdown {
    display: inline-block;
}

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

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

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

.navbar .show {display:block;}

.xavierslogo {
 width: 110px;
 height: 109px;
 margin-left: 20%;
 
}

.xavierstext {
 width: 100;
 height: 100;
 margin-left: 0%;
 
}

.topbg {
 
       background-color: #333;
 }
<header class="topbg">
<img src="xavierslogo.png" alt="St. Xavier's college logo" class="xavierslogo">
<img src="xavierstext.png" alt="St. Xavier's college logo" class="xavierstext">
</header>
<div>
<ul class="navbar">
  <li><a  href="#home">Home</a></li>
  <li><a href="#news">Text one</a></li>
   <li><a href="#news">Text Two</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Dropdown ▼</a>
    <div class="dropdown-content" id="myDropdown">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>

</ul>
  </div>

<div class="main">


z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can anyone suggest a way to change the orientation of mapped items from column to row?

In my React app, I am creating a keyboard using the following component: Keypad.js const Keypad = () => { const letters = [ 'Q', 'W', 'E', 'R', 'T', ...

Numerous titles being showcased

I need to enhance the functionality of clicking on multiple objects in order to display all titles at once within the header. Currently, only one title is shown each time an object is clicked. Therefore, when clicking on items 1, 2, and 3, I expect to se ...

How can an AngularJS/Ionic mobile application incorporate an external JavaScript script?

Can external JavaScript scripts be executed in an AngularJS/Ionic mobile app, particularly on non-jailbroken iOS devices? We are considering creating a launcher version of our app that downloads the required scripts and then installs and runs them. I have ...

An interactive CSS tutorial platform likened to tryruby.org or codecademy.com for mastering CSS skills

Last year, as I was delving into the world of Ruby, I completed a quick course at TryRuby. It’s an interactive terminal that guides you through executing ruby commands and helps you grasp the language. Similarly, at Codecademy, they challenge you to tac ...

Changing the HTML content of a div in a Web Forms view using HTML passed from the code behind

I am currently working on a project using ASP.NET Web Forms. I have a vertical menu that I want to display on all pages, but with different content depending on the page. My initial approach involved hard coding the menu content in one of my views for simp ...

Using the power of jQuery to load Ajax content, unfortunately, the content remains

Hey there, I'm currently working with an HTML file that utilizes AJAX to load content from other HTML files on the same server. However, for some reason, it's not functioning properly. I'm new to AJAX and I'm not sure what could be caus ...

What is the best way to create mocks in JEST for HTML elements such as webview or iframe?

I'm currently working on writing unit tests using Jest for a React component that displays a <webview>. The webview tag in Electron is based on Chromium's webview, which functions like an iframe. Every time I try to run my tests, I encount ...

Properly handling curves in THREE.JS to ensure efficient disposal

I am facing a challenge with managing memory in my project. I have a dynamic sphere with moving points on it, and I am creating curves to connect these points. Here is an illustration of the process: https://i.sstatic.net/PGWuU.jpg As the points are cons ...

Modify the color of the menu in OpenCart 1.5.6.4 to give it a fresh

In the default theme of OpenCart 1.5.6.4, I am looking to change the background color of the dropdown menu which is currently defined by the background image 'menu.png' to a custom hex value. The CSS code for this section is shown below: #menu & ...

Verify the grid of buttons in my code for the background color

My goal is to make sure that when all the buttons are black, the h2 text in the 'lightsoff' div is displayed. If any buttons are not black, the text will remain hidden. I plan to achieve this by creating a new function that checks the background ...

Extending a type by adding properties from separate files using TypeScript

I am faced with a situation where I have a file containing either a type or interface variable (all of which are exported), and I need to add new properties to this variable from different files without using extends. Essentially, making changes to the sam ...

Encountering an error with Netflix: "Unable to access property 'getAttribute' of null"

Currently, I am working on a project that aims to integrate Netflix with Discord by utilizing rich presence. However, I have encountered an issue while trying to implement the following code snippet: if (type === 'watch' && document.quer ...

Randomly assigning a unique color to each div upon loading the page

I would like to create an effect where the background color of an icon changes randomly to match the color of the text. There are four predefined color choices available for this randomization. Here is the current code I am working with: HTML: <div ...

Leveraging Vue.js's computed properties to access and manipulate elements within an

I have a simple template that displays text from a wysiwyg editor using two-way data binding, as shown below: <template> <div> <quill-editor v-model="debounceText" :options="editorOptionProTemplate" > </qu ...

What is the best method to implement a loading spinner within a React and Node application while using Nodemailer?

Is it possible to implement a functionality where clicking the Send button triggers a spinner next to the button, followed by changing the button text to "Message Sent"? I managed to change the button text, but there is no loading time, it happens instantl ...

How can Angular safely handle interpolating {{ }} in URL parameters?

Dealing with a unique edge case problem here where an external application is in need of a specific URL parameter to be included in the URL request made to our website: https://stackoverflow.com/questions/ask?abc=123 Sharing a screenshot for reference: ...

Tips for utilizing the 'map' function with nested arrays within an array (JSON)

Below is the JSON data provided: users = [{ "name": "alex", "id":123, "surname":"xx", tarriff: { "id":1, "name":"free" } }, { "name": "tom", "id":124, "surname":"henry", tarriff: { "id":1, "name": ...

developing a linked html dropdown menu with only JavaScript

I need help with creating a dependent drop-down list in JavaScript, without using jQuery. The goal is to only display div class option1 when option one is selected, and div class option2 when option two is selected. Below is the code I have so far: ...

Width and left values don't play well with absolute positioning

I am encountering an issue where a div with absolute positioning, which is a child of another absolute positioned element, is not behaving as expected. Despite setting width:100%; left:1px; right:1px on the child element, it extends beyond its parent. < ...

Monitor Jenkins live logs with a personalized dashboard for real-time viewing

Our Jenkins CE is currently running close to 4000 jobs, with users accessing a Dashboard that utilizes Jenkins APIs. Previously, we provided an href link with a logs button for viewing logs, which would open the Jenkins logs page in an iFrame when clicked. ...