What causes the CSS Position Fixed Property to fail?

Can someone help me fix my navbar so that it remains fixed when I scroll? Currently, it moves up as I scroll down. Below are the source codes I'm using:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
h1 {
  margin: 1rem;
}

ul {
  position: fixed;
  width: 100vw;
  padding-top: 5px;
  list-style-type: none;
  background-color: #333333;
  height: 40px;
}

li {
  display: block;
  float: left;
  font-size: 1.25rem;
}

a {
  text-decoration: none;
  color: white;
  padding: 12px;
}

a:hover:not(.home) {
  background-color: #111111;
}

.home {
  background-color: #04aa6d;
}

main {
  position: absolute;
  top: 40px;
  background-color: #1abc9c;
  width: 100vw;
}
<body>
    <header>
      <ul>
        <li><a class="home" href="">Home</a></li>
        <li><a href="">News</a></li>
        <li><a href="">Contact</a></li>
        <li><a href="">About</a></li>
      </ul>
    </header>
    <main>
      <h1>Fixed Top Navigation Bar</h1>
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear a story?</p>
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear...;

I need help fixing the header/navbar content to remain fixed while scrolling. I'm new to HTML/CSS, so detailed explanations would be greatly appreciated. Thanks, Abdul Alim

Answer №1

your code is technically correct, but the issue lies in the fact that the navbar is positioned behind another element, causing it to be covered up

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
h1 {
  margin: 1rem;
}

ul {
  position: fixed;
  width: 100vw;
  padding-top: 5px;
  list-style-type: none;
  background-color: #333333;
  height: 40px;
  z-index: 99; /* you can add z-index dinavbar */
}

li {
  display: block;
  float: left;
  font-size: 1.25rem;
}

a {
  text-decoration: none;
  color: white;
  padding: 12px;
}

a:hover:not(.home) {
  background-color: #111111;
}

.home {
  background-color: #04aa6d;
}

main {
  position: absolute;
  top: 40px;
  background-color: #1abc9c;
  width: 100vw;
}
<body>
    <header>
      <ul>
        <li><a class="home" href="">Home</a></li>
        <li><a href="">News</a></li>
        <li><a href="">Contact</a></li>
        <li><a href="">About</a></li>
      </ul>
    </header>
    <main>
      <h1>Fixed Top Navigation Bar</h1>
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear a story?</p>
      <!-- repeated paragraph for demonstration purposes -->
      <!-- more paragraphs... -->
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear a story?</p>
    </main>
  </body>

Answer №2

Make sure to set the z-index property for the < ul > element in your code. Since your header is fixed at the top, it may be hidden behind the < main > section when you scroll down. To avoid this issue, simply add a high z-index value to the < ul >.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
h1 {
  margin: 1rem;
}

ul {
  position: fixed;
  width: 100vw;
  padding-top: 5px;
  list-style-type: none;
  background-color: #333333;
  height: 40px;
  z-index: 99;
}

li {
  display: block;
  float: left;
  font-size: 1.25rem;
}

a {
  text-decoration: none;
  color: white;
  padding: 12px;
}

a:hover:not(.home) {
  background-color: #111111;
}

.home {
  background-color: #04aa6d;
}

main {
  position: absolute;
  top: 40px;
  background-color: #1abc9c;
  width: 100vw;
}
  <body>
    <header>
      <ul>
        <li><a class="home" href="">Home</a></li>
        <li><a href="">News</a></li>
        <li><a href="">Contact</a></li>
        <li><a href="">About</a></li>
      </ul>
    </header>
    <main>
      <h1>Fixed Top Navigation Bar</h1>
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear a story?</p>
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear a story?</p>
      [...]
      <p>Abdul Alim loves to cultivate crops and travel. Wanna hear a story?</p>
    </main>
  </body>

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

Attempting to create a footer design featuring buttons aligned to the left and right sides

I am trying to create a layout similar to the bottom of Google's homepage using this code. However, no matter what I try, the "lists" are still appearing vertically instead of horizontally. My goal is to have three columns of links positioned side by ...

I'm facing a challenge with getting the 'extend' feature to function correctly while using Flask in Python

As someone who is just starting out in programming, I recently decided to embark on creating a web app using flask, python, and HTML. To start off, I set up three essential files - two HTML files, and one python file. The first HTML file requiring informa ...

"Despite being higher in position, the z-index is causing the element to be placed below

I am currently facing an issue with a dropdown menu that I am trying to add to a WordPress site using Visual Composer. The problem arises when I attempt to place the dropdown on top of a parallax section below it. Despite setting the z-index of the paralla ...

When working with NodeJS and an HTML form, I encountered an issue where the 'Endpoint'

Having trouble sending input data from a form to my nodejs endpoint. When I try printing the req.body, it shows up as undefined and I can't figure out why. Here is the relevant API code snippet: var bodyParser = require('body-parser') var e ...

Failure to append an object to the array occurs when submitting an HTML form

I am facing an issue with a form that is supposed to add input values to an array as objects when submitted. However, every time I submit the form, the console briefly shows that there is an array with one object, only for it to disappear. Below is the f ...

AngularJS fails to fetch data from API queries

Currently facing a challenge with retrieving API data in my AngularJS project. The code below successfully fetches the data and logs it on the console as an array, but I am unable to access and utilize it in the front-end. Please note that the placeholder ...

Issues are arising with Bootstrap's functionality within the Vite React app

I am new to working with react and vite. I am currently developing a vite react application that requires the use of bootstrap. I followed the instructions provided on the official Bootstrap website here. However, not all Bootstrap functionalities are work ...

Iframe navigation tracking technology

Let's say I have an iframe element in my HTML document. <html> <body> This is my webpage <button> Button </button> <iframe src="www.example.com"></iframe> </body> </html> If a user clicks on links wi ...

Are there any special CSS hacks that only work for IE8?

When it comes to Internet Explorer 8 (IE8), my preference is to utilize the following CSS: color : green; I am looking to implement a hack that exclusively impacts IE8, without affecting IE9, IE6, and 7. ...

Assign a class while directing visitors away from a particular webpage

Can a div have a class added on page load, but only when redirected from a specific link or page? I currently have an <a href="marketing.php" id="openMyMaterialTab"><button class="secondaryAction">See all</button></a> link on my la ...

The positioning problem arising from using a Vuetify select dropdown within a datatable

Vuetify datatable is being used with 20 columns, functioning properly. To view all the columns, horizontal scrolling is available. Filters are needed for each column in the header, achieved using v-slot:header. An issue arises when clicking on the select ...

For Flexbox front end design, the left side should be contained within a container while the right side should seamlessly stretch across the

I'm facing a challenge in designing a website with a unique layout that has left me puzzled. I've created a codepen example to showcase what I'm trying to achieve. Specifically, I need to implement a Hero Banner on top of a standard 1200px ...

Using BeautifulSoup4 in Python to extract text data from a nested tag within an h1 element

Seeking assistance in extracting the text content within an anchor tag that is nested inside a heading tag. <h1 class="branded-page-header-title"> <span class="qualified-channel-title ellipsized"><span class="qualified-channel-title-w ...

What is the best way to retrieve a particular div element from a webpage?

I'm having trouble retrieving a specific div element (specifically with the attribute id="vung_doc") from a website. Instead of getting just that element, I seem to be getting almost every element on the page. Any ideas on what could be causing this i ...

The HTML5 webpage is failing to send a JQuery Ajax request

I am encountering an issue with my Ajax setup on a HTML page. The call to a web service does not seem to be working as intended, and I suspect that the ajax call is not even reaching the web service at all. The problem arises when I click the button, an e ...

The correct method for implementing server-side rendering with JavaScript

My website consists of multiple pages and utilizes React as a widget toolkit, with different bundles on each page. I am facing an issue where some widget state persists within a login session, and when the user revisits the page, the components should relo ...

Combining HTML, CSS, JAVASCRIPT, and PHP for a seamless web development experience

As a beginner in web development, I have some knowledge of javascript, html, css and am currently delving into php. However, I am struggling to find comprehensive resources that show how to integrate all these languages together. I would greatly appreciat ...

Issues with Jquery/AJAX function not responding to onChange Event

I am currently working on a project where I need to populate a dropdown menu based on the value selected from another dropdown. However, I am encountering an issue with the function that I am trying to call in the onChange event of the select tag. This is ...

Controlling the Flow of Events in JavaScript Documents

Explore the integration of two interconnected Javascript files and delve into managing event propagation between them effectively. 1st js file var red = [0, 100, 63]; var orange = [40, 100, 60]; var green = [75, 100, 40]; var blue = [196, 77, 55]; var ...

Elevate the index of the name attribute for an input field within a dynamically generated form

In my scenario, I have created a form that includes indexed input fields for username and level: <form> <table> <tr class="trToClone"> <td> <label>Username:</label> <input type="text" name="usernam ...