How to align Bootstrap navbar in the center both horizontally and vertically

I am new to using Bootstrap and I am trying to center the navbar both horizontally and vertically. I want it to look like this https://i.sstatic.net/gqwg9.png, but I am having trouble figuring it out.

This is my HTML code:

<html>
    <body>
        <nav class="navbar navbar-default">
          <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav" 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>
            </div>
            <div class="collapse navbar-collapse" id="nav">
              <ul class="nav navbar-nav"> 
                <li class="active"><a href="#home">Home</a></li>
                <li><a href="#portfolio">Portfolio</a></li>
                <li><a href="#blog">Blog</a></li>
                <li><a href="#pages">Pages</a></li>
                <li><a href="#features">Features</a></li>
                <li><a href="#mega-menu">Mega menu</a></li>
                <li><a href="#contact">Contact</a></li>
              </ul> 
            </div><!-- /.navbar-collapse -->
          </div><!-- /.container-fluid -->
</nav>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    </body>
</html>

and here is the CSS I have used:

nav {
    color: #fff;
    height: 112px;
    text-transform: uppercase;
    font-size: 15px;
    font-weight: bold;
}

Answer №1

To align the links at the center, you will need to add these styles to the .navbar-nav and li classes (you may also want to include these within a media query).

CSS for Centered Links

@media (min-width: 768px) {
  .navbar.navbar-default .navbar-nav {
    width: 100%;
    text-align: center;
  }
  .navbar.navbar-default .navbar-nav > li {
    display: inline-block;
    float: none
  }
}

You can adjust the vertical alignment of your links by using the line-height property.

Vertical Alignment for Links

@media (min-width: 768px) {
  .navbar.navbar-default .navbar-nav > li > a {
    line-height: 4;
  }
}

Example in Action:

.navbar.navbar-default {
  background: white;
  border: 1px solid transparent
}
.navbar.navbar-default .navbar-nav.navbar-center > li > a,
.navbar.navbar-default .navbar-search > li > a {
  color: #266080;
}
@media (max-width: 767px) {
  .navbar.navbar-default {
    padding: 15px 0;
  }
  .navbar.navbar-default .navbar-collapse {
    margin-top: 15px;
    margin-bottom: -15px;
    border: 0;
    box-shadow: none;
  }
}
@media (min-width: 768px) {
  .navbar.navbar-default {} .navbar.navbar-default .navbar-nav.navbar-center {
    width: 100%;
    text-align: center;
  }
  .navbar.navbar-default .navbar-nav.navbar-center > li {
    display: inline-block;
    float: none
  }
  .navbar.navbar-default .navbar-nav.navbar-center > li > a {
    line-height: 4;
    text-transform: uppercase;
    font-size: 14px;
    font-weight: bold;
  }
  .navbar.navbar-default .navbar-search {
    position: absolute;
    right: 2%;
    top: 17px;
    line-height: 4;
    font-size: 20px;
  }
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">

    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#nav" 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>
    </div>

    <div class="collapse navbar-collapse" id="nav">
      <ul class="nav navbar-nav navbar-center">
        <li class="active"><a href="#home">Home</a>
        </li>
        <li><a href="#portfolio">Portfolio</a>
        </li>
        <li><a href="#blog">Blog</a>
        </li>
        <li><a href="#pages">Pages</a>
        </li>
        <li><a href="#features">Features</a>
        </li>
        <li><a href="#mega-menu">Mega menu</a>
        </li>
        <li><a href="#contact">Contact</a>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right navbar-search">
        <li><a href="#"><span class="glyphicon glyphicon-search"></span> </a>
        </li>
      </ul>
    </div>

  </div>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

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

Working with PHP and CodeIgniter to highlight active nav-link in Bootstrap for a subdirectory

Just starting out with Bootstrap and running into a little issue. Spent some time on it but can't seem to figure it out. Perhaps it's something simple that I'm overlooking. Essentially, my goal is to keep the nav-link active when navigating ...

Having difficulties with building a HTML layout using Bootstrap functionality

I am struggling to recreate a specific view using Bootstrap. The image of the view I want to achieve can be seen here: https://i.sstatic.net/VtaMc.png My attempt at coding this view using Bootstrap is as follows: <div class="col-md-12"> <di ...

Responsive center alignment of stacked `<div>` elements using CSS

My goal is to center a stack of divs in 3 columns per row if the browser window is wider than 1024px, and switch to 2 columns per row if it's smaller, while still displaying all 6 divs. Unfortunately, I'm facing difficulties trying to apply what ...

Position the div at the center of its parent container, disregarding any floating

I am attempting to generate a 'section' division with a header. The goal is for this header to have a centered title and the possibility of including a jquery UI button on its right side. The width of the 'section' division will be dete ...

Restoring text boxes to their original styling

I've been working on a jQuery script that scans through form input elements and turns their border color to red if they are empty. When the submit button is pressed, it reassesses the elements; this time I'm trying to revert the textbox back to i ...

The overlay menu in Jquery version 1.10.1

In a nutshell, my issue is this: I am looking to create split-buttons with a menu inside (achieved using jquery-ui as shown here: jqueryui-demo). However, I want these buttons to not impact the positioning of other elements. The second button should remain ...

What could be causing the malfunction of the dropdown menu attached to my button?

I've been working on setting up a simple button in my Angular application that should display a dropdown menu with various options when clicked. I copied and pasted some Bootstrap template code, made some modifications to match the style of my app, bu ...

Switch videos within the video tag as you scroll

I am trying to implement a video tag within a div element on my website. Here is the current code: <div class="phone" align="center"> <div class="phone-inner"> <video width="100%" height="100%" autoplay="" loop="" muted id="phonevideo ...

Can two BootstrapVue tooltips be displayed on a single element with different target settings?

While working in Vue with BootstrapVue, I encountered a problem where the bottom 2 tooltips that I am using both appear on the button targeted by the click to mask this value button. The tooltip containing the SSN is also only appearing on top of the butto ...

increasing the vertical space between two divs on the right side

I am trying to stack divs on top of each other, with the lower div shifted to the right. I want the output to look like this https://i.sstatic.net/eciXT.png Currently, I have achieved this by using the following code: <div id="div1" class="checkbox"& ...

What is the reason behind v-container in vuetify.js automatically setting the fluid property when zoomed to 67%?

I have encountered an issue with vuetify where the v-container automatically takes full width at 67% zoom of the browser, even though I have not used the fluid property. It seems to work fine for the rest of the cases. <v-container> < ...

Steps for dynamically loading the correct pane without having to refresh the header, footer, or left navigation

Looking to create a web application using HTML, Bootstrap, and jQuery that includes a header, footer, left navigation, and right pane. The content of the right pane will be loaded from a separate HTML page based on what is clicked in the left navigation. ...

Unending Jquery Loop Iteration

I encountered a bug while writing code to display JSON data in an HTML table. Even though I only have 25 IDs to print from the JSON, my code is printing them more than 10 times when it should run only once. I tried adding breakpoints in the code but still ...

Update the parent node in the Google Org Chart

This is my first time working with Google Charts and I have a couple of questions: Is there a method in the API to update the parent of an existing node? I am facing a challenge where I need to build a tree structure top-down, which means that I know the ...

AngularJs fails to apply style to dynamic content in IE8 when using HTML5

Currently, I am in the process of developing a website with AngularJs and utilizing numerous JS scripts to address compatibility issues with Internet Explorer. I have organized the data in JSON format, and each page is designed to fetch and display differ ...

How can I prevent CSS classes from being applied to the submit button in jQuery UI?

Currently, I am utilizing jQuery UI with a customized theme on my website. One issue I have encountered is related to an <input type="submit"> element present on the page. Due to jQuery being active, this button automatically inherits the jQuery UI a ...

Expanding the content of :before

I have implemented the code below to enable a hover effect when users hover over an image: &:before ' content: "example image" Background: #444; Padding: 20px Height: 300px Width: 300px Font family: 'open sans' Opacity: 0; ' &: ...

Switching image display through data toggle

<a href="#demo1" data-toggle="collapse"><h3 style="color:#0F77CD;">Form</h3></a> <div id="demo1" class="collapse"> <asp:CheckBox ID="CheckBox1" runat="server" Text="Capsules" /><br /><br /> <asp:Check ...

When there are multiple elements within the parent container, the CSS hover margin-top alteration fails to take effect

I am currently working on creating a dynamic menu bar that has elements which smoothly move upwards on hover. However, I have encountered an issue where this movement effect only works when the parent container contains exactly one element (such as in titl ...

Utilizing CSS background sizing with jQuery for stunning website aesthetics

In my HTML file, there is a main div container with two nested divs inside it. Each of these inner divs has been assigned separate ids for content placement. By implementing a functionality that changes the background image of the parent div upon clicking ...