Position the navigation bar with Absolute and Sticky positioning for easy access and fixed

I'm in the process of creating a navigation bar that overlaps my header and remains fixed at the top of the page when scrolling.
Initially, it will be positioned at top: 45px and then switch to top: 0 upon scrolling.

My initial approach involved setting it to position: fixed; top: 45px and adjusting the value using JavaScript on a scroll event. However, I encountered a warning from Firefox regarding "asynchronous panning" which was discussed in this thread.

I managed to achieve the desired effect using some CSS tricks, but I am curious if there is a simpler CSS solution or a valid JavaScript method to accomplish this without triggering a warning.

body {
  position: relative;
  height: 100%;
  width: 100%;
  background-color: grey;
  overflow-x: hidden;
  margin: 0;
}

.container {
  position: absolute;
  top: 0;
  left: -1px;
  width: 1px;
  bottom: 0;
  padding-top: 45px;
  overflow: visible;
}

nav {
  position: sticky;
  top: 0;
  transform: translateX(-50%);
  margin-left: 50vw;
  width: 300px;
  height: 70px;
  background-color: red;
}

header {
  height: 50vh;
  background-color: blue;
}

main {
  height: 200vh;
  width: 100%;
  background-color: green;
}
<div class="container">
  <nav></nav>
</div>
<header>
</header>
<main>
</main>

Answer №1

You can enhance your code by streamlining it and eliminating the need for an additional container:

body {
  background-color: grey;
  margin: 0;
}

nav {
  position: sticky;
  top: 0;
  width: 300px;
  height: 70px;
  margin:45px auto -115px; /* 115 = height + margin-top */
  background-color: red;
}

header {
  height: 50vh;
  background-color: blue;
}

main {
  height: 200vh;
  background-color: green;
}
<nav></nav>
<header>
</header>
<main>
</main>

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

The alignment of an inline unordered list is off-center

My website at this link features a sticky navigation bar. However, the list items are slightly misaligned to the left and I'm struggling to pinpoint the cause. Despite adjusting margins and padding extensively, the issue persists. It's important ...

Tips for adjusting the width of a div to accommodate word wrapping

Can a div element be resized when word wrap occurs? To illustrate, take a look at this example from a similar query: .mypost { border: 1px solid Peru; margin: auto; min-width: 200px; display: inline-block; background: red; } .test { margin: ...

Vue.js does not display HTML properly within the vue-swal component

I'm currently working on integrating HTML content into a Sweet Alert popup using this code snippet Link: https://www.npmjs.com/package/vue-swal this.$swal({ title: '<i>Custom HTML</i>', html:`This is an <em> em ...

JavaFX WebView showcases HTML content differently compared to Libre Office Writer

After using SceneBuilder 2 to create a Dialog for displaying Help documents in a WebView, I proceeded to generate an HTML document in Libre Office Writer. Upon loading the 'Help.html' file, I noticed that the line spacing in the WebView differed ...

Displaying a PHP variable within Leaflet.js on an OpenStreetMap using JavaScript

I am currently working on integrating an openstreetmaps set to a specific latitude and longitude using leafletjs. For the custom fields from the backend, I am retrieving them like this : $longitude = the_field('longitude','option'); $ ...

Utilize Ajax to transfer an image from an input form to a PHP script

I am currently working on a basic HTML page that includes an image upload form. Here is the code for the form: <input type='file' id='image_uploaded' accept='image'/> <input type='submit' id="upload_image"/ ...

When making a JavaScript ajax request, Django is not being activated

I'm struggling to implement a feature where the text on a webpage changes based on the selection made in a dropdown menu. My JavaScript code doesn't seem to be calling the Django function correctly, so the placeholder text remains unchanged. Jav ...

Using jQuery Mobile to manipulate the window location in Chrome can cause unexpected jumping behavior

After clicking on a link, I am creating a URL for a clickonce application using an ajax call. Once the ajax request successfully receives the generated link, I use window.location = my_generated_link; This process functions correctly in IE9 – the clicko ...

Tips for extracting and structuring strings from unstructured CSV data using JavaScript?

From a public source, I've extracted a string of allergy data: Cedar 679 gr/m3 High, Grass 20 gr/m3 Medium, Trees 80 gr/m3 Medium, Molds Low. Although the number of items may vary, the standard format for trees and grasses is consistent, with allerge ...

The error message "writeUserData is not defined" is indicating that there is an undefined variable in the react code

I'm still learning React and I've been working on a new function: This is my code in summary: class Signup extends Component { constructor(props) { super(props); } componentDidMount() { } writeUserData = (userId, username, email) => ...

What is the process for incorporating data- attributes into ExtJs-generated HTML?

Currently working with ExtJs 4.1. In the process of developing a panel (for instance), I want to incorporate one or more "data-" attributes in the generated html, such as data-intro="some text" data-step="1" Any tips on how to achieve this? ...

Customized slider in jQuery UI allowing users to select height using a scaled ruler

Currently, I am tackling a challenging math problem related to adjusting the height selector input. Essentially, I have implemented a jQuery UI slider for choosing a height. It operates in inches and ranges from 0 to 120 (equivalent to 10 feet in height). ...

Using jQuery to create a script that will transition random images simultaneously within a div

I am currently working on a JavaScript script that will animate 10 images flying into a div, appearing side by side. The goal is to have each image fly in randomly with a unique transition. At the moment, my script is almost complete as I've successf ...

Load Angular modules dynamically

Is there a way to dynamically load a module (js, css and html) using a single directive at any point during the app's lifecycle? <my-module id="contacts"></my-module> The template for this directive looks like this <my-module id="con ...

Creating an animated border that fades to transparency using keyframes

I am attempting to create a simple animation of a circle with a border that transitions from red to a transparent color. My approach is to initially set the color to red and then animate it to transparent using keyframes as follows: .pulse{ margin: 20 ...

Trouble with applying position absolute to pseudo element in Firefox version 12 and higher

I seem to be running into an issue with the positioning of a pseudo element in Firefox version 12 or higher (possibly even earlier versions). Despite having position:relative on the anchor tag, the element appears to be relative to the entire page. Any ide ...

Tips for handling an AJAX form submission with various submit buttons and navigating through Laravel routes

I am having trouble getting the form below to submit to the database. The issue arises when trying to use AJAX to submit the forms. The problem seems to occur at this point: $(i).submit(function(event) { Can someone help me figure out what I am doing wr ...

I'm curious if anyone has had success utilizing react-testing-library to effectively test change events on a draftJS Editor component

​I'm having trouble with the fireEvent.change() method. When I try to use it, I get an error saying there are no setters on the element. After that, I attempted using aria selectors instead. const DraftEditor = getByRole('textbox') Draf ...

javascript: Retrieve specific row data from Bootstrap Modal table and pass it back to the main webpage

I am a beginner in using bootstrap modals and JavaScript, and I need assistance in resolving the following issue. Desired Outcome: 1. When the parent screen opens the modal window, it should display a form to find a username: <div class="form-gro ...

Maintained grid column stability amidst various modifications

I have a complex 2-column grid layout, and the example shown here is just one part of it. The complete code follows a similar structure. I am looking for a way to ensure that the green box always stays close to the red box in the layout, without having a ...