A dual row navigation layout with the second row featuring equally distributed elements in a neat and organized manner

Looking to create a responsive collapsible navigation with two rows. The first row should contain the logo and language selector, while the second row should have the main links:

|                                                             |
|                             LOGO          lang              |
|        link1        link2         link3        link4        |

Check out the progress so far on JSFiddle

I'm having trouble getting the main links to have equal width and be distributed horizontally evenly on larger viewports. The links are currently stacked very tightly.

Here is the code for the second row navigation:

<ul class="navbar-nav nav nav-pills nav-justified">
  <li class="nav-item">
    <a class="nav-link" href="#">Home</a>
  </li>    
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">
      Categories
    </a>
    <div class="dropdown-menu">
      <a class="dropdown-item">Cat 1</a>
      <a class="dropdown-item">Cat 2</a>
      <a class="dropdown-item">Cat 3</a>
      <a class="dropdown-item">Cat 4</a>
    </div>
  </li>

  <li class="nav-item">
    <a class="nav-link" href="#">Live Auction</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Contact us</a>
  </li>
</ul>

Does anyone have any suggestions on how to override the classes or provide another solution? Thank you!

Answer №1

To ensure that your flex items (li) adjust to the content, set the initial size by changing flex-basis from 0 to auto. In Bootstrap 4, you can achieve this by using the flex-fill class. Include it in your HTML:

<li class="nav-item flex-fill">
    <a class="nav-link" href="#">Live Auction</a>
</li>

Alternatively, you can specify this property in your CSS:

.nav-justified li.nav-item {
    /* ... */
    flex-basis: auto;
}

Check out this demo for reference: https://jsfiddle.net/wcj1gzr5/1/

If you want to distribute flex items with an effect similar to justify-content: space-between, make sure the div and ul containing the flex items expand their width. This allows enough space for the flex items to be evenly distributed. Add min-width: 100%; to both elements to fill their container, then use justify-content on the ul for spacing & positioning control.

Test out this functionality in the live demo here: https://jsfiddle.net/wcj1gzr5/2/

Answer №2

To achieve this effect, simply insert width:100% into the styles for both .navbar-collapse and .nav-pills. See the code snippet below for reference:

<!DOCTYPE html>
<html lang="en>

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <style>
    .navbar-collapse,
    .nav-pills {
      width: 100% !important;
    }
    
    header {
      width: 100%;
      background: #282828;
      background-position-x: 0%;
      background-position-y: 0%;
      background-image: url("https://cdn.wallpapersafari.com/65/40/cJtjUm.jpg");
      background-size: cover;
      background-position: center;
    }
    /* center the logo */
    
    .navbar {
      justify-content: center;
    }
    /* in order to center the logo */
    
    .navbar .navbar-toggler {
      position: absolute;
      right: 1rem;
      top: 0.5rem;
    }
    /* center all navbar items */
    
    .navbar-nav {
      align-items: center;
    }
    /* since it's expanding at lg */
    
    @media (min-width: 992px) {
      /* in order to display in 2 rows */
      .navbar-expand-lg {
        flex-flow: column nowrap;
      }
      /* same logic as the navbar-toggler above */
      .navbar-nav.upper-controls {
        position: absolute;
        right: 1rem;
        top: 0.5rem;
        font-size: 85%;
      }
    }
  </style>
</head>

<body>

  <header>
    <div class="container">
      <div class="row">
        <div class="col">
          <nav class="navbar navbar-expand-lg navbar-light">
            <a href="#" class="navbar-brand">
                            CompanyLogo
                        </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".collapse">
                            <span class="navbar-toggler-icon"></span>
                        </button>
            <div class="collapse navbar-collapse">
              <ul class="navbar-nav upper-controls">
                <li class="nav-item">
                  <a class="nav-link" href="#">Language</a>
                </li>
              </ul>
              <ul class="navbar-nav nav nav-pills nav-justified">
                <li class="nav-item">
                  <a class="nav-link" href="#">Home</a>
                </li>
                <li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">
                                        Categories
                                    </a>
                  <div class="dropdown-menu">
                    <a class="dropdown-item">Cat 1</a>
                    <a class="dropdown-item">Cat 2</a>
                    <a class="dropdown-item">Cat 3</a>
                    <a class="dropdown-item">Cat 4</a>
                  </div>
                </li>

                <li class="nav-item">
                  <a class="nav-link" href="#">Live Auction</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#">Contact us</a>
                </li>
              </ul>
            </div>
          </nav>
        </div>
      </div>
    </div>
  </header>


</body>

</html>

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

Eliminating the border around a textarea in HTML

Currently, I am dealing with the textarea tag in HTML and am trying to eliminate the border surrounding the box. Additionally, I am aiming to position the text at the bottom of the textarea. ...

Creating a 3-column grid in React using the map function is a simple and efficient way

Looking to arrange my cards in a specific grid pattern: [] [] [] [] [] [] [] [] [] ... ... Facing an issue with rendering items using the map function. I've attempted a solution, but it's not working as expected. To create a new row for every in ...

I need assistance with retrieving an image from a database using PHP

How can I display an image from the database on a new page using PHP? Whenever I click on the image, it always shows the same image on the new page. How can I make it so that the image displayed on the new page is the one I clicked on in the previous pag ...

Leveraging the power of Bootstrap 4 to place the footer beneath all remaining content on the

I am currently working on a React application that consists of a header, container, and footer. To add animation to the route loading process, I had to apply position: absolute to the container section of the page. Within the container, there is a row wit ...

What steps can I take to whitelist the necessary components for BootstrapVue in order to successfully integrate Purgecss with Nuxt.js?

After setting up a Nuxt.js project utilizing BootstrapVue for UI, I encountered a common issue with an overly large bundle size due to bundling bootstrap.css and bootstrap-vue.css in their entirety. To tackle this, I integrated nuxt-purgecss to eliminate u ...

Is it possible to incorporate MUI React components within an iframe?

Looking to replicate the style seen in this Material UI website screenshot, I aim to design a container that will encompass my iframe contents, such as the navBar and menu drawer. However, I want to utilize Material UI's components for these elements. ...

Turn off the perpetual active mode

I have a situation where a link maintains its active state after being dragged and released. This is causing an issue that I need to address. For example, you can see the problem here: http://jsfiddle.net/Ek43k/3/ <a href="javascript:void(0);" id="foo ...

Having trouble with my form validation for a university project. Not sure if it's a problem with the JavaScript or HTML

I had attempted to simplify things, but I'm at a loss as to what could be causing the issue. I'm uncertain if the return command is functioning properly or if the button is creating a problem, but I can't seem to pinpoint the exact issue. M ...

Bootstrap doesn't display in the dropdown menu

Struggling to get a dropdown menu to display on my page, my HTML code is: <div class="dropdown d-inline"> <div class="widget-header mr-3"> <button href="#" class="icon icon-sm rounded-circle border" data-toggle="dropdown"><i cl ...

Encountering an issue with Nuxt 3.5.1 during the build process: "ERROR Cannot read properties of undefined (reading 'sys') (x4)"

I am currently working on an application built with Nuxt version 3.5.1. Here is a snippet of the script code: <script lang="ts" setup> import { IProduct } from './types'; const p = defineProps<IProduct>(); < ...

Dynamically insert a new row into an HTML table using AJAX and refresh the table with .load method

I am facing an issue with my HTML table that loads data dynamically through a PHP snippet containing SQL queries. There is a Select option and a button on the table to add a new row, which triggers an AJAX procedure to send the data to PHP for insertion in ...

What is the process to showcase a database value in a particular index of an HTML table?

I am looking for assistance with displaying selected data values from a database into specific positions in an HTML table. I have managed to write the code that displays the data, but it only shows up at index 0 on the table. Does anyone know how to make ...

Applying a translucent layer of color to a jpeg image to create a subtle background effect

I am attempting to create a semi-transparent, solid background color on top of an <img> tag, while still showing the original image underneath at the same ratio and size. Below is a snippet of the code I am working with (let me know if you need more ...

Creating a div overlay triggered by the addition of a child tag

Using the Paypal zoid feature, I have a button that opens an iframe in the parent div when clicked. However, the iframe causes the other contents of the website to shift around, making it look messy. I'm wondering if there is a way to make the parent ...

Is anyone else experiencing issues with their imagemap due to Twitter Bootstrap?

I'm excited to ask my first question here as a beginner. I am truly grateful for all the assistance and guidance I have received from this site. I hope that the questions I ask can also benefit others in some way :) Although imagemaps may not be used ...

Is there a way to retrieve all CSS styles associated with a specific element?

When I come across a site element that I really like and want to incorporate into my own site, what is a simple way to do so? It can be challenging to navigate through numerous CSS files to find all the necessary styles. ...

Mysterious obsidian container lurking in the background of the video on Chrome version 67.0.3396

Displayed on the page is an HTML Video tag, which streams a video from the speaker (WebRTC). <div id="remoteVideoContainer"> <video id="remotevideo" autoplay="autoplay" controls="" loop="loop" preload="true" height="500" width="100%"> ...

Problem arising from animation not commencing at expected starting point

Creating a feature on an app that involves breathing exercises using CSS animations. The challenge I'm facing is ensuring the words "exhale" and "inhale" appear synchronously with the animation of a circle shrinking and enlarging. However, the animati ...

Displaying dynamic content in JavaScript using Galleria module

Hello and thank you for taking the time to check out my question. I've encountered a little issue that I can't seem to solve by myself. I'm hoping someone could lend me a hand. On my website, there is a dynamic field filled with a code from ...

Assign a CSS class to a DIV depending on the vertical position of the cursor

The website I am currently developing is located at Within the site, there is a collection of project titles. When hovering over a project title, the featured image is displayed directly below it. I would like to change the positioning of these images to ...