I am working on a web page that uses Bootstrap. I am trying to add a fixed navbar. This navbar will have three sections. I want the left content to be left-aligned, the center content to be center-aligned and the right content to be aligned to the right. Basically, I need it to looks like this:
+-----------------------+-----------------------+-----------------------+
| Hello | item 1 | item 2 | contact |
+-----------------------+-----------------------+-----------------------+
In an attempt to do this, I've written the following code:
<nav class="navbar navbar-static-top" style="background-color:#000;">
<div class="navbar-brand pull-left">Hello</div>
<ul style="list-inline">
<li>item 1</li>
<li>|</li>
<li>item 2</li>
</ul>
<div class="pull-right">
contact
</div>
</nav>
The "Hello" is properly aligned. The "contact" is properly aligned. However, I can't seem to figure out how to center the ul within the navbar. Can somebody please share their CSS mojo?
Thanks!
You can do it using col- and text-align like this:
* {
color: #fff;
}
.list-inline {
display: inline-block;
}
.left-content{
text-align: left;
}
.center-content{
text-align: center;
padding: 15px;
}
.right-content{
text-align: right;
padding: 15px;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-static-top" style="background-color:#000;">
<div class="navbar-brand left-content col-xs-2">Hello</div>
<div class="center-content col-xs-8">
<ul class="list-inline">
<li>item 1</li>
<li>|</li>
<li>item 2</li>
</ul>
</div>
<div class="right-content col-xs-2">contact</div>
</nav>
You can change the cols according to what you need, check out this fore more info http://www.w3schools.com/bootstrap/bootstrap_grid_system.asp
Related
On wider screens my login button is visible, but on smaller screens it is hidden.
JSBIN http://jsbin.com/nowubuseqeza/1/edit?html,js,output
Relevant code:
<div class="top-bar" data-topbar="" style="background:#074e68">
<ul class="title-area">
<li class="name">
<h1><img class="left" src="images/pluslogo.png" style="margin:2px 1em;height:40px;width:40px" alt="Pacific Medical Training"> Pacific Medical Training</h1>
</li>
</ul>
<div class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
<li class="active">
Login
</li>
</ul>
</div>
</div>
Is it possible to have this visible in all circumstances? Otherwise my users cannot login.
Change your code to this DEMO
<div class="top-bar" data-topbar="" style="background:#074e68">
<div class="top-bar-section right">
<!-- Right Nav Section -->
<ul class="right show-for-small">
<li class="active">
Login
</li>
</ul>
</div>
<ul class="title-area">
<li class="name">
<h1><img class="left" src="images/pluslogo.png" style="margin:2px 1em;height:40px;width:40px" alt="Pacific Medical Training"> Pacific Medical Training</h1>
</li>
</ul>
<div class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
<li class="active">
Login
</li>
</ul>
</div>
</div>
In the smaller screen, a number of css properties are changing for the title-area and the links in the top-bar. If you add the following, it should bring the login button back on small screens:
//Only use it for screens under 40.063em, using the media query
#media only screen and (max-width: 40.063em) {
.top-bar .name h1 a {
width: auto;
}
.top-bar-section ul {
width: auto;
}
.top-bar-section ul li.active > a {
padding: 0px 15px;
line-height: 45px;
}
.top-bar .title-area {
float: left;
}
}
However, it may need a bit more refinement for very small screens where the "Pacific Medical Training" takes up the whole top-bar and pushes the login button down
I'm trying to move my navbar to the right, tried both navbar-right, and pull-right, but all I get is something like this -
<div class="container" >
<h1 align="center">My Site</h1>
<div class="container" >
<ul class="nav nav-tabs navbar-right" >
<li class="active">tab1</li>
<li>tab2</li>
<li>tab3</li>
<li>tab4</li>
</ul>
</div>
<br>
Hello
</div>
So there are 2 problems here -
The grey line underneath the tabs is much shorter, and the tabs themselves aren't aligned properly - I want "tab1" to be the rightmost one, and correspondingly "tab4" the leftmost one.
I get the same results for navbar-right and pull-right. How can this be fixed?
Thanks!
you can easily have that behavior by just extending the Bootstrap.
Demo on JSBin: http://jsbin.com/zaniz/1/edit?html,css,output
all I did was added a right-to-left class to the <ul> and the CSS Style should be:
.right-to-left li { float: right; }
I've added the following rules to solve the problem without losing the border on the ul or reordering the markup:
.nav-tabs-right { text-align: right; }
.nav-tabs-right > li { display: inline-block; float: none; }
you can apply that to .nav-tabs directly but I prefer to create a separate class to replace .navbar-right
You can try this; if you want that gray line to cover the entire page horizontly
<div class="container" >
<h1 align="center">My Site</h1>
<div class="container" >
<ul class="nav nav-tabs" >
<li class="active pull-right" >tab1</li>
<li class="pull-right">tab2</li>
<li class="pull-right">tab3</li>
<li class="pull-right">tab4</li>
</ul>
</div>
<br>
Hello
</div>
I just added a float: right; (equal to pull-right) style to the <ul> and it seemed to produce the desired output, as for the tab1 being the rightmost one, it's probably best to reorder your markup to say:
<ul class="nav nav-tabs navbar-right" >
<li class="active">tab4</li>
<li>tab3</li>
<li>tab2</li>
<li>tab1</li>
</ul>
Using Bootstrap 5 you can simple add justify-content-end to the <ul> class
<div class="container" >
<h1 align="center">My Site</h1>
<div class="container" >
<ul class="nav nav-tabs justify-content-end" >
<li class="active">tab1</li>
<li>tab2</li>
<li>tab3</li>
<li>tab4</li>
</ul>
</div>
<br>
Hello
I'm having trouble centering my content in the bootstrap navbar. I'm using bootstrap 3. I've read many posts, but the CSS or methods used will not work with my code! I'm really frustrated, so this is like my last option. Any help would be appreciated!
<!DOCTYPE html>
<html>
<head>
<title>Navigation</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li class="dropdown">
Dropdown <b class="caret"></b>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li>Separated link</li>
<li>One more separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
<h1>Home</h1>
</div>
<!--JAVASCRIPT-->
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
https://jsfiddle.net/amk07fb3/
I think this is what you are looking for. You need to remove the float: left from the inner nav to center it and make it a inline-block.
.navbar .navbar-nav {
display: inline-block;
float: none;
vertical-align: top;
}
.navbar .navbar-collapse {
text-align: center;
}
http://jsfiddle.net/bdd9U/2/
Edit: if you only want this effect to happen when the nav isn't collapsed surround it in the appropriate media query.
#media (min-width: 768px) {
.navbar .navbar-nav {
display: inline-block;
float: none;
vertical-align: top;
}
.navbar .navbar-collapse {
text-align: center;
}
}
http://jsfiddle.net/bdd9U/3/
The original post was asking how to center the collapsed navbar. To center elements on the normal navbar, try this:
.navbar-nav {
float:none;
margin:0 auto;
display: block;
text-align: center;
}
.navbar-nav > li {
display: inline-block;
float:none;
}
There's another way to do this for layouts that doesn't have to put the navbar inside the container, and which doesn't require any CSS or Bootstrap overrides.
Simply place a div with the Bootstrap container class around the navbar. This will center the links inside the navbar:
<nav class="navbar navbar-default">
<!-- here's where you put the container tag -->
<div class="container">
<div class="navbar-header">
<!-- header and collapsed icon here -->
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<!-- links here -->
</ul>
</div>
</div> <!-- close the container tag -->
</nav> <!-- close the nav tag -->
If you want the then align body content to the center navbar, you also put that body content in the Bootstrap container tag.
<div class="container">
<! -- body content here -->
</div>
Not everyone can use this type of layout (some people need to nest the navbar itself inside the container). Nonetheless, if you can do it, it's an easy way to get your navbar links and body centered.
You can see the results in this fullpage JSFiddle:
http://jsfiddle.net/bdd9U/231/embedded/result/
Source:
http://jsfiddle.net/bdd9U/229/
For Bootstrap 4:
Just use
<ul class="navbar-nav mx-auto">
mx-auto will do the job
Bootstrap 5 (Update 2021)
The Navbar is still flexbox based in Bootstrap 5 and centering content works the same as it did with Bootstrap 4. Examples
Bootstrap 4
Centering Navbar content is easier is Bootstrap 4, and you can see many centering scenarios explained here: https://stackoverflow.com/a/20362024/171456
Bootstrap 3
Another scenario that doesn't seem to have been answered yet is centering both the brand and navbar links. Here's a solution..
.navbar .navbar-header,
.navbar-collapse {
float:none;
display:inline-block;
vertical-align: top;
}
#media (max-width: 768px) {
.navbar-collapse {
display: block;
}
}
http://codeply.com/go/1lrdvNH9GI
Also see:
Bootstrap NavBar with left, center or right aligned items
This code worked for me
.navbar .navbar-nav {
display: inline-block;
float: none;
}
.navbar .navbar-collapse {
text-align: center;
}
from the bootstrap official site (and, almost, like Eric S. Bullington said.
https://getbootstrap.com/components/#navbar-default
the example navbar looks like:
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
...etc
to center the nav, that what i've done:
<div class="container-fluid" id="nav_center">
css:
#media (min-width:768px) { /* don't break navbar on small screen */
#nav_center {
width: 700px /* need to found your width according to your entry*/
}
}
work with class="container" and navbar-right or left, and of course you can replace id by class. :D
Seems like all these answers involve custom css on top of bootstrap. The answer I am providing utilizes only bootstrap so I hope it comes to use for those that want to limit customizations.
This was tested with Bootstrap V3.3.7
<footer class="navbar-default navbar-fixed-bottom">
<div class="container-fluid">
<div class="row">
<div class="col-xs-offset-5 col-xs-2 text-center">
<p>I am centered text<p>
</div>
<div class="col-xs-5"></div>
</div>
</div>
</footer>
this should work
.navbar-nav {
position: absolute;
left: 50%;
transform: translatex(-50%);
}
Use following html
<link rel="stylesheet" href="app/components/directives/navBar/navBar.scss"/>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Collect the nav links, forms, and other content for toggling -->
<ul class="nav navbar-nav">
<li><h5>Home</h5></li>
<li>|</li>
<li><h5>About</h5></li>
<li>|</li>
<li><h5>Contacts</h5></li>
<li>|</li>
<li><h5>Cart</h5></li>
</ul>
</div><!-- /.container-fluid -->
</nav>
And css
.navbar-nav {
width: 100%;
text-align: center;
}
.navbar-nav > li {
float: none;
display: inline-block;
}
.navbar-default {
background-color: white;
border-color: white;
}
.navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:hover, .navbar-default .navbar-nav>.active>a:focus{
background-color:white;
}
Modify according to your needs
V4 of BootStrap is adding Center (justify-content-center) and Right Alignment (justify-content-end) as per: https://v4-alpha.getbootstrap.com/components/navs/#horizontal-alignment
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
it because it apply flex. you can use this code
#media (min-width: 992px){
.navbar-expand-lg .navbar-collapse {
display: -ms-flexbox!important;
display: flex!important;
-ms-flex-preferred-size: auto;
flex-basis: auto;
justify-content: center;
}
in my case it workded correctly.
I've tried a few option but i can't manage to get 4 links to span across the navbar. I thought it would be quite easy to add the span3 class to each <li>.
Here's my HTML:
<div class="navbar center">
<div class="navbar-inner">
<div class="container row">
<ul class="nav span12">
<li class="active span3">
Home
</li>
<li class="span3">Link</li>
<li class="span3">Link</li>
<li class="span3">Link</li>
</ul>
</div>
</div>
</div>
To center the links I've used the solution described here: Modify twitter bootstrap navbar
Here's the CSS:
.navbar-inner {
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
}
.center.navbar .nav,
.center.navbar .nav > li {
float:none;
display:inline-block;
*display:inline; /* ie7 fix */
*zoom:1; /* hasLayout ie7 trigger */
vertical-align: top;
}
.center .navbar-inner {
text-align:center;
}
All I've managed to get is this:
How can I get those four links spanned on the same row?
Using spanX is not the best solution here (unless you're using bootstrap-resonsive.css, see below). You can uses percentages, as long as you're willing to modify you CSS when the number of items in the navbar changes.
You can make this work with default navbar markup:
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<li>Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
</div>
</div>
And two CSS rules. The first removes the margin on the <ul class="nav">, removes the float and sets its width to be 100% of its container (in this case, the <div class="container"> within <div class="navbar-inner">.
The second rule sets the width of each <li> to be a certain percentage of the width of the <ul>. If you have four items, then set it to 25%. If you have five, it'd be 20%, and so on.
.navbar-inner ul.nav {
margin-right: 0;
float: none;
width: 100%;
}
.navbar-inner ul.nav li {
width: 33%;
text-align: center;
}
jsFiddle DEMO
UPDATE
If you are using the responsive bootstrap CSS, you CAN use the built-in spanX classes, like so:
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul class="nav row-fluid">
<li class="span4">Home</li>
<li class="span4">Link</li>
<li class="span4">Link</li>
</ul>
</div>
</div>
</div>
Then, all the CSS you need is:
.navbar-inner ul.nav li {
text-align: center;
}
jsFiddle DEMO
You did a good start but your markup doesn't reflect the real grid :
You don't put .spanX in a .span12
.container and .row might have conflicting properties
It seems to work with this :
<div class="navbar center">
<div class="navbar-inner">
<div class="container">
<div class="row">
<ul class="nav">
<li class="active span3">
Home
</li>
<li class="span3">Link</li>
<li class="span3">Link</li>
<li class="span3">Link</li>
</ul>
</div>
</div>
</div>
</div>
Demo (jsfiddle) and fullscreen
I'm using 960.gs (http://960.gs) as a CSS framework for my site and I'm trying to colour in the background of the .container_12 classes rather than my .grid_12 classes for the extra 10px I get on each side, giving the grid a bit of whitespace to breathe.
I looked at the 960.gs example site and he has a background repeating on the y-axis and it seems to expand no matter how large or what is inside the .grid_12 or .grid_16 classes.
When I want to paint the background white (instead of have an image repeat on the y-axis), it appears as though my .grid_12 is floating and therefore .container_12 has no height and thus, no white background.
Here is my relevant HTML
<body>
<div id="logoContainer" class="container_12">
<div id="logo" class="grid_12">
<h1>Logo</h1>
</div> <!-- end #logo -->
</div> <!-- end #logoContainer -->
<div class="clear"></div>
<div id="menuContainer" class="container_12">
<div id="menu" class="grid_12">
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</div> <!-- end #menu -->
</div> <!-- end #menuContainer -->
<div class="clear"></div>
<div id="contentContainer" class="container_12">
<div id="content" class="grid_12">
<p>Content here</p>
</div> <!-- end #content -->
</div> <!-- end #contentContainer -->
<div class="clear"></div>
</body>
and CSS
/* 960.gs */
#import url('reset.css');
#import url('960.css');
#import url('text.css');
#contentContainer
{
background-color: #fff;
margin-bottom: 10px;
border-radius: 20px;
-o-border-radius: 20px;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
}
To answer my own question, the <div class="clear"></div> need to be after each .grid_, not each .container_