Uniform widths in bootstrap 4 navigation bar

Seeking assistance with customizing a navbar using Bootstrap 4 and flexbox. The desired design is to have the logo centered, with equal space on either side for the links.

         --------------------------------------------------------------
|         |    Link  |   Link  |   Logo   |  LongerLink  |  LongerLink |        |
         --------------------------------------------------------------

The problem I'm facing is that the LongerLink items are taking up more space than the shorter Links, causing the logo to be off-center. How can I resolve this issue? See code below:

.navbar {
  background-color: #389FCE;
  height: 100px;
  display: flex;
}

.navbar-nav {
  width: 100%;
  display: flex;
  flex: 1;
  min-width: 0;
  justify-content: space-between;
  align-items: center;
}

.nav-link {
  font-family: 'Nexa-Bold', sans-serif;
  font-style: normal;
  font-weight: 700;
  font-size: 20px;
  color: white;
  line-height: 80px;
}
<nav class="navbar navbar-expand-sm">
  <button class="navbar-toggler" data-toggle="collapse" data-target="#collapsibleNavbar" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon">
      <i class="fas fa-bars" style="color:white; font-size:28px;"></i>
    </span>
  </button>

<div class="collapse navbar-collapse" id="collapsibleNavbar">
    <ul class="navbar-nav nav-fill w-100">
      <li class="nav-item">
        <a class="nav-link" id="shortLink" href="#">HOME</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="shortLink" href="#">ABOUT</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">
          <img src="images/Logo_navbar.png">
        </a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="longLink" href="#">PRODUCTS</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="longLink" href="#">CONTACT</a>
      </li>
    </ul>
  </div>
</nav>

Answer №1

To achieve this, you must specify widths depending on the number of items. There isn't a flexbox property that will automatically handle it for you.

.nav-fill .nav-item {
    flex: 1 0 20%; /* 5 items each at 20% width */
    text-align: center;
    border:1px solid blue;
}

Sample Codepen Demo

Answer №2

To optimize the nav-item's layout, set its flex-basis to 100% instead of using auto. Allow the items to shrink, but ensure you also specify a minimum width as mentioned here...

.nav-item {
    flex: 0 1 100%;
    min-width: 0.1px;
}

Additionally, consider how you want to handle overflowing text when longer links are wider than the rest. Utilize text-overflow: ellipsis; as shown below...

.nav-link {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

See a live demo here: https://codeply.com/p/N8MvGaunOR

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

Instructions on generating a thumbnail image for an HTML page using the class "thumbnail"

I am trying to replicate the thumbnail feature used by the Chrome browser for representing most visited sites. I noticed a class called "thumbnail" in the inspection tool, but I am not sure how to implement it. Are they using scripts or is it just CSS or ...

Having trouble with charts not appearing on your Django website?

I am working on a Django project where I need to integrate bar-charts using Django-nvd3. Although I have successfully displayed the bar-charts in separate projects, I am facing an issue with integrating them into my current project. Below is the code snipp ...

What causes the inconsistency in width distribution among flex children when the flex grow property is set to 1 alongside text content?

Why do the elements one, two, and damn not take up equal space, as they all have flex: 1? I want them to have equal widths based on the parent's width. How can I adjust it so that the element with class damn takes up less space than the other two? .w ...

What is the process of extracting attribute values from child elements in XPath for HTML documents?

Initially, I searched for all the necessary elements, but now I am encountering errors while trying to retrieve attribute values from the child elements - title, URL, and image. What could be the mistake in my approach? function getContent($value) { ...

Tips for locating the previous sibling of an HTML tag using only CSS

What is the method for locating adjacent elements in HTML using only CSS? <div class="form-group"> <label for="userID">User ID</label> <input id="userID" type="text" class="form-control" placeholder="Enter User ID" tabindex="1"/ ...

What is causing the animate callback to not properly wait for the completion of the animations?

Both animations are triggered simultaneously (sometimes even multiple times).... <div id="home-bt" class="button"></div> <div id="about-bt" class="button"></div> <div id="beauty-bt" class="button"></div> <div id="pam ...

What could be causing the vertical alignment issue of this logo in its parent container?

I am having an issue with the vertical centering of the logo element within the <header> container on this page. It seems to be more pronounced on mobile devices compared to desktop. However, the second element (#forum-link) is aligning correctly. W ...

The compatibility of Bootstrap 4 with Internet Explorer 11 is not optimal

I am facing an issue where my entire website is not functioning properly in IE 11. Despite the fact that Bootstrap 4 should officially support IE 11, I'm unsure of what the problem might be. Website: www.ergotherapie-klinkicht.de I feel a bit lost at ...

The div element is finally loading properly after multiple clicks

I need some assistance with loading dynamic divs in Angular. I have created a button that adds new divs each time it is clicked in a specific area. However, once these new divs are added, they appear incorrectly: https://i.sstatic.net/sAE6q.png After add ...

The Precision of Nesting CSS Selectors

I'm really puzzled about why "Item One" is not displaying as orange below. By specificity rules, the selector .one.two should have a score of 20 (two classes), while .one li should have 11 (one class, one element). So logically, it seems like it shoul ...

Exploring optimal practices for CSS components and responsive design integration

If I were to develop a reusable component like a table that will be used across various pages, the content and number of columns may differ from one page to another, but the overall design should remain consistent. .my-table { background-color: ... et ...

Drawing the canvas is taking place prior to the image being fully loaded in JavaScript

Currently, I am in the process of creating a collage using images that are all sized at 174x174 pixels. The images are stored on my server, and while my program can locate them successfully, it requires me to click the "save image" button twice for them to ...

Guide to removing text color from a background with css or javascript

Is there a way to achieve the effect shown in this image on my HTML page: http://prntscr.com/b912c0? I've attempted using mix-blending modes, but haven't been successful in recreating the desired result. Any tips or tricks for accomplishing this ...

Displaying the content of a Bootstrap 4 row on top of the navbar

Just starting out with Bootstrap and recreated a page layout similar to the starter template on the Bootstrap website. However, I'm facing an issue where the row content overlaps the navbar, despite using the align-items-center utility class on the ro ...

Expanding the functionality of Django with the extends tag

I'm utilizing the extends tag to streamline my HTML files and prevent repetitive code. For this purpose, I have created a Base.html file and Home.html located in: Etat_civil |__Home |__templates |__Home.html |__BirthCertificate |__Identity ...

Alignment of React page gets messed up upon inclusion of Gallery component

I have searched for solutions online, but nothing seems to work for me. I am facing an issue with my webpage that has multiple sections, and I want to display them one after the other vertically. Here is the code snippet: import React from 'react&ap ...

Having issues selecting an option from a select input in Safari while using Selenium

Currently, I am utilizing Selenium in combination with Mink to automate the registration process on several of our websites. In order to proceed with the registration, it is necessary to select your birth date from three different drop-down menus. My appr ...

Ways to combine text styling with images, stroke effects, and shadows all at once

I am looking to enhance my text with a background image, a text-stroke, and a text-shadow. The issue I am facing is that the text-shadow appears on top of the background image. Does anyone have a solution for placing the shadow behind the image? If you te ...

What is the best way to incorporate external CSS files into AMP?

Struggling to load external CSS files such as bootstrap.css and other stylesheets into an HTML file that is AMP-compatible. I've scoured the AMP documentation but have yet to find a solution. As a newcomer to both AMP and SEO, I need to implement SEO ...

Assistance required in creating a numerical list from an array of objects

I'm currently facing an issue with creating a numbered list from an array of objects. Below, you'll find the code containing the objects. You need to add the necessary TS code to display the atom names along with their weights in a numbered list ...