Tips for positioning bootstrap dropdowns side by side in a row

I'm having trouble aligning my li items in a Bootstrap dropdown next to each other. I've tried everything but nothing seems to work. Here's my code:

 <nav class="navbar navbar-expand-lg
    navbar-light bg-light">
        <ul class="navbar-nav m-auto se">
          {% for category in category_list %} 
          <li class="nav-item dropdown second">    
            <a class="nav-link dropdown-toggle secon text-dark" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              <strong>{{ category.name }}</strong>
            </a>
            <ul class="dropdown-menu cat-drop" aria-labelledby="navbarDarkDropdownMenuLink">
              {% for subcategory in category.our_categories.all %}
              <li><a class="dropdown-item cat-item text-white text-center" href="{% url 'products:product' subcategory.id %}">{{ subcategory.name }}</a>
              {% endfor %}
            </ul>
        </li>
        {% endfor %}
    </ul>
</nav>

CSS code for the above:

 .second{
      display: flex !important;
      flex-direction: row;
    }
    .secon{
      font-size: 12px;
      margin: 2px !important;
    }

Answer №1

The issue arises because the default flex-direction value for .navbar-nav is set to column. Adjust this to row

To resolve this in CSS:

.navbar .navbar-nav {
  flex-direction: row;
}

Alternatively:

Apply the flex-row helper class to navbar-nav in the HTML like so:

<ul class="navbar-nav m-auto se flex-row">
  <li class="nav-item dropdown second">
    .
    .
    .
  </li>
  .
  .
  .
</ul>

See a working example with the first solution below.

.second {
  display: flex !important;
  flex-direction: row;
}

.secon {
  font-size: 12px;
  margin: 2px !important;
}

.navbar .navbar-nav {
  flex-direction: row;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04666b6b70777076657444312a352a35">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13717c7c67606761726353263d223d22">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
<nav class="navbar navbar-expand-lg
navbar-light bg-light">
  <ul class="navbar-nav m-auto se">
    <li class="nav-item dropdown second">
      <a class="nav-link dropdown-toggle secon text-dark" href="#" id="navbarDarkDropdownMenuLink1" role="button"
        data-bs-toggle="dropdown" aria-expanded="false>
        <strong>Category 1</strong>
      </a>
      .
.
.

Answer №2

The navbar-nav component is designed to be responsive. According to the documentation for navbar-nav:

Navigation in navbars will expand to fill as much horizontal space as possible, ensuring alignment of navbar contents.

Given their inherent responsiveness (thanks to Bootstrap), there are various methods to horizontally align items within navbar-nav:

  1. Expand your viewport to ≥992px. This can be achieved by using navbar-expand-lg (or by following option 2 below).
  2. Switch from navbar-expand-lg to navbar-expand-sm to achieve horizontal alignment of dropdowns on viewports of ≥576px.
    1. An alternative is to eliminate responsiveness by changing navbar-expand-lg to navbar-expand. This guarantees consistent horizontal alignment.
  3. Utilize custom CSS classes with higher specificity. While not recommended, this option is valid and available.

The following demonstrates the Alternative Option 2 (option 2.1) solution.

Please note that the only modification made to the original code is replacing navbar-expand-lg with navbar-expand on the <nav /> element.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791b16160d0a0d0b1809394c57485748">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aba6a6bdbabdbba8b989fce7f8e7f8">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

 <nav class="navbar navbar-expand
    navbar-light bg-light">

     <ul class="navbar-nav m-auto se">
       <li class="nav-item dropdown">
         <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink1" role="button" data-bs-toggle="dropdown" aria-expanded="false">
           <strong>Category 1</strong>
         </a>
         <ul class="dropdown-menu" aria-labelledby="navbarDarkDropdownMenuLink1">
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 1a</a>
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 1b</a>
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 1c</a>
         </ul>
       </li>
       <li class="nav-item dropdown">
         <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink2" role="button" data-bs-toggle="dropdown" aria-expanded="false">
           <strong>Category 2</strong>
         </a>
         <ul class="dropdown-menu" aria-labelledby="navbarDarkDropdownMenuLink2">
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 2a</a>
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 2b</a>
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 2c</a>
         </ul>
       </li>
       <li class="nav-item dropdown">
         <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink3" role="button" data-bs-toggle="dropdown" aria-expanded="false">
           <strong>Category 3</strong>
         </a>
         <ul class="dropdown-menu" aria-labelledby="navbarDarkDropdownMenuLink3">
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 3a</a>
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 3b</a>
           <li><a class="dropdown-item" href="{% url 'products:product' subcategory.id %}">Category 3c</a>
         </ul>
       </li>
     </ul>

 </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

Top tip for handling repetitive code with echo commands

I've been struggling with a question for quite some time now. Imagine you have a code that consists of multiple nested divs and spans, forming a square with an image inside. echo '<div> <div> <div> <div> <span> < ...

How can nunjucks templates be accessed from NPM (node modules)?

Is there a solution to make nunjucks imports/includes resolve from the NPM node_modules directory? Let's say we have installed a package as follows: npm i -S @example/cards Now, we need to import from it in a template like this: {% import "@exampl ...

various stunning galleries accessible from a single page of thumbnail images

I'm trying to create a unique gallery experience on my website. I have a set of 6 images, each featuring a different house. What I want is for each image, when clicked, to open up a fancybox gallery showcasing 4 more detailed photos of the same house. ...

Unable to switch between navigation items and icons

Currently, I am in the process of learning vanilla JavaScript and I'm trying to incorporate a navigation feature similar to when the hamburger icon is clicked. However, I have encountered an issue where I cannot toggle the hamburger icon to change it ...

Discovering the destination URL that an HTML button submits to

While attempting to scrape data from instacart.com, I encountered the requirement of entering a zip code to determine the displayed information. My intention is to utilize Python requests to submit a random zip code. However, I am unsure of which URL to po ...

Using javascript, hide or show a div without using jquery or the display:none property

I am looking for a way to show/hide a div up and down, but I have some requirements: I cannot use jQuery: toggle(), slideToggle(), fade, animate, etc. all use display: none, and I need the div to still occupy space in the DOM (I will be processing things ...

JS: Initiating a new keypress operation

When I press the Tab key, I want it to do something different instead of its default action. Specifically, I have a text box selected and I would like it to add spaces (essentially making the textarea behave like a text editor). How can I trigger this type ...

experiencing a problem with the scrollTo() function

I am encountering difficulty in finding the correct X and Y coordinate values to move an image using mouse scroll within a div. I have included my sample code below. I have successfully managed to scroll the image without using a div. Can anyone assist m ...

What is the best way to extract numbers from a string using JavaScript?

I am working with a string in javascript that looks like this: var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114"; My goal is to extract the numbers and store them in an array. I attempted the following code: var x=xyz.match(/\ ...

Output the result of a PHP script inside a JavaScript code

I'm having trouble getting the code below to display the success word. Can anyone spot what's wrong with it? Appreciate any help in advance. <script type="text/javascript"> function verification(){ var s = document.test_form.textfiel ...

Leverage the power of the General Sibling Selector to easily traverse through deeply nested elements in your

Hi there, I have a query regarding the General Sibling Selector. Let's start by looking at the HTML: <div id="layout-middle"> <div id="Center"> <a class="col Picture1">Title1</a> <a class="col Picture2">Title2< ...

To modify the color of text in a mat-list-option item

Using the mat-selection-list component, I have set up a list of contacts displayed through mat-list-option: https://i.sstatic.net/ri4lP.png Currently, the background-color changes when I click on a specific contact-name (e.g. Graeme Swan), and it remains ...

Is there a way to determine if the tab has been muted by the

Chrome enables users to easily mute tabs by right-clicking on them and selecting Mute Tab. I am curious about whether there is a method to detect when a user has muted the tab. In other words, I am interested in knowing if it's possible for a website ...

Using Javascript to Swap the Content of an Element

Recently, I've been delving into the world of javascript and have hit a roadblock. I understand how to instruct javascript to set a specific value for an ID, but now I have a new challenge: I want to create javascript code that can retrieve informati ...

React JS - Large image failing to display entirely

I'm currently facing challenges with displaying my image in full size. I am unsure what is causing this issue and would appreciate any guidance on correcting my approach. The specific image I am attempting to display at full-size can be found https:/ ...

What is the procedure to reduce my final search result by 1?

When the search is triggered, I want to filter the images by name. However, the issue arises when trying to make everything disappear on filter addition. <ul id="list2"> <li class="in2"> An extra number is added ...

Revamp selected bootstrap attribute

Imagine a scenario where there is: @media (min-width: 576px) { #projects-edit-middle-column .equal-columns-wrapper .equal-columns { width: 50%; padding-right: 25px; } } This is being utilized within a container: <div class="equal-columns ...

Determining the orientation of an image in JavaScript

Currently, I am attempting to determine the orientation of images using JavaScript in order to apply a specific class to them. This process seems to be functioning correctly on Firefox, but is presenting challenges on other browsers. It appears to work bet ...

Developing tabbed sections in XSLT

Utilizing Angular JS within XSLT, I am aiming to develop a tab-based user interface using XML data. The requirement is to generate multiple links and corresponding div elements based on the number of nodes in the XML. To manage the display of these div ele ...

The CSS file I've linked to in my HTML (ejs) document seems to be getting

As I work on developing my app from the backend, I am encountering an issue with loading CSS locally on my page. I am utilizing Node.js, Express.js, and EJS for my pages - part of the MEN/MEAN stack. <link href="../public/stylesheets/style.css" rel="st ...