I am trying to implement a Bootstrap 3 navbar so that the brand logo to always remain in the middle. This is the code:
<div class="navbar navbar-fixed-top navbar-default">_x000D_
<div class="navbar-inner">_x000D_
<div class="container">_x000D_
<button type="button" style="float: left;" class="pull-left btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">_x000D_
<span class="icon-bar"></span>_x000D_
<span class="icon-bar"></span>_x000D_
<span class="icon-bar"></span>_x000D_
</button>_x000D_
<a class="brand" style="margin: 0; float: none;" href="#">_x000D_
<img src="/Content/themes/next/images/logo.png" /></a>_x000D_
<div class="nav-collapse">_x000D_
<ul class="nav">_x000D_
<li> <a href="#">Item 1</a></li>_x000D_
<li> <a href="#">Item 1</a></li>_x000D_
<li> <a href="#">Item 1</a></li>_x000D_
</ul>_x000D_
</div>_x000D_
<ul class="nav pull-right">_x000D_
<li>_x000D_
<a href="#">_x000D_
<div class="nextCog"></div>_x000D_
</a>_x000D_
</li>_x000D_
</ul>_x000D_
<span class="navbar-text pull-right">superpup1 </span>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
It makes a nice looking navbar:
However, I would like the logo (green) remain in the middle persistently. I am adding this style to the tag with "brand" class:
<a class="brand" style="margin: 0; float: none; text-align:center" href="#">
<img src="/Content/themes/next/images/logo.png" />
</a>
It partially solves the problem: the logo is in the middle but it pushes the rest of the navbar elements down:
This is an undesirable effect that I would like to eliminate. Could you suggest a solution? Maybe it's a wrong approach to centering a logo from the start ?
This question is related to
css
twitter-bootstrap-3
bootstrap-4
Old question, but just for posterity.
I've found the easiest way to do it is to have the image as the background image of the navbar-brand. Just makes sure to put in a custom width.
.navbar-brand
{
margin-left: auto;
margin-right: auto;
width: 150px;
background-image: url('logo.png');
}
Updated 2018
See if this example helps: http://bootply.com/mQh8DyRfWY
The brand is centered using..
.navbar-brand
{
position: absolute;
width: 100%;
left: 0;
top: 0;
text-align: center;
margin: auto;
}
Your markup is for Bootstrap 2, not 3. There is no longer a navbar-inner
.
EDIT - Another approach is using transform: translateX(-50%);
.navbar-brand {
transform: translateX(-50%);
left: 50%;
position: absolute;
}
http://www.bootply.com/V7vKDfk46G
In Bootstrap 4, mx-auto
or flexbox can be used to center the brand and other elements. See How to position navbar contents in Bootstrap 4 for an explanation.
Also see:
<style>
.navbar-brand {
margin: auto;
}
</style>
<!--HTML-->
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">
<img src="logo goes here" width="100" height="100" class="logo" alt=""
loading="lazy">
</a>
</nav>
A solution where the logo is truly centered and the links are justified.
The max recommended number of links for the nav is 6, depending on the length of the words in eache link.
If you have 5 links, insert an empty link and style it with:
class="hidden-xs" style="visibility: hidden;"
in this way the number of links is always even.
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
<style>_x000D_
.navbar-nav > li {_x000D_
float: none;_x000D_
vertical-align: bottom;_x000D_
}_x000D_
#site-logo {_x000D_
position: relative;_x000D_
vertical-align: bottom;_x000D_
bottom: -35px;_x000D_
}_x000D_
#site-logo a {_x000D_
margin-top: -53px;_x000D_
}_x000D_
</style>_x000D_
<nav class="navbar navbar-default navbar-fixed-top">_x000D_
<div class="container">_x000D_
<div class="navbar-header">_x000D_
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">_x000D_
<span class="sr-only">Nav</span>_x000D_
<span class="icon-bar"></span>_x000D_
<span class="icon-bar"></span>_x000D_
<span class="icon-bar"></span>_x000D_
</button>_x000D_
</div>_x000D_
<div id="navbar" class="collapse navbar-collapse">_x000D_
<ul class="nav nav-justified navbar-nav center-block">_x000D_
<li class="active"><a href="#">First Link</a></li>_x000D_
<li><a href="#">Second Link</a></li>_x000D_
<li><a href="#">Third Link</a></li>_x000D_
<li id="site-logo" class="hidden-xs"><a href="#"><img id="logo-navbar-middle" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/32877/logo-thing.png" width="200" alt="Logo Thing main logo"></a></li>_x000D_
<li><a href="#">Fourth Link</a></li>_x000D_
<li><a href="#">Fifth Link</a></li>_x000D_
<li class="hidden-xs" style="visibility: hidden;"><a href="#">Sixth Link</a></li>_x000D_
</ul>_x000D_
</div>_x000D_
</div>_x000D_
</nav>_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>_x000D_
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
_x000D_
To see result click on run snippet and then full page
The simplest way is css transform:
.navbar-brand {
transform: translateX(-50%);
left: 50%;
position: absolute;
}
DEMO: http://codepen.io/candid/pen/dGPZvR
This way also works with dynamically sized background images for the logo and allows us to utilize the text-hide class:
CSS:
.navbar-brand {
background: url(http://disputebills.com/site/uploads/2015/10/dispute.png) center / contain no-repeat;
transform: translateX(-50%);
left: 50%;
position: absolute;
width: 200px; /* no height needed ... image will resize automagically */
}
HTML:
<a class="navbar-brand text-hide" href="http://disputebills.com">Brand Text
</a>
We can also use flexbox though. However, using this method we'd have to move navbar-brand
outside of navbar-header
. This way is great though because we can now have image and text side by side:
.brand-centered {
display: flex;
justify-content: center;
position: absolute;
width: 100%;
left: 0;
top: 0;
}
.navbar-brand {
display: flex;
align-items: center;
}
Demo: http://codepen.io/candid/pen/yeLZax
To only achieve these results on mobile simply wrap the above css inside a media query:
@media (max-width: 768px) {
}
Source: Stackoverflow.com