How to align li elements at center in a Bootstrap navbar

I'm having trouble centering link1 and link2 both horizontally and vertically on the page. The horizontal alignment is working fine, but I can't seem to get the vertical alignment right, no matter what I try.

Here is the HTML code snippet:


      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <!-- <%= link_to 'Home', root_path %> -->
      </div>

      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li>Link 1</li>
          <li>Link 2</li>
        </ul>
     </div>

  </div>
</nav>

And here is the CSS code snippet:

.navbar .navbar-nav
  display: inline-block
  float: none
  vertical-align: middle

.navbar .navbar-collapse
  text-align: center

/* styling for links */
.navbar-nav > li
  margin: 3px
  text-transform: uppercase
  font-weight: bold
  color: white

Can anyone help me with a solution to center link1 and link2 both vertically and horizontally? Also, should I be using Bootstrap or developing everything with custom CSS?

Answer №1

Include the following CSS code in your stylesheet:

.navbar-nav {
    width: 100%;
    text-align: center;
    background: red;
}
.navbar-nav>li {
    float: initial;
    line-height: 150px;
}
.nav>li {
    display:inline-block;
}

The line-height property determines the height of the li elements, ensuring they are centered within the block. Hopefully this achieves the desired result for you.

You can view a live demo on Bootply here: http://www.bootply.com/1Tiu9JzEBN#

Answer №2

.navbar-nav>li {
    height: 100px;
    line-height: 100px;
    margin: auto;
    text-align :center;
}

.navbar-nav > li span {
    vertical-align: middle;
    display: inline-block;
    line-height: 1.2em;
}
    <div class="navbar-collapse expand">
        <ul class="nav navbar-nav custom">
          <!-- <li><%= link_to 'Home', root_path %></li> -->
          <li><span>Link One</span></li>
          <li><span>Link Two</span></li>
        </ul>
     </div>

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

Unlocking the Potential of Larger jQuery Icons

In a recent announcement, the jQuery team unveiled new icons for their UI components. However, I have been unable to locate any examples of how to use them or where they can be downloaded from. Also, it appears that the themes in the ThemeRoller only provi ...

Arrange a div within a list element to create a grid-like structure

My current structure is as follows: <ul class="wrap-accordionblk"> <li class="accordionblk-item"> <div class="accordionblk-header"> <div class="row-fluid"> <div class="infoblk"> <label>SESSION ID ...

Enhance your HTML checkboxes with this stylish jQuery plugin!

Similar to the input tag in the style of StackOverflow, I stumbled upon a jQuery plugin that achieves the same effect. Unfortunately, I can't remember the name. Can someone please assist me? ...

Setting a specific time for a div element with opacity: A step-by-step guide

Is there a way to adjust the timing for the appearance of the add-to-cart when hovering over the product-list-item? Currently, it appears when I hover over the item and disappears when I move my mouse away. .product-list-item { position: relative; w ...

Minimizing the menu bar while scrolling down (bootstrap3)

Could anyone help me with creating a navigation-bar effect similar to the one seen on ? I want the bar to shrink and the logo to change after scrolling down my page. My website is built using bootstrap 3. Is there a simple way to achieve this effect with ...

Convert the canvas to an image by right-clicking

Is it possible to treat the canvas element as an image when using drawImage() to draw on it? When I attempt to right click on the canvas element that has been drawn on, the option "Save image as" does not appear. The right click menu displays: What step ...

Press a single button to toggle between displaying and hiding the table

$(document).ready(function() { $("#t1").hide(); // hide table by default $('#sp1').on('click', function() { $("#t1").show(); }); $('#close').on('click', function() { $("#t1").hide(); }); }); <li ...

Adjust the parent div's background when hovering over it

Here is the code snippet I am working with: <div class="thumb_image_holder_orange"> <img src="http://dummyimage.com/114x64/000/fff.png" /> </div> I want to change the background of the div when hovering over the image, rather than j ...

The Viadeo Social Toolbox seems to be encountering technical difficulties at the moment

I attempted to utilize a Viadeo Social Toolbox, specifically the Viadeo Share Button, but it seems to be malfunctioning in certain browsers? I came across some outdated viadeo share URLs like this: http://www.viadeo.com/shareit/share/?url=${url}&title ...

Using a custom font in a Django project without relying on the Google Fonts API

I've hit a roadblock in my programming journey and I'm seeking help. As a newcomer to both programming and Django, I've been following a helpful tutorial up until part 4. Everything was going well until I stumbled upon a new font online cal ...

Fill the list items with values from the given array

I have an array called var array = ["one", "two"]; and I want to display each element in a separate list item. The desired output is: <li>Number one</li> <li>Number two</li> However, the current output is: <li>Number onetw ...

How can I create clickable table entries using php and html?

I want to create a table on this page that is clickable. When the user clicks on a row, the data from that row will be sent to a PHP file with a form prepopulated with the selected user's information for updating purposes. <!DOCTYPE html> &l ...

Mysterious sayings encircling the words fetched through ajax

If the localhost is pointing to the folder named www, where the structure looks like: www/ file/test.cpp index.html I want to dynamically load the content of test.cpp into index.html and display it with the help of highlight.js. Below is the cod ...

CSS list items do not expand

I have a list with links inside each item and I want the item size to remain fixed regardless of content. Below is the CSS code: .files { display: inline-block; padding: 0; margin: 0 auto; } .files li { display: inline; } .files l ...

Display the column every time the user types something into the search bar

Currently working on an Angular project and I'm trying to figure out how to make the middle column visible whenever the user enters text in the search bar. At the moment, I have a search bar for user input and three flex columns. The middle column is ...

While browsing in Firefox and moving the cursor over a sub element

While hovering over a child element in Firefox using the rule .parent:hover > .child { /*style*/ }, the child element is considered separate from the parent element and does not receive styling. For example, in Firefox when you hover over the button, on ...

How can I align a div in a vertical manner if it is displayed as a circle?

This is the creation I have come up with: This is the design I am aiming for: Below is the code I have written: <div id="OR"><span style=" vertical-align: middle;background:blue;">OR</span></div> Here is the corresponding CSS: ...

revealing a particular field in jQuery during the validation process

Edit: Currently, I am utilizing the jquery validate plugin and have hidden all error messages initially. Upon checking a specific input field, my objective is to unhide the error message if it is invalid without causing error messages to appear in other f ...

The form data is being passed as null to HttpContext.Current.Request

My file upload module is working well with Postman without any content type. However, in the code, the file count always shows as 0 in the backend API. If anyone has any insights into what I might be doing wrong, please help me out. Thank you. Below is my ...

Inquiries regarding jQuery's functionality and efficiency

I was wondering about the best way to improve performance? Would it be more efficient to have two images written in HTML, one visible and one hidden, and then use jQuery to toggle their display (hiding the visible one and showing the hidden one)? <img ...