When the navbar collapses, it pushes non-collapsible elements aside to make room

I'm facing an issue with my navbar design. Currently, I have a navigation bar with a toggle button that collapses the menu on smaller screens. However, I want to prevent a specific element, which I refer to as the "army of shopping carts", from collapsing when the screen size is reduced.

Here's an illustration of what I'm trying to achieve:

https://i.sstatic.net/5iB2D.png

Despite searching for solutions and experimenting with code, I haven't been able to achieve this effect yet. Here is a link to the JSFiddle containing the relevant code snippet.

Snippet of Code:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">English Park Cuisine</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav ml-auto">
            <li class="nav-item active">
                <a class="nav-link" href="index.php">Home<span class="sr-only">(current)</span></a>
            </li>
            
            /* Other navigation items omitted for brevity */
            
        </ul>
    </div>

    <div class="d-flex order-lg-1 ml-auto pr-2">
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>           
    </div>

</nav>

This issue resembles a problem discussed in this stackoverflow thread. However, the uniqueness of my case lies in the specific ordering of elements within the navbar structure.

Answer №1

To solve this issue, utilize the order-* classes from Bootstrap. You need to adjust the order values of elements in the navbar so that they appear correctly when collapsed. In the example below:

(1) On larger screens (lg), the shopping cart icons have an order-4 and will be positioned last.

(2) When the navbar is collapsed, the shopping cart icons will have an order-1 and the collapse toggle button will have an order-2. This ensures that the collapse button does not interfere with the icon placement.

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">

  <a class="navbar-brand order-0" href="#">English Park Cuisine</a>

  <div class="order-lg-4 order-1 ml-auto mr-2">
    <a href="#" class="navbar-text">
      <i class="fa fa-shopping-cart fa-lg" style="color: white;"></i>
    </a>
    <a href="#" class="navbar-text">
      <i class="fa fa-shopping-cart fa-lg" style="color: white;"></i>
    </a>
    <a href="#" class="navbar-text">
      <i class="fa fa-shopping-cart fa-lg" style="color: white;"></i>
    </a>
  </div>

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

  <div class="collapse navbar-collapse order-3" id="navbarNavDropdown">
    <ul class="navbar-nav ml-auto">
      <li class="nav-item active">
        <a class="nav-link" href="index.php">
          Home<span class="sr-only">(current)</span>
        </a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Menu
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="menu.php">Pizza</a>
          <a class="dropdown-item" href="#">Pasta</a>
          <a class="dropdown-item" href="#">Soups</a>
          <a class="dropdown-item" href="#">Salads</a>
          <a class="dropdown-item" href="#">Desserts</a>
        </div>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contact</a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <p class="dropdown-item">0746117702</p>
          <p class="dropdown-item">0742112452</p>
          <p class="dropdown-item">0735212352</p>
        </div>
      </li>
    </ul>
  </div>

</nav>

Answer №2

check this out:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">English Park Cuisine</a>

    <div class="d-flex order-lg-1 ml-auto pr-2" style="margin-right:20px;">
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>

    </div> 
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav ml-auto">
            <li class="nav-item active">
                <a class="nav-link" href="index.php">Home<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Menu
            </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <a class="dropdown-item" href="menu.php">Pizza</a>
                    <a class="dropdown-item" href="#">Pasta</a>
                    <a class="dropdown-item" href="#">Soups</a>
                    <a class="dropdown-item" href="#">Broths</a>
                    <a class="dropdown-item" href="#">Desserts</a>
                </div>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contact</a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <p class="dropdown-item">0746117702</p>
                    <p class="dropdown-item">0742112452</p>
                    <p class="dropdown-item">0735212352</p>
                </div>
            </li>
        </ul>
    </div>



</nav>

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

Troubleshooting problem with navigation tabs in Bootstrap version 3.x

My bootstrap component nav-tabs works fine on desktop when the page width is larger than necessary for the row. However, on mobile devices, the responsive design doesn't behave as expected and the tabs become misaligned. You can see the issue in the c ...

Stacked divs needed to be aligned vertically

Is there a way to make the head and main tabs fixed vertically one below the other? Keep in mind that these need to be separate angular components and cannot be combined under another div. The rows also need to be scrollable. .pages { float: left; ...

What causes the child div to not be centered when using margin and border box?

<style> .parent { width: 300px; height: 300px; border: 1px solid red; padding: 20px; } .child { box-sizing: border-box; margin: 20px; width: 100%; height: 100%; ...

Using CSS to create a background-clip effect on text along with a smooth opacity

I've coded a unique effect for my <h1> heading text where each letter is enclosed in its own <span> element, like this: <h1 class='gradient-heading' id='fade'> <span>H</span> <span>e</span ...

Icon alignment to wrapped text width has been successfully achieved with the implementation of Flexbox onto a single

I am facing an issue with text wrapping within a flexbox. I want the width of the flex item to always match the length of the wrapped text so that an icon placed next to the item stays adjacent to the text. However, due to text wrapping, there is a gap bet ...

Adjusting CSS styling based on screen orientation changes when resizing a browser window on a

I'm uncertain about the functionality on desktop screens when it comes to resizing the browser window. My understanding is that changing the orientation should occur naturally when the height surpasses the width and vice versa after resizing. However ...

Banner advertisement on a webpage

Currently, I am working on a website project for clients who are interested in having background ads displayed throughout the entire site. In terms of coding this feature with CSS, it is relatively straightforward: body { background-image: url('a ...

The hidden popup functionality is not functioning properly within the Bootstrap modal

I've been attempting to activate the callback function when the bootstrap close event is triggered, but unfortunately, it doesn't seem to be working. Here's the code I'm using: <div class="modal" id="overlayyy" role="dialog" aria-la ...

What is the best way to confine the child elements within a parent element using CSS?

Check out my CSS and HTML code here: https://jsfiddle.net/z2cc11yk/ Here's the HTML: <div class="progressbar-container"> <div class="progressbar-text">125%</div> <div class="progressbar-progress" style="background-color: red; wi ...

Why does this inner HTML table always adjust its width based on the content within it? Is there a way to make it match the width of its container instead

I'm not very familiar with HTML and CSS, and I've come across a problem. Here is the CSS structure in question: <!-- TECHNICAL REFERENCE: --> <div id="referenteTecnicoTab"> <table width="800px" class="standard-table-cls table-he ...

Creating an Innovative Grid Design with Varying Column Widths for Each Row

Seeking advice on achieving a specific layout using HTML. I have attempted to use HTML tables but struggled with creating rows with varying columns. Does anyone have recommendations for grid tools that may be more helpful? Thank you ...

What is the process for validating the CSS background-image URL using Javascript?

The concept here is to verify the URL of the CSS element's id (.boot). If the URL matches, which it does, then it should display "hello world." However, despite everything seeming correct, the expected behavior is not occurring, and the reason behind ...

The footer is failing to appear in its correct position on the homepage of the Rails application

My footer looks great throughout my Rails app except for the Home page. Here is the content of my application.html file: <!DOCTYPE html> <html> <head> <title><%= yield(:title) || "Wagon Rails" %></title> <meta n ...

How to style a div's height responsively behind a circle using CSS

Can a CSS pattern be created with responsive design, ensuring that the background line is always in the correct position and height regardless of window size? https://i.sstatic.net/27Tf2.jpg ...

Incorporate an additional ring around the outer edge of the donut pie chart

I have been attempting to create a donut pie chart similar to the one shown below. The main challenge I am facing is with the outer strip. How can I achieve this effect? Your guidance in the right direction would be greatly appreciated. https://i.sstati ...

The rounding of my image is uneven across all sides

I'm attempting to give my image rounded borders using border-radius, but I'm unsure if the overflow property is affecting the image. Image 1 shows my current image, while Image 2 depicts what I am trying to achieve. I am utilizing React, HTML, no ...

Place a <p> element at the bottom of a parent <div> that is floating

I am currently facing an issue related to the layout of my webpage. There is a @page media mentioned along with a floating <div> element placed at the top of the page. Inside this <div>, there are an <img> and a <p> tag (which conta ...

Customizing nested tables in your HTML email

When creating an HTML email template with nested tables for maximum email client support, the question arises about using inline styling. Should the style always be applied to the lowest level td, or is it possible to apply it to a parent table or td? For ...

Vue-loader component experiencing issues with applying dynamic CSS styling

I'm working with a Vue Component and I want to enhance it by binding a color to a specific message. Initially, I attempted to achieve this using a binding like :style="styles" However, I encountered the following error: The property or method "st ...

Using jQuery to update the input value when the mouse hovers over a div

Attempting to update an input value using jQuery mouseover. Situation: There are 5 divs with different colors and usernames. When hovering over a div, the input text (and background color for the color input) changes based on database values. Each time a ...