What is the best way to rearrange the order of navbar items when working with two nav bars and a single toggle in bootstrap 4?

I am looking to implement a design with two navigation bars that share a single toggle. The first navbar should include the logo, email icon, and a search box, while the second navbar is reserved for additional links. On smaller screens, when the toggle is activated, I want the items from the top navbar to display below the second navbar in the following order:
Home
About
Departments
Admissions
Downloads
Email
Search

<nav class="navbar navbar-expand-xl navbar-light" style="background-color:#FFFFFF;" id="navtop">
   <a class="navbar-brand" href="#"><img src="logo.png"></a> 
      <button class="navbar-toggler" data-toggle="collapse" data-target=".navbars">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse navbars" id="collapse_target1">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item ">
               <a class="nav-link" href=""><i class="far fa-envelope"></i></a>
            </li>
          </ul>
            <form class="form-inline">
               <input class="form-control form-control-sm" type="search" placeholder="Search">
              <button class="btn btn-light btn-sm " type="submit"><i class="fas fa-search" aria-hidden="true"></i></button>
            </form>
        </div>
</nav>

<nav class="navbar navbar-expand-xl sticky-top" style="background-color:#FFFFFF;" id="navbottom" >
   <div class="collapse navbar-collapse navbars" id="collapse_target2" >
       <ul class="navbar-nav mx-auto" >
          <li class="nav-item">
              <a class="nav-link" href="#">Home</a>
          </li>
          <li class="nav-item">
              <a class="nav-link" href="#">About Us</a>
          </li>
          <li class="nav-item">
              <a class="nav-link" href="#">Departments</a>
          </li>
          <li class="nav-item">
              <a class="nav-link" href="#">Admissions</a>
          </li>
          <li class="nav-item">
              <a class="nav-link" href="#">Downloads</a>
          </li>
        </ul>
    </div>
</nav>
 

Answer №1

To rearrange the item order for different screen sizes, especially moving main menu items to a second row on larger screens, using flex order within one navbar is the most straightforward approach.

In this solution, I eliminated the second navbar and introduced a container div beneath the navbar-collapse div. This new div is styled with Bootstrap classes, setting it initially to display: flex with flex-direction: column for a single-column layout on smaller devices. At the medium breakpoint, the display switches to flex-direction: row and flex-wrap: wrap, allowing the main menu items to wrap onto a second line.

The sequence of elements starts with the menu items, followed by email and search icons. This sequence determines how the navbar content appears on mobile screens and is read by assistive technologies like screen readers. By utilizing order-md-1 -2 -3, the order can be adjusted for wider screens as needed.

To prevent the logo image from affecting the centering of the main menu, I positioned it absolutely. In the absence of specific details about the logo in the original query, a placeholder.com image was used as a temporary solution.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d2dfdfc4c3c4c2d1c0f0849e869e80">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a2afafb4b3b4b2a1b080f4eef6eef0">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<nav class="navbar navbar-expand-md navbar-light bg-light" id="navtop">
    <a class="navbar-brand p-0 position-absolute align-self-start" href="#"><img src="
        https://via.placeholder.com/120x60.png?text=Logo" alt="Logo" style="height: 1.875rem; width: auto;"></a>

    <button class="navbar-toggler ml-auto" type="button" data-toggle="collapse" data-target="#collapse_target1" aria-controls="collapse_target1" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse navbars" id="collapse_target1">
        <div class="flex-container w-100 d-flex flex-column flex-md-row flex-md-wrap">
            <ul id="lower-nav" class="navbar-nav mr-auto order-md-3 justify-content-md-center w-100">
                <li class="nav-item">
                    <a class="nav-link" href="#">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">About Us</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Departments</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Admissions</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Downloads</a>
                </li>
            </ul>
            <ul class="navbar-nav mr-auto mr-md-0 ml-md-auto order-md-1">
                <li class="nav-item px-3 pl-md-0">
                    <a class="nav-link" href=""><i class="far fa-envelope"></i></a>
                </li>
            </ul>
            <form class="form-inline mr-auto mr-md-0 order-md-2">
                <input class="form-control form-control-sm" type="search" placeholder="Search">
                <button class="btn btn-light btn-sm " type="submit"><i class="fas fa-search" aria-hidden="true"></i></button>
            </form>
        </div>
    </div>
</nav>

<div class="content text-center vh-100 bg-info">
    Center
</div>

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

What is causing the col-auto with the red border to exceed its available height?

For this code, I've utilized version 5 of Bootstrap. In full screen width, the col-auto with the red border seems to be taking up more space than its content. The yellow border marks the available content space inside. I'm curious about what migh ...

Creating a character jump animation on an HTML5 canvas

While following a tutorial on creating character animations, I encountered an issue. The tutorial, located at , made it easy to make the character move left (the right movement was already implemented). However, I am struggling with how to animate the char ...

Prevent clicks from passing through the transparent header-div onto bootstrap buttons

I have a webpage built with AngularJS and Bootstrap. It's currently in beta and available online in (German and): teacher.scool.cool simply click on "test anmelden" navigate to the next page using the menu This webpage features a fixed transparent ...

Display an SVG image on a web page using its URL in an HTML

I have a URL to an SVG file hosted on an external server, for example: user-online.svg. I want to display this image on my website. How can I achieve this? I attempted the code below, but it only displays https://i.sstatic.net/r390N.png. <img src=&quo ...

What could be causing the AngularUI bootstrap datepicker to not appear?

Exploring the popup datepicker demo from angularUI bootstrap, which is embedded in their page and works perfectly. However, when I tried to create a minimal plunker here, the button click does not open the calendar. Any thoughts on what might be causing th ...

The appearance of the page varies when viewed on different computers using the same version of Firefox

Previously, I posed a question with a touch of irony regarding Firefox without providing an example. As a result, my question was downvoted and I had to request its removal. Now, I have an example illustrating the issue at hand. It took some time to clean ...

Guide to dynamically updating a textarea in vue.js by incorporating data from several inputs

Is there a way to update a textarea based on multiple inputs using jQuery and vue.js? I have successfully implemented the jQuery part with line breaks, but when I try to display the value of the textarea elsewhere using vue.js, it doesn't seem to work ...

Utilizing SASS, JavaScript, and HTML for seamless development with Browser Sync for live syncing and

I've been on a quest to find a solution that covers the following requirements: Convert SASS to CSS Post-process CSS Minify CSS Move it to a different location Bundle all Javascript into one file Create compatibility for older browsers Tre ...

Issue with Ionic app on iPhone X not correctly detecting safe areas

Struggling to place a loading element between the top toolbar and bottom tabs on various iPhone models and iOS versions. The challenge arises with iOS 10. Using "margin-top" causes errors, while "padding-top" doesn't work on iPhone X. Is there a CSS s ...

How can I display an image caption within a lightbox using jQuery?

There is a caption under each image. <p> <a href="large.jpg"> <img width="85" height="75" src="thumb.jpg/> </a>Caption</p> I am looking to display the same caption at the bottom of the lightbox. <span id="lightbox-image ...

The functionality of the Bootstrap 4 Carousel featuring multiple items is experiencing malfunctions

I'm encountering an issue with my Bootstrap 4 card carousel. When the next or prev buttons are clicked, there is a strange transition effect. Additionally, in the mobile version, the buttons do not work properly. It seems that when the card slides to ...

Tips for achieving proper styling and formatting of elements in jQuery UI

I've encountered an issue when trying to use jQuery UI after downloading it multiple times. In the examples provided with the download, the UI elements appear perfectly formatted, but when I implement them on my own pages, the styles and formatting ge ...

The border-radius property in Bootstrap buttons causes unwanted white border artifacts to appear

Strange anomalies in the borders of my Bootstrap buttons caught my attention recently. Upon further investigation, I realized that this issue is not linked to my custom styles or theme, as the artifacts can also be seen on the Bootstrap button documentatio ...

Remove the footer from the main section

I'm facing an issue with the web page layout of my website. Here is a snippet of the structure: <body> <div class="container"> <div class="sidebar"> </div> <div class="content"> </div> </div> < ...

Customize Appearance: Overlay Images with Display Properties

I have created a unique slideshow that overlays multiple images using JavaScript to change their opacity and attributes. You can view the slideshow here: Upon inspection (using firebug), I noticed an issue with how the images are displayed. When the displ ...

Creating a text box that stands out by adjusting the length in Bootstrap

My webpage displays three text boxes, and I want to increase the size of one specific text box. Here is the code that I have tried: <div class="row"> <div class="col-md-3"> <mat-form-field (keydown.enter)="$event.preventDefa ...

Setting a background-image using jQuery in Codeigniter can be done by following these steps

Currently, I am developing a project in Codeigniter. In my jQuery file, I have included code to set the background image of the body element: $('body').css('background-image','url(<?php echo base_url("assets/images/bg2.png");?& ...

Types of Content in Solr Responses

My journey with Solr has been enlightening, but I've hit a bump in the road. When querying Solr using: curl "http://localhost:8983/solr/myCollection/select?q=*:*&wt=json&rows=0" I receive a webpage displaying the JSON response. However, the ...

``Is there a way to redirect users when they click outside the modal-content in Bootstrap

I have a modal on my website that currently redirects to the homepage when the close button is clicked. However, I also want it to redirect to the homepage when clicking outside of the bootstrap modal, specifically targeting the ".modal-content" class. I ...

Generate HTML content based on the permissions from a REST API

I have a frontend that renders based on user permissions from a REST API. These permissions could include deleting users, creating users, adding users to workflows, and more. Most of these permissions are managed by an administrator. So my question is: H ...