Struggling to properly position navigation bar items in a Bootstrap website

I'm struggling to align the bar items on a new site that I've built. I've tried placing two bars at the top to mimic the layout of this site: . I'd like it to look similar to this site: , where everything is nicely centered and mobile-friendly. However, I can't seem to figure out how to replicate that layout.

I've attempted to center the bars using the following code snippets:

<nav class="navbar navbar-expand-lg bg-dark border-bottom" id="mainNav">
    
    <div class="container-fluid"> 

        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
          <i class="fas fa-bars" style="color: #fff"></i>
        </button>    

        <div class="collapse navbar-collapse w-100" id="collapsibleNavbar">

            <a class="navbar-brand" href="<?= $url ?>">
                <img src="<?= $url ?>images/img-logo.png" alt="<?= $url ?>" class="d-block d-sm-inline-block mw-100 mx-auto">
            </a>

            
        </div>

    </div>
    
</nav>

<nav class="navbar navbar-expand-lg bg-dark border-bottom" id="mainNav">
  <div class="container-fluid">
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
      <i class="fas fa-bars" style="color: #fff"></i>
    </button>

    <div class="collapse navbar-collapse w-100" id="collapsibleNavbar">
      <div class="d-flex justify-content-center w-100">
        <div class="d-flex align-items-center">
          <a class="navbar-brand me-3" href="https://www.digitalproductscorner.com/">
            <img src="https://www.digitalproductscorner.com/images/img-logo.png" alt="https://www.digitalproductscorner.com/" class="mw-100" style="width: 100px; height: auto;">
          </a>

          <ul class="nav nav-pills nav-fill flex-column flex-sm-row mb-3" style="display: flex; gap: 10px;">   
            <li class="nav-item"><a href="https://www.digitalproductscorner.com/" class="nav-link ">Home</a></li>
            <li class="nav-item"><a href="https://www.digitalproductscorner.com/about.php" class="nav-link ">About</a></li>  
            <li class="nav-item"><a href="https://www.digitalproductscorner.com/contact.php" class="nav-link ">Contact</a></li>  
          </ul>
        </div>
        
       
        <ul class="nav navbar-nav ms-auto">
          <li>    
            <form action="index.php" method="get" class="d-flex mb-3">
              <input type="text" name="q" class="form-control" placeholder="e.g. health or golf etc" required>
              <button type="submit" class="btn btn-success ms-2"><i class="fas fa-search"></i></button>
            </form>  
          </li>           
        </ul>
      </div>
    </div>
  </div>
</nav>

The only element that appears correctly is the top search box. My CSS is relatively simple at this point:

.nav-fill .nav-item {
    font-weight: 500;
}

.nav-pills .nav-link {
    background-color: #fff;
    border: 1px solid #ccc;
    color: #333;
    font-weight: bold;
    margin-right: 10px !important;
    padding: 0.5rem 1rem;
    text-transform: uppercase;
}

.navbar-brand {
    display: flex;
    margin-bottom: 15px;
}

.nav.navbar-nav.ms-auto {
    display: flex;
    margin: auto;
}

footer {
    background-color: black;
    left: 0;
    position: absolute;
    right: 0;
    width: 100%;
}

Even copying the code line by line from this site: doesn't seem to solve the alignment issue on my site.

Answer №1

To meet your request of centering the nav-pills, simply remove the margin-left property from the nav-pills element and apply the following CSS:

    flex: 1 1 auto;
    justify-content: center;

Additionally, make sure to update the .nav-fill .nav-item by using flex: 0 0 auto;

I recommend delving deeper into the workings of flexbox for a comprehensive understanding. This resource on flexbox will enhance your knowledge:

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

Utilizing browser local storage in web development

Currently, I am in the midst of working on an e-commerce platform, a project that holds significant importance for me as it marks my debut into major projects. For the first time, I am delving into the realm of local storage to manage basket data such as q ...

Integrating HTML, JavaScript, PHP, and MySQL to enhance website functionality

Exploring the intricacies of HTML, JavaScript, PHP, and MySQL, I have been working on an order form to understand their interactions. View Order Form The aim of this table is to allow users to input a quantity for a product and have JavaScript automatica ...

Issue with PHP redirection not functioning as expected upon submission

Thank you for taking the time to offer assistance. I am encountering an issue with a basic HTML contact form that utilizes a PHP file for processing. The form's method is set to post and it directs its action to a PHP file. Upon completion of the PHP ...

Ways to modify the font style of Python comments in JupyterLab's code cells and code editor:

Many resources discuss how to change font families for all content, but I am seeking guidance on making syntax-specific changes. For example, I would like to switch the font style of Python comments from italic to normal. Can anyone offer assistance? Thank ...

When the screen size changes, the sidebar in Bootstrap 5 smoothly reveals hidden content

I've been working on a sidebar design and everything seems to be going well so far. However, I've encountered an issue that has been giving me trouble for days now. Whenever I display a new page and then resize the screen, the content of the page ...

Developing an IF statement in JavaScript that relies on hexadecimal color values

I've created a JavaScript code that changes the background color of my webpage every time it loads: document.getElementById("band").style.background = '#'+(Math.random()*0xFFFFFF<<0).toString(16); To improve visibility, I am aiming t ...

tips for aligning text to the right of an image

For my college project, I am working on a website. However, I have encountered an issue with using a jsp script to display products in a specific way on the webpage. The problem is that I cannot control where the information about price, name, and descript ...

Center-align an input and button vertically within a div

I am facing an issue with aligning my search setup vertically inside a div Below is the code I have written: HTML: <div class="search"> <input class="searchfield" type="text" placeholder="Search..." value="" />&nbsp;<button class="sea ...

How to retrieve FormData data using Axios

One challenge I'm facing is with an application that I have developed using VueJS, where users can upload files and send them to the backend. The process involves using Axios to send the file as FormData. However, once the file is received on the back ...

Engaging JavaScript Navigation

I am looking to create an interactive JavaScript menu or image map where users can press down, highlight, and hit enter on four different items to reveal hidden messages. However, I struggle with JavaScript and could really use some assistance. I have alr ...

Creating Dynamic Videos with PHP and HTML

Trying to display the user-submitted video, but all my attempts failed for some reason: 1. echo "<video width='500px'>"; echo "<source type='video/mp4' src=$var autoplay>"; echo "</video>"; echo "<video width=& ...

How can the printing of content be adjusted when the browser zoom function is activated?

Is there a way to prevent the content from zooming when printing while the browser is zoomed in? The goal is for the printing (using iframe) to remain unchanged even if the browser is zoomed. I attempted: document.body.style.transformOrigin = 'top le ...

How to modify an array in an HTML document using BeautifulSoup

Running a Python script, I am trying to access a local HTML file containing a JavaScript array inside a script tag that needs updating. Here is the test code snippet: from bs4 import BeautifulSoup html = ''' <script> var myArray ...

Comparison between Bootstrap 4 and Firefox Preload Error

Hello fellow front end developers who are experiencing an issue with CSS preload in Firefox versions above 74.0. I have encountered a problem with the following code snippet: <link rel='preload' as='style' onload="this.onload=null; ...

How do I ensure that my absolutely positioned element is centered in this specific scenario?

I've been working on centering an element within its parent container, and I'm having trouble ensuring it remains centered responsively. Here's what I have so far: HTML <div class="example"> <span class="element1">Element ...

Express.js problem: Unable to load CSS and JS files from the 'public' directory

Currently, I am working on a web project with Express.js and encountering difficulties in loading CSS and JavaScript files from the 'public' folder. Despite following common practices, it seems that something is amiss. Here is the layout of my pr ...

The controller remains unresponsive when attempting to activate the button on the webpage

I'm a beginner in frontend development and I'm currently working on a website that uses Thymeleaf. On this website, there is a div block with a button. Please take a look at the HTML code below, specifically the button at the last three rows. &l ...

"Enhance Your Website with Dynamic Text Effects using JavaScript

Is there a way to continuously animate text during the loading process of an AJAX request? I've tried implementing various methods, such as using a setTimeout function or an infinite loop, but nothing seems to work for repeating the animation once it& ...

Always ensure that only one div is visible at a time

I am currently working on a project where I cannot use ng-cloak due to the way the css files are loaded. I have been exploring alternative solutions and have tried a few different approaches. My main goal is to ensure that two icons are never shown at the ...

What causes the burger dropdown to be concealed by additional content?

While viewing my menu on an iPhone SE in responsive mode, I noticed that all of my burger menu items are hidden except for the one that fits perfectly in the space between my header and main content. https://i.sstatic.net/7K9FD.png I am using Mantine wit ...