Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center unordered list navbar - bootstrap 3

So, Im trying to center my nav-bar list items. Since there isn't a utility function for this task, I devised the following code that places the unordered list in a column within a row. But the list is still justified to the left even after I try centering with the old 'text-align:center'

<div class="navbar navbar-fixed-top ">
    <!--<a class="navbar-brand" href="#">Title</a> -->
    <div class= "row">
        <div style="border:1px solid black;text-align:center;" class="col-4 col-offset-4">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Projects</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
    </div>
</div>
like image 589
prufrock Avatar asked Jul 28 '13 04:07

prufrock


People also ask

How to align navbar items to center using bootstrap 4?

- GeeksforGeeks How to Align Navbar Items to Center using Bootstrap 4 ? In Bootstrap the items can be easily assigned to the left and right as it provides classes for the right and left. By default, left was set, but when it comes to aligning items to the center you have to align it by yourself as there are no in-built classes for doing this.

How do I Center a list in the navigation bar?

3 Answers 3 ActiveOldestVotes 23 Steps I took: Remove float: leftfrom list and list elements Use display: inlineinstead for list elements Set links to display: inline-blockso they keep their block dimensions Simply add text-align: centerto the navbar to center everything

How to create an unordered list using CSS?

An unordered list starts with the <ul> tag. Each list item starts with the <li> tag. The CSS list-style-type property is used to define the style of the list item marker. It can have one of the following values: Note: A list item ( <li>) can contain a new list, and other HTML elements, like images and links, etc.

How to collapse navbar collapse in Bootstrap?

We have defined the class navbar-centre. By using Bootstrap: This method is a quick tricky that can save you from writing extra CSS. In this, we simply add another div tag above the div tag having class collapse navbar-collapse. This new div tag will also have the same collapse navbar-collapse class.


2 Answers

Steps I took:

  • Remove float: left from list and list elements
  • Use display: inline instead for list elements
  • Set links to display: inline-block so they keep their block dimensions
  • Simply add text-align: center to the navbar to center everything
  • Wrap in media query so vertical list on mobile is unaffected

Resulting CSS adding a .navbar-centered style:

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

Use by applying .navbar-centered style to navbar:

<div class="navbar navbar-default navbar-centered" role="navigation">
    ...
</div>
like image 69
deizel Avatar answered Oct 01 '22 17:10

deizel


I was able to figure this out on my own. Not sure if it was the best solution:

        <div class="navbar navbar-fixed-top">                
                    <div style="border:1px solid black" class="container">
            <div class="inner-nav">
                <!--<a href="#" class="navbar-brand">Title</a> -->
                <ul class="nav navbar-nav">
                    <li class="active"><a href="/" >Home</a></li>
                    <li><a href="#" >About</a></li>
                    <li><a href="#" >Projects</a></li>
                    <li><a href="#" >contact</a></li>
                </ul>
            </div>
                    </div>                
            </div>   

I then added the following styles to bootstrap.css:

/* ADDED for centering navbar items */
.navbar .inner-nav ul {
position:relative;left:50%;float:left;margin-right:0;margin-left:0;
}

/* ADDED for centering navbar items */
.navbar .inner-nav li {
position:relative;right:50%;float:left;margin:0;list-style:none
}
like image 42
prufrock Avatar answered Oct 01 '22 18:10

prufrock