My HTML elements are not displaying in the correct order. Any suggestions on how to resolve

Hi, I am currently working on a website using HTML, CSS, and JS. I have encountered a small issue in the HTML section.

Here is the code snippet:

window.onscroll = function() {
  changeOnScroll()
};

function changeOnScroll() {
  var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
  var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
  var scrolled = (winScroll / height) * 100;
  document.getElementById("myBar").style.width = scrolled + "%";
}
<!DOCTYPE html>

<html>

<head>
  <title>Progress bar and nav bar</title>
  <style>
    h1 {
      background-color: cyan;
    }
    /* The navigation bar */
    
    .navbar {
      overflow: hidden;
      background-color: #333;
      position: fixed;
      top: 0;
      width: 100%;
    }
    
    .navbar a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }
    
    .navbar a:hover {
      background: #ddd;
      color: black;
    }
    
    .main {
      /* Avoiding overlay */
      margin-top: 80px;
    }
    
    .header {
      position: fixed;
      top: 0;
      z-index: 1;
      width: 100%;
      background-color: #f1f1f1;
    }
    /* The progress container */
    
    .progress-container {
      width: 100%;
      height: 8px;
      background: #ccc;
    }
    /* The progress bar */
    
    .progress-bar {
      height: 8px;
      background: #04AA6D;
      width: 0%;
    }
  </style>
</head>

<body>


  <div class="navbar">
    <a href="#home">Home</a>
    <a href="#news">Gallery</a>
    <a href="#contact">About</a>
  </div>

  </div>

  <div class="header">
    <div class="progress-container">
      <div class="progress-bar" id="myBar"></div>
    </div>
  </div>

  <div class="main">
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>
    <h1>A progress bar and a menu.</h1>

  </div>
</body>

</html>

Output:

Upon inspecting the code, it is clear that the progress bar is displayed above the nav bar in the output, even though in the code the nav bar div appears first. I am seeking a solution to have the nav bar appear above the progress bar. Any suggestions on how to achieve this?

I have tried adjusting the positions, but to no avail. Any help would be greatly appreciated.

Thank you!

Answer №1

By adjusting the CSS properties of both the .header and .navbar elements to include position: fixed and top: 0px, I was able to solve the issue partially by setting the top value of the .header to 40px. Although this may not be the most conventional approach, it effectively resolves the problem for now.

window.onscroll = function() {
  updateOnScroll()
};

function updateOnScroll() {
  var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
  var docHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
  var scrollPercentage = (winScroll / docHeight) * 100;
  document.getElementById("myBar").style.width = scrollPercentage + "%";
}
h1 {
  background-color: cyan;
}

/* Styling for the navigation bar */
.navbar {
  overflow: hidden;
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.navbar a:hover {
  background: #ddd;
  color: black;
}

.main {
  margin-top: 80px; /* To prevent overlap */
}

.header {
  position: fixed;
  top: 40px;
  z-index: 1;
  width: 100%;
  background-color: #f1f1f1;
}

/* Styling for the progress bar container */
.progress-container {
  width: 100%;
  height: 8px;
  background: #ccc;
}

/* Styling for the progress bar */
.progress-bar {
  height: 8px;
  background: #04AA6D;
  width: 0%;
}
<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">Gallery</a>
  <a href="#contact">About</a>
</div>
<div class="header">
  <div class="progress-container">
    <div class="progress-bar" id="myBar"></div>
  </div>
</div>

<div class="main">
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>
  <h1>A progress bar and a menu.</h1>

</div>

Answer №2

Both the .navbar and .header elements are set to position: fixed; and top: 0;, causing them to stack on top of each other at the top of the viewport.

To properly position the progress bar, you will need to adjust the top value in the .header class to achieve the desired result.

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

Steps for showcasing each element of an array separately upon a mouse click

I am working on a website where each click on the screen reveals a new paragraph gradually. I plan to organize these paragraphs in an array and display them one after the other in sequential order upon user interaction. The challenge lies in combining thes ...

What is the best way to transform an array of arrays into an array of objects using AngularJS?

Here is some code I am working on: $scope.students=[]; $scope.students[[object Object][object Object]] [0]{"id":"101","name":"one","marks":"67"} [1]{"id":"102","name":"two","marks":"89"} I would like to reformat it into the ...

The Proper Method of Displaying Data from a JSON File Using AngularJS

I'm having trouble getting the data from a JSON file (click on the "Json File" link to view the structure). I'm not sure what to put after "$Scope.listOfRecipe=". I tried using response.data.recipes, but it's not working and causing errors. ...

The password prompt window refuses to align with the center of the screen. This

I've been struggling to position the Javascript popup notification using the width, height, top, and left parameters in the window.open function. No matter what I attempt, it stubbornly remains in the top-left corner. Can someone offer some guidance o ...

Is there a way to prevent the conversion of .json/.webmanifest URLs into base64 strings?

I've been grappling with an issue for quite some time now. In my VSCode vite-react-typescript project, I have a link in the index.html file pointing to a webmanifest, which is essentially a json file with a different extension. After building my app, ...

Is it possible to add additional text to an input field without modifying its existing value?

I have a numerical input field labeled "days" that I want to add the text " days" to without altering the actual numerical value displayed. <input type="number" class="days" (keyup)="valueChanged($event)"/> Users should only be able to edit the num ...

Merging text and a JSON object to retrieve the information

Having some trouble with a JSON object and retrieving values. This is the syntax that works for getting the data I need. dataJSON.companies[0].fields.Internet.length I want to dynamically evaluate the object using a string variable, like this... var me ...

Store the text area content as a JSON object

What is the best way to store the content of a textarea in JSON format? I am currently working on a project where I have a textarea element and I need to save its value into a JavaScript object. Everything is functioning correctly except when 'enter ...

What is the best way to access the element before and after a specific element in an array using JavaScript?

I need to create a function that accepts a string input, searches for its match in an array, and then returns the previous and next elements in the array. const array = [ "11111", "22222", "343245", "5455", "34999", "34 ...

Implementing a messaging feature with inbox functionality in a Node.js web application

In my node.js application, I am looking to incorporate a send message and inbox feature. The website focuses on job postings within a forum, catering to freelancers, mentors, and clients. To facilitate communication between these three types of users, I ...

Modifying the label focus does not alter the CSS style

I'm struggling with a simple form where I want to highlight the focused labels by changing their background colors. However, the jquery code doesn't seem to be working as expected. Despite no errors showing up in the console, the functionality is ...

Is there a way to effortlessly refresh the WooCommerce cart and receive updated HTML content all in one go?

I am currently working on a customized "mini-cart" that mimics the functionality of the main cart page, including options to adjust quantity, remove items, enter coupons, and remove coupons. I have set up the cart to submit changes via Ajax by listening fo ...

Fill a JavaScript array with values inserted in the middle position

I'm having trouble populating an array: var arr2 = ["x", "y", "z"]; I want to insert a random number/value between each original value using a for loop, like this: for (let i = 1; i < arr2.length; i+2) { arr2.splice(i, 0, Math.random()); } B ...

Exploring the possibilities of combining Selenium Code and F# with Canopy

Currently, I am facing the challenge of incorporating Selenium code into my F# project while utilizing the canopy wrapper. Canopy relies on Selenium for certain functions. My main struggle lies in converting Selenium code from Java or C# to fit within an ...

onpageshow event behaves as expected exclusively on webkit browsers (triggers function solely when visible on the screen)

I am inserting an HTML file using an object tag. The encompassing div of the object tag is hidden with a display:none property. However, I encounter an issue where the onpageshow event that I placed on the body tag of the HTML object behaves differently a ...

How can I use PHP to transform a JSON object containing xy coordinates into an image?

What is the best way to convert a JSON object containing xy coordinates into an image (PNG or JPG) using PHP or JavaScript? ...

Encountered a error 500 while attempting to POST using Postman in Node.js

After successfully setting up the server, I encountered an issue when attempting to create a user using Postman with the details in Application/JSON format. The server responded with: POST /sign-up 500. Initially, I tried specifying utf-8 without succes ...

Position the select tag adjacent to the textbox

Looking for assistance on how to arrange the <select> tag beside the "Desired Email Address" field within my email registration form. I believe a modification to the CSS code is necessary. Below you will find the HTML and CSS (snippet) related to th ...

Implementing dynamic content loading using CSS and jQuery upon link/button activation

I'm facing an issue where I am attempting to align content to the left side of my screen to display a feed while also ensuring that the pushed content is responsive. Unfortunately, the feed is not visible and the content remains unresponsive. Is ther ...

Node.js: retrieving a webpage and waiting for it to load before extracting data

Is it possible to scrape a webpage using just node.js without relying on other libraries like phantom.js? That is the question I have been pondering lately. In my code below, I am requesting a webpage with request and then using cheerio to extract data by ...