Guide on repositioning a navbar item as the first item when the hamburger menu is shown

I am currently in the process of constructing a new website using Bootstrap, and I have implemented a NavBar. On desktop and tablet screens, the NavBar appears as desired with links and a Search Box situated in the right corner:

https://i.sstatic.net/KWihJ.png

However, on mobile devices, I find it inconvenient to have the search feature at the bottom of the menu:

https://i.sstatic.net/4fMni.png

To provide an example of my code, I have created a Fiddle:

https://jsfiddle.net/hpxgowa8/

Below is the code snippet for reference:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="javascript:void(0)">Gravity Now!</a>
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navb">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navb">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">Calculator</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">Comparison</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">About</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search">
    </form>
  </div>
</nav>

I am seeking advice on how to reposition the Search Box as the first element when the hamburger menu is visible. Any suggestions would be greatly appreciated.

Answer №1

Using flexbox makes achieving this layout quite simple. By setting the navbar-collapse wrapper as a flex item, you have the ability to adjust the order of its contents:

.navbar-collapse {
    display: flex;
    flex-direction: column;
}

.form-inline {
    order: -1;
}

https://jsfiddle.net/dgz7h01w/

Answer №2

An effective grid solution is as follows:

To create a grid layout, use display: grid on the div. This will divide the div into two parts, with the first row set to 25% and the remaining to 75%, accommodating the list and form menu sections.

Utilize grid-row to specify that the search bar should be placed in row 1 and the list in row 2.

#navb  {
  display: grid;
  grid-template-rows: 25% 75%;
}

#navb > ul.navbar-nav {
  grid-row: 2;
}

#navb > form.form-inline.my-2.my-lg-0 {
  grid-row: 1;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <a class="navbar-brand" href="javascript:void(0)">Gravity Now!</a>
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navb">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navb">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">Calculator</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">Comparison</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="javascript:void(0)">About</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text" placeholder="Search">
    </form>
  </div>
</nav>

Enhance your CSS skills by mastering css-grids (alongside flex boxes). Verify compatibility for your project and dive deeper into grids by visiting this link and exploring more about grids here.

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 width of the table remains consistent

I have created a division that includes two tables stacked on top of each other. However, I am facing an issue where the width of the second table remains fixed and does not change even when I try to increase it. Here is the code snippet below: functio ...

Is there a way for me to establish a maximum word count for a paragraph?

When my paragraphs get lengthy, I only want the first 30 words to display on two lines. Is there a way to achieve this using HTML, CSS, or JS? The code snippet I tried did not work as expected. .long-text{ width: 70ch; overflow: hidden; white-sp ...

Guide to making an adaptive hamburger menu using Bootstrap

I am currently working on a project where I am attempting to create a responsive navbar with a hamburger menu that shifts the menu to the left when the device has a max-width of 480px. However, I seem to be facing some issues and can't quite figure ou ...

A division element continually broadens to its maximum width, regardless of whether there is enough space to accommodate additional elements on the same

Whenever the browser window is resized while displaying the code provided below, I am facing an issue where the div expands to the max-width even when I do not want it to do so. When everything fits on a single line, the layout appears fine. However, upon ...

Interactive vertical table headers and clickable cells

I am struggling with making cells clickable in a table I created with vertical text headings. The table is meant to document file formats used within our system, where currently only the text link is clickable to lead to the documentation. However, I aim t ...

Guide on activating javascript code for form validation using php

How can I activate JavaScript code for form validation? I am currently implementing form validation on a combined login/register form where the login form is initially displayed and the register form becomes visible when a user clicks a button, triggering ...

Personalized 404 Error Page on Repl.it

Is it possible to create a custom 404-not found page for a website built on Repl.it? I understand that typically you would access the .htaccess file if hosting it yourself, but what is the process when using Repl.it? How can I design my own 404-not found p ...

What is the problem with locating elements in Selenium with Java?

I've been encountering difficulties in finding elements on a webpage structured like this: <tr id="filter100" style="...." idx=0 <td> <div onclick=... style=... <table dir = "fil"> <tbody> ...

Stop the selection of text within rt tags (furigana)

I love incorporating ruby annotation to include furigana above Japanese characters: <ruby><rb>漢</rb><rt>かん</rt></ruby><ruby><rb>字</rb><rt>じ</rt></ruby> However, when attemp ...

Is the "sizeContent" function in jQuery associated with CSS media queries?

On my website, I have implemented jQuery code to ensure that the height is always full-screen: $(document).ready(function () { 'use strict'; sizeContent(); $(window).resize(sizeContent); }); function sizeContent() { 'use s ...

Automatically change a text based on its location

Currently leveraging the amazing flexible-nav to showcase the current subtopic within my post. I am also considering the possibility of extracting this current-string and showcasing it at the top of the page in my main navigation bar. This way, the text w ...

Responsive CSS - reordering div elements

#div1 { position: relative; max-width: 100%; height: 100px; background-color: green; color: white; } #div2 { position: relative; max-width: 100%; height: 100px; background- ...

Determine the Placement of Bootstrap Popover by Utilizing SVG and Mouse Coordinates

Is there a way to dynamically reposition the popover that currently appears to the right of the Circle SVG? I would like to either base its position on the user's mouse click within the CircleSVG or move it to the exact center of the SVG, including bo ...

Disable the ability to close the dialog box by clicking

this is my dialog <div *ngIf="visible" class="overlay" (click)="close()"> <div role="dialog" class="overlay-content"> <div class="modal-dialog" (click)="$event.stopPropagation()"> <!-- Modal content--> ...

What is the best way to toggle text visibility with a button click and what alternative method can be used if getElement does not work?

After successfully completing the HTML and CSS part, I hit a roadblock with JavaScript. I'm struggling to figure out how to create a button that can toggle the visibility of text when clicked. An error message keeps popping up in the console stating ...

Using Three.js to generate a CSS3DObject containing an HTML iframe from a different origin results in an error

The HTML code snippet below incorporates Three.js for creating a 3D scene and rendering it with CSS. It includes two separate HTML files: index.html and moo.html. <!doctype html> <html lang="en> <body> <script src="js/Three.js">&l ...

Tips for preventing Bootstrap 4 cards from extending to the far right side of the screen

My goal is to display a variable number of Bootstrap cards on an HTML page. The number of cards should depend on user data in the database, meaning that I need to create these cards dynamically with jQuery. One issue I encountered is that Bootstrap cards a ...

Getting the title name with CSS selectors in Scrapy

Using scrapy for web scraping, I encountered the following html structure: <a class="pointy" title="Rating: 9 / 10">dont select this text</a> <a class="pointy" title="Rating: 1 / 10">or this text</a> Q: How can I extract the title ...

Grid sidebar using tailwindcss

I just started using Tailwind CSS, but I'm having trouble understanding how the grid system works! I want my layout to look like this: https://i.sstatic.net/hlHmy.png Here is what I tried, but it's not working as expected: <div class=" ...

The disablement of the button is a result of concealed select elements

I'm facing an issue with a form that contains three select options: Fit Colour Size By default, the 'Fit' dropdown and 'Colour' dropdown are active with a default value selected (e.g. Regular Fit and Blue Colour). There are mul ...