Adjust the margin-top based on the height of another element

I'm facing an issue with two fixed navbars positioned one under the other. I used the 'fixed-top' class to fix both at the top, but the margin-top I added to make them align is not responsive.

Is there a way to make the margin-top responsive or calculate the necessary margin dynamically?

DEMO

HTML

<nav class="navbar navbar-expand-md navbar-light bg-white MyNavBar fixed-top" >
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2" id="dual-collapse2">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item active">
                    <a class="btn"><img src="home.svg"></a>
                </li>
            </ul>
        </div>
        <div class="mx-auto order-0">
            <a class="btn"><img src=".logo-home.svg"></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
        <div class="navbar-collapse collapse w-100 order-3 dual-collapse2" id="dual-collapse2">
            <ul class="navbar-nav ml-auto">
                <div class="row">
                <li class="nav-item">
                 <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
                </li>
                <li class="nav-item">
                      <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
                </li>
                <li class="nav-item">
                     <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
                </li>
                <li class="nav-item">
                      <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
                </li>
                <li class="nav-item" style="width: 57px;">
                     <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
                </li>
            </div>
            </ul>
        </div>
    </nav>
<nav class="navbar navbar-expand-md navbar-light bg-white fixed-top Mytoolbar" style="margin-top: 74px;">
      <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse22" id="dual-collapse22">
          <ul class="navbar-nav mr-auto" id="dual-collapse22">
            <li class="nav-item folder_category">
               <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
            <li class="nav-item btn-upload">
              <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
          </ul>
      </div>
      <div class="mx-auto order-0">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#dual-collapse22">
          <span class="navbar-toggler-icon"></span>
      </button>
        <ul class="navbar-nav navbar-collapse collapse w-100 order-3" id="dual-collapse22">
          <li class="nav-item btn-upload">
              <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
            <li class="nav-item btn-upload">
              <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
        </ul>

      </div>
      <div class="navbar-collapse collapse w-100 order-3 dual-collapse22" id="dual-collapse22">
          <ul class="navbar-nav ml-auto">
           <li class="nav-item btn-upload">
              <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
            <li class="nav-item">
              <a class="nav-link">
            </a>
            </li>           
            <li class="nav-item">
              <a class="toggle-third nav-link">
                <img src="ra.svg" >
              </a>
            </li>
            <li class="nav-item btn-upload">
              <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
            <li class="nav-item btn-upload">
              <a class="nav-link btn">sdfsdfsdfsdfsdfsdfsdfsdfsdfs</a>
            </li>
          </ul>
      </div>
  </nav>

Answer №1

Here is a solution that should work:

  1. Enclose both navbars within the same div
  2. Add the class fixed-top to this div
  3. Remove the fixed-top class from both navbars
  4. Get rid of the inline css of the second navbar (margin-top: 74px;)

Refer to the code snippet below:

...

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

Reduxforms does not rely on preloaded CSS or JavaScript

Currently, I am utilizing MDBootstrap as my primary CSS framework and integrating redux forms to simplify form management. The issue at hand is that the design and style of elements appear different compared to the original static html page layout. Here ...

Ways to verify if a specific extjs panel has finished loading

After a specific panel has finished loading, I need to insert a JavaScript code (using the panel's ID). What is the best way to ensure that the panel has been fully rendered so that I can access its ID using document.getElementById? Thank you. ...

What could be causing the removeChild() method to fail when trying to delete a list item in JavaScript DOM manipulation?

Check out this interactive shopping list. You can add items, delete them, and mark them as completed. Adding and deleting items works fine, but there's an issue when toggling them. Issue (occurs when toggling multiple list items) If you add 3 items ...

Ensuring Uniform Column Height in Bootstrap 4 - Preventing Buttons from Being Forced to the Bottom of

Using the Bootstrap 4 class 'h-100' to ensure equal height for all three columns, I encountered an issue where the buttons that were initially aligned to the bottom of each div are no longer in correct alignment. How can I maintain button alignme ...

Exploring the engaging section of interconnected images featuring rounded edges

I'm currently working on a unique widget that can be found at this link: http://codepen.io/JonnyNineToes/pen/zGYxwV This widget features an image that reveals additional information when clicked, with the extra info sliding out from behind the image. ...

Calculate the number of parent nodes and their respective child nodes

I am curious about how I can determine the number of children nested within parent-child relationships. For example: const parent = document.querySelectorAll('.parent'); parent.forEach(el => { const ul = el.querySelector('.child3-chi ...

Issue with Dropzone not functioning correctly within Vue transition modal

I've implemented a dropzone function in the mounted function, which works perfectly when the dropzone is outside of a modal in the view. However, when I try to use it within a modal with a transition effect, it doesn't work. Any suggestions on ho ...

Scrolling Container Following Down the Page, Halts at Its Own Bottom (Similar to Twitter)

I am facing an issue and need some help. I am working on creating a three-column page layout where the middle section is for posts, the right section is for links and references (a bit long), and the left section is fixed. My question is: How can I preven ...

Differences in rendering of HTML select element between Chrome on macOS and Chrome on Windows

During testing of a section of the app at my workplace, it was discovered that dropdowns and select elements in a specific UI scenario appeared differently on Chrome for Windows and Ubuntu compared to Chrome for macOS. I attempted to analyze the elements ...

How can I obtain live subprocess output updates?

I am attempting to execute the "tail - f" command on the server to receive real-time output displayed on this HTML page. However, I have not been successful in achieving this and only simple output commands are being displayed instantly. How can I utilize ...

The alignment of vertical text can impact the positioning of surrounding text

I am working on creating an online portfolio using HTML and CSS, but I am facing a challenge with vertical alignment. My goal is to have a line of vertical text running down the left side of the screen that displays "My occupation". On the right side of t ...

Creating a hyperlink within an @Html.LabelFor tag

In my current asp.net mvc project, I have a code snippet that generates a name like this - @Html.LabelFor(x => x.ReturnAirTemperature, new { style = "position: absolute;top: 310px;left: 18px;z-index: 10000;font-weight:bold" }) Here is the model associ ...

Placeholder information is not displaying correctly on Bootstrap 5.2

I am currently working on a webpage using Bootstrap 5.2 and have the following code snippet included: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91f3fefee5e2e5e3f0e1d1a4bfa3bfa ...

Instructions on how to navigate to a class page by clicking a button in a ReactJS interface

I am currently working with React and have implemented 3 classes in separate files. The App.js file contains a navbar and button, which when clicked, displays a table from 'Table.js'. I have also added buttons in front of each row in the table th ...

Deciding on the Best Watcher for Running `make watch` in Twitter Bootstrap

When it comes to making watch on Twitter Bootstrap, should we be utilizing watchr for Node.js or the gem watchr? A previous discussion recommended using the gem version of watchr. I attempted to do this with gem watchr, following the instructions from htt ...

Eliminate spaces between divs without any margins or padding

In my HTML code, I have two divs with different classes - mini-date-holder and mini-event-holder. Even though both of these divs are set to have no margin or padding, they are still displaying with a gap between them. I'm struggling to understand why ...

"Exploring the Power of Angular with HTML Checkboxes

I am trying to display a checkbox as either checked or unchecked based on the value of a variable {{CurrentItem.STATUS}}, which can be either 1 or 0. Is there an Angular solution that does not require any javascript/typescript to achieve this? I want the ...

Troubleshooting a Navigation Styling Issue in HTML and CSS

I'm struggling to position my drop-down menus correctly in my WordPress theme navigation. They are appearing far away from the nav bar and end up near the top left corner of the screen when I hover over a ul li >. Here is the CSS code I am using: ...

Creating specialized validation for numeric fields in Angular2

Attempting to implement custom validation in Angular 2 has been a challenge for me. I have followed the necessary steps based on my understanding, but still struggling to get it working. import { FORM_DIRECTIVES, AbstractControl, ControlGroup ,FormBuilder ...

Using HTML5 canvas to draw circles with additional metadata stored in variables

I'm designing a seating chart that will indicate spots with circles and show a tooltip on mouseover with the person's first name, last name, and possibly other details. I plan to use Kinetic.JS for region/tooltip support based on the tutorials I& ...