Reputation: 1902
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">
<div class="navbar-inner">
<div class="container">
<button type="button" style="float: left;" class="pull-left btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" style="margin: 0; float: none;" href="#">
<img src="/Content/themes/next/images/logo.png" /></a>
<div class="nav-collapse">
<ul class="nav">
<li> <a href="#">Item 1</a></li>
<li> <a href="#">Item 1</a></li>
<li> <a href="#">Item 1</a></li>
</ul>
</div>
<ul class="nav pull-right">
<li>
<a href="#">
<div class="nextCog"></div>
</a>
</li>
</ul>
<span class="navbar-text pull-right">superpup1 </span>
</div>
</div>
</div>
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 ?
Upvotes: 63
Views: 224433
Reputation: 362760
Bootstrap 5 (update 2022)
In Bootstrap 5, mx-auto
or flexbox justify-content-center
can still be used to center the brand and other elements.
https://codeply.com/p/UKfeHAJQQC
Bootstrap 4 (update 2018)
In Bootstrap 4, mx-auto
or flexbox can be used to center the brand and other elements. See How to center position navbar content in Bootstrap for an explanation.
Bootstrap 3 (original answer)
See if this example helps: https://codeply.com/p/G71Uruhqwy
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;
}
https://codeply.com/p/3kkmEHtVGH
Also see:
Bootstrap NavBar with left, center or right aligned items
Upvotes: 30
Reputation: 29
As it's a flexbox (and probably due to a justify-content:space-between
BT4 css prop.), if you add a third element, sibling of the name and the button, it wil naturaly centering the second element in the code.
I just add <span>login</span>
just after the a
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-sm navbar-light bg-light mt-5">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<span>login</span>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
Upvotes: 0
Reputation: 19
<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>
Upvotes: 0
Reputation: 786
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"/>
<style>
.navbar-nav > li {
float: none;
vertical-align: bottom;
}
#site-logo {
position: relative;
vertical-align: bottom;
bottom: -35px;
}
#site-logo a {
margin-top: -53px;
}
</style>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<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">Nav</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav nav-justified navbar-nav center-block">
<li class="active"><a href="#">First Link</a></li>
<li><a href="#">Second Link</a></li>
<li><a href="#">Third Link</a></li>
<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>
<li><a href="#">Fourth Link</a></li>
<li><a href="#">Fifth Link</a></li>
<li class="hidden-xs" style="visibility: hidden;"><a href="#">Sixth Link</a></li>
</ul>
</div>
</div>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
To see result click on run snippet and then full page
Upvotes: 0
Reputation: 3642
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) {
}
Upvotes: 62
Reputation: 227
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');
}
Upvotes: 5
Reputation: 9723
Try this:
.navbar {
position: relative;
}
.brand {
position: absolute;
left: 50%;
margin-left: -50px !important; /* 50% of your logo width */
display: block;
}
Centering your logo by 50% and minus half of your logo width so that it won't have problem when zooming in and out.
See fiddle
Upvotes: 85