Place a stationary element under another stationary element

I have a dilemma with two fixed elements on my website. One of them can toggle between display: block and display: none, while the other is always visible. I want both elements to stay at the top of the webpage without overlapping each other.

I've come across some related questions:

How to position a fixed div under another fixed div?

Fixed element below fixed element without JS

The suggestion is to place both divs inside a container div and set it as fixed.

Unfortunately, I am unable to follow this advice since the two elements are positioned differently in the code.

Below is a code snippet illustrating my issue:

nav,
.secondmenu {
  position: fixed;
  height: 120px;
  opacity: 1;
  top: 0;
  width: 100%;
  background: lightgrey;
}

.secondmenu {
  height: 50px;
  background: grey;
  opacity: 1;
  z-index: 2;
}

body {
  height: 1000px;
}
<div class="secondmenu">Might be there or not and overlays the other navigation</div>
<div>Some other stuff separating the two from each other with relative position</div>
<nav></nav>

My requirements and considerations:

  • If both elements are visible, they should remain fixed at the top of the page, with one below the other
  • If only the second element is visible, it should be fixed at the top of the page
  • The visibility of the first element can change dynamically using inline styles (display:none <-> display:block, without reloading the website)
  • I am open to solutions involving JavaScript or jQuery

Answer №1

To achieve this effect, you can add a 'top' property to the second navigation element with the same height as the first navigation, like I demonstrated below.

Keep in mind that this is just part of the solution: If you only want to display the second navigation, you can accomplish this using JavaScript by resetting the 'top' property back to 0.

nav,
.secondmenu {
  position: fixed;
  height: 120px;
  opacity: 1;
  top: 0;
  width: 100%;
  background: lightgrey;
}

.secondmenu {
  height: 50px;
  background: grey;
  opacity: 1;
  z-index: 2;
  top: 120px;
}

body {
  height: 1000px;
}
<div class="secondmenu">This might overlap or replace the other navigation elements</div>
<div>Include some content between the two sections with relative positioning</div>
<nav></nav>

Answer №2

It is best to create a holder for both elements. However, in your situation, you can position them both as fixed. You can then handle the style of both elements (such as top and left properties) using Javascript when one is hidden or shown.

You can try something like this: https://i.sstatic.net/1fZfW.png

Answer №3

Are you familiar with Sticky Kit?

Sticky Kit offers a simple method for affixing elements to the page as the user scrolls, ensuring that the element remains constantly visible.

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

Tips for modifying a state without triggering a re-render with the useState hook

I have a unique function onCondChange that triggers whenever the user modifies the value in the TextField. const onCondChange = (conds) => { updateState({ ...state, a: { ...state.a, conds }, b: ++state.b, }) } In this scenar ...

Navigating through the list of items in the navbar to find a specific element by

I have a question about my navbar functionality -- specifically, I want to be able to click on a navbar item and then smoothly scroll down to the corresponding id element. Here is the structure of my navbar: <nav class="navbar navbar-default navbar-st ...

Having trouble with passing parameters to my ASP.Net Core controller via AJAX and JQuery

I'm looking for some assistance with a datagrid and download button setup on my UI. The goal is to pass the selected row's ID as a parameter to my controller's Download method for further processing. Despite my efforts with Ajax/JQuery, I s ...

Problem encountered when utilizing dat.GUI in a three.js demonstration

I recently experimented with using dat.GUI in a specific three.js example. To integrate a GUI for adjusting mesh opacity, I made the following code modifications. var loader=new THREE.VTKLoader(); loader.load ("models/vtk/bunny.vtk", function(geom){ var ...

Exporting keys of objects one by one

I am trying to mock the `fs` module in vitest using [memfs](https://github.com/streamich/memfs). To do this, I have created a mock file located at `./__mocks__/fs.ts` where I have set up the mocked volume and fs. However, I am facing an issue with the moc ...

I am experiencing issues with Jquery ajax JSON not functioning properly on my webserver when accessing my pages

When utilizing the Jquery Ajax function to parse the last.fm API, everything runs smoothly. Here is the Jquery function: $.ajax({ url: "http://ws.audioscrobbler.com/2.0/?method=album.search&album=believe&limit=2&api_key=b25b959554ed76058a ...

Save information to chrome's storage system

I have the need to save favorite and deleted IDs in my database. I created two functions for this purpose: function ADD_BLOCKED(id) { chrome.storage.local.get("blocked", function (data) { if (data.blocked == null) data.blocked = [] ...

Fetching data from a DIV element

I am trying to figure out how to assign a value from a div tag (6.897882999999999, 79.92221100000006) to a PHP variable using the div's id. Can anyone help me with this? Thank you. <form id="mainForm" method="post" action=""> &l ...

Tips for implementing an `onclick` event on this specific button

I've utilized JavaScript to generate a button. Now, I'm wondering how to incorporate an onclick event for this button. var b1 = document.createElement("button"); b1.setAttribute("class", "btn btn-default"); b1.setAttribute("id", "viewdetails"); ...

The POST method is failing to collect data from the form

Currently in the process of learning Web Design with HTML, PHP, and Javascript, I have taken on the challenge of creating my own website called MySite.html. Within this site, there is a sign-in screen that utilizes the POST method to send data to a file na ...

Trouble with Div Display

I have a section below my form that will show an alert message if there is an error. The issue I am facing is that if I use the inline-block property, everything within the section will be displayed in the alert color. I specifically want the background- ...

Preventing a Click Event on Child Element from Triggering the Parent's Click Event in jQuery

Can you help me figure out how to prevent a button from executing the onclick function of its parent div in this example? <div onclick="$('#extra-info').slideToggle();" class="card card-body item"> <div class="ro ...

The jQuery hide() method conceals an element without affecting its parent

Can't seem to figure out JQuery... So, here's the issue: I've got a page where visitors can enter their email address by clicking on a link inside a div (id="contact"). <div id="contact"><a href="">Interested in leaving your con ...

Guide to dynamically insert rows in HTML using Vue.js based on the selected option

This is the initial row. Depending on the selection made here, different rows will be displayed <div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Case Type< ...

What's the best way to update the value of an angular field upon submission?

Could someone please provide instructions on how to update the myName variable when the "submit" button is pressed? Thank you! app.js: app.controller('SomeController', ['$scope', 'emails', function($scope, emails) { emails ...

What is the best way to align a group of checkboxes to the left side of the page

I am struggling to display a group of 8 checkboxes in 2 columns with labels on the right side of the checkboxes, and I want them to be left-aligned instead of center-aligned. Despite trying different approaches with the CSS, I can't seem to achieve th ...

Vue feature allows users to save favorite films

Welcome to my homepage where I showcase 3 movies taken from the store, each with an "add to fav" button. My goal is to have the selected movie appear on the favorites page when clicked. As a newcomer to Vue, any code or documentation suggestions would be h ...

Is utilizing a standardized logging system considered a best practice for a node and express application?

I am currently working on a node.js application that consists of several dozen modules and uses bunyan for logging with JSON output and multiple configurable streams. I have been searching for good examples on how to implement a logger instance across all ...

Apps hosted on Heroku are restricted from accessing CORS, even within the platform's domain

I've been struggling with this problem for a while now. My Nuxt app and service are both hosted on Heroku. After ditching cors(), I added the following code: app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", '*&ap ...

Manipulating the DOM with PHP, jQuery, and JavaScript

In my project, I have a dynamic list that is populated from the database and sorted by dates using the time field in ascending order. To achieve this, I am utilizing a PHP foreach loop. <li id="events"> Time: <?php echo $venue['time']; ...