Align the Bootstrap Nav Bar in the Center

<!-- Unique Header -->
    <nav class="navbar navbar-default" role="navigation">
      <div class="container-fluid">

        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#a-navbar-collapse" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>

          <a class="navbar-brand" href="#">
            Brand
          </a>
        </div>


       <div class="navbar-collapse collapse text-center"  id="a-navbar-collapse">  
           <ul class="nav navbar-nav">
                     <li><a href="#">Link 1</a></li>
                     <li><a href="#">Link 2</a></li> 
                     <li><a href="#">Link 3</a></li>
                     <li><a href="#">Link 4</a></li> 
                     <li><a href="#">Link 5</a></li> 
           </ul>


              <ul class="nav navbar-nav navbar-right">
                 <li><a href="#">Link 6</a></li>
                 <li><a href="#">Link 7</a></li>
              </ul>
        </div> 
       </div>
    </nav>
    <!-- End of Unique Header -->

This is my innovative HTML Code for a Navigation Bar - I prefer to display the brand at the left corner, while Link 1 to Link 5 are positioned in the center when expanded. However, upon collapsing, all links should align to the left.

Need assistance in maintaining alignment dynamics based on the screen width during collapse and expand modes.

Your feedback is welcomed! Apologies for any language limitations.

Answer №1

To center align a group of links in your navigation bar, you can use the following CSS code: Simply add class=navbar-center to the <ul> element that contains the links you want to be centered.

Example:

@media (min-width: 768px) {
  .navbar-nav.navbar-center {
    position: absolute;
    left: 50%;
    transform: translatex(-50%);
  }
}
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/libs/jquery/1.12.1/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<nav class="navbar navbar-default" role="navigation">
      <div class="container-fluid">

        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#a-navbar-collapse" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>

          <a class="navbar-brand" href="#">
            Brand
          </a>
        </div>


       <div class="navbar-collapse collapse"  id="a-navbar-collapse">  
           <ul class="nav navbar-nav navbar-center">
                     <li><a href="#">Link 1</a></li>
                     <li><a href="#">Link 2</a></li> 
                     <li><a href="#">Link 3</a></li>
                     <li><a href="#">Link 4</a></li> 
                     <li><a href="#">Link 5</a></li> 
           </ul>


              <ul class="nav navbar-nav navbar-right">
                 <li><a href="#">Link 6</a></li>
                 <li><a href="#">Link 7</a></li>
              </ul>
        </div> 
       </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

My project is experiencing issues with the execution of JavaScript codes

Greetings, I'm having trouble with my JavaScript codes. The following code works perfectly: function Choice() { var box = document.getElementById('searchh'); if (!count) { var count = 0; } box.onclick = function () ...

Display sub navigation when clicked in WordPress

I currently have the default wordpress menu setup to display sub navigation links on hover, but I am interested in changing it so that the sub navigation only appears when the user clicks on the parent link. You can view my menu here https://jsfiddle.net/f ...

"Drag and drop elements that automatically snap to a grid and move with

I'm trying to create a small, square div that follows the mouse cursor, but I want it to snap to a 3x3 pixel grid. Here is what I have so far: $(document).mousemove(function(e) { window.x = e.pageX; window.y = e.pageY; if(window.x % 9 === 0 ){ ...

AJAX enhancing the functionality of the webgrid with dynamic filtering

I'm encountering a problem with implementing JS on my webgrid for sorting purposes. The scenario is that a user enters a string into a textbox and the webgrid below is refreshed (filtered based on matching results) without the entire page being refres ...

jquery expanding the width of the final element

Is there a way to increase the width of the last 'th' in a string? I want it to be the current width plus 20px. Here is the code on Fiddle var header="<th id='tblHeader_1' style='width: 80px;'><span>Id</span&g ...

Continue running the ajax request repeatedly until it successfully retrieves results

At the moment, I am using a basic ajax call to retrieve data from our query service api. Unfortunately, this api is not very reliable and sometimes returns an empty result set. That's why I want to keep retrying the ajax call until there are results ( ...

Failure to send Websocket connection

Currently working on PHP, here's a snippet: $room_array = array(); $room_array[0] = 'room-list'; $room_array['info'] = array('room_name' => $room['room_name'], 'owner' => $username['usernam ...

Ways to bring in external javascript files in reactjs

I'm currently working on a form that requires the user to input their location. To achieve this, I have integrated the npm package react-geosuggest-plus. However, I want to avoid including <script src="https://maps.googleapis.com/maps/api/js?key=AI ...

Placing a Tooltip in the Right Spot

I am currently experimenting with adjusting the positioning of my tooltips to appear on the left side of the cursor instead of the right side. I am using a jQuery plugin called EasyTooltip. My attempt to set a negative value in the header's call for ...

Issues with Weglot link hooks not functioning properly within the sticky header

I have integrated Weglot for translations on my website, aigle.ca. Due to issues with their widget, I am using link hooks instead. You can find more information about link hooks at: weglot.com link-hooks However, when scrolling down the page and the menu ...

Adding information using AJAX jQuery in ASP.NET

I'm facing an issue and I can't seem to figure out what's wrong... <script type="text/javascript"> $(document).ready(function () { $("#SendCommentBtn").click(function () { $.ajax({ ...

Forwarding users to a new destination through a script embedded within a frame

Currently, I am facing an issue where a page (lobby_box.php) is being loaded every 4 seconds on my main page (index.php) using JavaScript. The problem arises when the code within (lobby_box.php) that is meant to redirect the client from index.php to anothe ...

Dispatching $emit / $broadcast events from multiple areas of the code and capturing them in a single location

I have a practice of sending $emits (or $broadcasts) from various parts of the code and different controllers, but intercepting them all from one centralized place. While this approach seems to be functioning properly, I am unsure if it may be considered b ...

Implementing a play and pause button functionality for the carousel in Bootstrap 5

How can I implement a play/pause feature on the same button when clicking in a Bootstrap 5 carousel using jQuery? **HTML ** <div id="customSlider" class="carousel slide" data-bs-ride="carousel" data-bs-interval="2500& ...

Creating a jQuery Mobile Multi-Select feature with dynamically loaded data

I'm facing an issue with integrating jQuery Mobile and AngularJS. I need a multi-select option for users to choose categories, which have parent-child relationships. The data structure is fetched asynchronously from a database. The problem is that t ...

Exploring the versatility of CSS variables in JavaFX

In need to style four buttons in a consistent way, but each one should have its own unique image. Three styles are required: one for normal state, and the others for hover and focused states. Using CSS for this task would involve a lot of repetition, as ...

The orientation of the mesh in relation to its parent entity

Currently tackling a project involving the creation of a 3D Widget for rotating, scaling, and translating a mesh. Facing challenges with positioning the cone used for scaling the parent mesh. Here is the code snippet in question: function generateScaleWid ...

How can you retrieve script data within HTML and PHP tags?

I am currently working on a web application using CodeIgniter-3 and I have encountered an issue with a form that contains two dropdowns which are dependent on each other. When a selection is made in the first dropdown, the data in the second dropdown sho ...

Is it necessary for me to validate the type before making each database request?

Currently, I am in the process of honing my error-handling skills within Express while using a Postgres database. My backend code consists of numerous small functions that serve as intermediaries for handling interactions with the database in larger funct ...

Layers in leaflet not displaying styles

Having trouble styling the layers received from a server. I've tried using the layer.setStyle() function and defining the style when creating the layer, but nothing seems to work. Here's how my code looks: var stateStyle = { "color": "#3D522 ...