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>
- 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.
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
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.
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.
Steps I took:
float: left
from list and list elementsdisplay: inline
instead for list elementsdisplay: inline-block
so they keep their block dimensionstext-align: center
to the navbar to center everythingResulting 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>
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With