This question already has answers here:
Bootstrap Center Navbar Items
(5 answers)
Closed 5 years ago.
I've been trying to centre the links/tabs on my Bootstrap navbar for over a week now and it's doing my head in. Have gone through every similar question on here and absolutely nothing is working. I'm thinking I've doubled up rules somewhere along the line or am not actually targeting the right thing.
It's the basic Bootstrap navbar snippet on Dreamweaver. I've managed to customise colour/font/size, but just can't get the darn thing to sit in the middle.
.navbar.navbar-default {
background-color: #B3C5D7;
background-attachment: fixed;
position: fixed;
width: 100%;
border-radius: 0px;
float: none;
text-align: center;
}
.nav.navbar-nav {
font-size: x-large;
width: 100%;
display: inline-block;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1" aria-expanded="false"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="active">HOME<span class="sr-only">(current)</span></li>
<li>OUR CARE FACTOR</li>
<li>FACILITIES</li>
<li>FUNCTIONS</li>
<li>CONTACT US</li>
</ul>
</div>
</div>
</div>
</nav>
You can do it with the Flexbox:
.navbar {
display: flex; /* displays flex-items (children) inline */
justify-content: center; /* centers them horizontally */
}
Try adding these classes
CSS
.navbar-header
{
float:none;
}
.navbar-nav li
{
float:none;
display:inline-block;
}
hope this helps..
Basically bootstrap using float:left in navbar-header and li so parent div doesn't accept the center alignment or margin auto so update css part which included in following snippet or check fiddle link
In first option you will get nesting classes to look css and html both carefully.
1st option with nesting class to get center
In second option you will get only css changes so check css carefully.
Second option to get center
2nd option to get center
Update css part, I am using inline-flex to get center
.navbar.navbar-default {
background-color: #B3C5D7;
background-attachment: fixed;
position: fixed;
width: 100%;
border-radius: 0px;
float: none;
text-align: center;
}
.nav.navbar-nav {
font-size: x-large;
width: 100%;
display: inline-block;
}
/* Add this css part */
.navbar-header {
float: none; /* Add this */
}
.navbar-nav li {
display: inline-flex; /* Add this */
float: none; /* Add this */
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1" aria-expanded="false"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="active">HOME<span class="sr-only">(current)</span></li>
<li>OUR CARE FACTOR</li>
<li>FACILITIES</li>
<li>FUNCTIONS</li>
<li>CONTACT US</li>
</ul>
</div>
</div>
</div>
</nav>
.navbar.navbar-default {
background-color: #B3C5D7;
background-attachment: fixed;
position: fixed;
width: 100%;
border-radius: 0px;
float: none;
text-align: center;
}
.nav.navbar-nav {
font-size: x-large;
width: 100%;
display: inline-block;
}
/* Add this css part */
.navbar-header.center-items {
float: none; /* Add this */
}
.navbar-center.navbar-nav li {
display: inline-flex; /* Add this */
float: none; /* Add this */
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header center-items"> <!--Add class -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1" aria-expanded="false"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav navbar-center"> <!--Add class -->
<li class="active">HOME<span class="sr-only">(current)</span></li>
<li>OUR CARE FACTOR</li>
<li>FACILITIES</li>
<li>FUNCTIONS</li>
<li>CONTACT US</li>
</ul>
</div>
</div>
</div>
</nav>
Related
Currently I have to implement a complex navigation styling.
I succeeded to simplify the problem to show only the part of the html.
The problem is that I cannot touch the HTML code.
Here is the code (a simple bootstrap navbar):
https://jsfiddle.net/5y4asdef/
I want to fill the space in left of "Application name" for example with white color is such way that to work on all resolutions especially (wider)
I've tried something like this:
.navbar-header:after {
content: " ";
background-color: white;
width: 110px;
height: 80px;
display: block;
position: absolute;
left: 0px;
top: 0;
}
I've tried with combining some background properties, but couldn't figure out how to apply it.
I want to ensure that no matter of how long is the screen it will work:
https://jsfiddle.net/5y4asdef/show (fullscreen)
Use .container-fluid class instead of .container
Here's the updated snippet:
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Application name</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
</ul>
</div>
</div>
</div>
Here is my non-quality solution for now:
.navbar-header {
position: relative;
}
.navbar-header:after {
content: " ";
background-color: white;
width: 1000px;
height: 80px;
display: block;
position: absolute;
left: -1000px;
top: -6px;
}
I'm using BS3 and a bunch of custom styling. I know the line of code that's causing the issue, but I can't think of a way to make the navbar not so tall without the height or max-height attribute. Click on the JSFiddle and you'll see that there is no color background when you look at the menu when collapsed (mobile). Simply remove the max-height line and it works, but the navbar is too tall for what I want.
There's a ton of HTML & CSS, so beware the following paste. Note that I'm linking to a custom bootstrap.css as well...
<div class="navbar-wrapper">
<div class="container-fluid">
<nav class="navbar navbar-inverse navbar-static-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">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button> <a class="navbar-brand" href="#"><img id="logo" src="http://www.thinkliz.com/dev/midtownaustin/img/midtown-church-austin-logo-white-vector.svg" class="img-responsive"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
</div>
</div>
CSS:
/* CUSTOMIZE THE NAVBAR
-------------------------------------------------- */
.navbar-wrapper {
position: absolute;
top: 20px;
right: 0;
left: 0;
z-index: 20;
}
.logo {
margin-top: -42px;
}
.navbar-brand img {
height: 290px;
max-height: 130px;
margin-top: -55px;
}
/* RESPONSIVE CSS
-------------------------------------------------- */
#media (max-width: 768px) {
.navbar-brand img {
max-height: 70px;
margin-top: -60px;
}
/* THIS IS WHAT IS BREAKING THE NAV */
.navbar {
min-height: 80px;
max-height: 80px;
}
.navbar-toggle {
margin-top: 23px;
}
}
So are you trying to get the background of the mobile collapsed menu a different color so you can read the text? You could add a custom CSS rule such as:
.navbar-collapse ul {
background-color: #ff0000;
}
I doubt you want red at the background, but it's just an example.
Most often it breaks the layout if you use position absolute without knowing what you really do. Especially when you work with a CSS framework. You have to fix too much and there seems to be no end.
Customize as much as possible with bootstrap and as less as possible by your own. Only that which you can't do with bootstrap.
To make the navbar work with a bigger (custom) logo follow this instruction:
Instruction bootstrap navbar with bigger logo
with your logo and the size of it.
After you have done this, set the background colors for the
.navbar-header and .navbar-collapse. You have a perfect working navbar!
How I can set logo like this one? (second one)
I already try but not working. My code
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand navbar-brand" href="#"><img src="img/logo.png" alt=""></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
Buy
</li>
<li>
Rent
</li>
<li>
New Properties
</li>
<li>
Property Alert
</li>
<li>
E-Learning
</li>
<li>
About Us
</li>
<li>
Contact Us
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
DEMO: https://jsbin.com/holumi/1/
https://jsbin.com/holumi/1/edit?html,css,output
The size of your logo matters, adjust the MIN-WIDTH media queries to move the logo into place.
Add navbar-custom on the navbar.
Remove the brand and navbar-brand and add logo
Adjust other CSS as needed. The CSS BEFORE the min-width media query is for all viewport sizes, the stuff INSIDE the min-width media query is for that min-width and up.
The use of position:absolute puts the logo, at that min-width, out of the document flow, therefore padding is used to put the navbar-nav into position. Notice the values.
The small viewport CSS in this example uses the same logo for both large and small, if the logo goes on dark, you can use the responsive utility classes to hide one and show the other at the min-width or max-width, such as visible-xs on the logo that is to go on the dark background and hidden-xs on the the logo for all other sizes.
CSS
.navbar-custom {
background: navy
}
.navbar-custom .logo img {
padding: 15px;
max-width: 100%;
height: auto;
}
.navbar-custom .logo {
float: left;
padding-right: 40px;
width: 100%;
}
.navbar-custom .navbar-toggle {
position: absolute;
right: 15px;
top: 15px;
border: 0px;
width: 50px;
margin: 0;
}
.navbar-custom .icon-bar {
width: 100%;
margin: 5px 0;
height: 3px;
}
#media (min-width:768px) {
.navbar-custom.navbar {
height: 200px
}
.navbar-custom .container {
position: relative;
overflow: visible;
}
.navbar-custom .navbar-nav {
padding: 125px 0 0;
text-align: center;
width: 100%;
}
.navbar-custom .navbar-nav > li {
display: inline-block;
float: none;
}
.navbar-custom .logo {
position: absolute;
background: #fff;
left: 0;
right: 0;
z-index: 5000;
display: block;
float: none;
padding: 0;
}
.navbar-custom .logo:before,
.navbar-custom .logo:after {
position: absolute;
background: #fff;
content: '';
left: -2000px;
width: 2000px;
bottom: 0;
display: block;
height: 100%;
}
.navbar-custom .logo:after {
left: auto;
right: -2000px;
}
}
HTML
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top navbar-custom" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="logo" href="#"><img src="http://placehold.it/100x75/000/FFFFFF&text=LOGO" alt=""></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
Buy
</li>
<li>
Rent
</li>
<li>
New Properties
</li>
<li>
Property Alert
</li>
<li>
E-Learning
</li>
<li>
About Us
</li>
<li>
Contact Us
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
You could just remove the brand completely from the navbar and then add a div above that navbar with an img attribute which would be your logo
Create two navbars and make top position of second navbar to start below first nav bar.
<nav class="navbar navbar-inverse navbar-fixed-top my-logo" role="navigation">
.....
</nav>
<nav class="navbar navbar-inverse navbar-fixed-top my-menu" role="navigation">
Now create style for second navbar as
.my-menu {
top: 50px;
}
When my web page is shown on small devices the menu collapses as expected but when you click on the collapsed menu icon the menu does not appear. There is a slight adjustment of the vertical height of the page below the menu and the last menu item half appears along the top of the page but the rest of the menu is nowhere to be seen.
My Bootstrap code looks like this:
#myNavbar {
position: relative;
border:none;
}
#myNavbar .nav {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: -10px;
}
.navbar-nav.navbar-right:last-child {
margin-right: 0;
}
.navbar-default {
background:#FFFFFF;
border-left:none;
border-right:none;
border-top:none;
border-bottom:solid;
border-bottom-width:2px;
border-color:#01B6AC;
width:100% !important;
margin-right:0px;
padding-right:0px;
border-radius: 0 !important;
-moz-border-radius: 0 !important;
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img class="img-responsive" src="/assets/img/outa.png" width="120px;"/></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav pull-right">
<li>Saved searches</li>
<li>Settings</li>
<li>About</li>
<li>Sign In</li>
</ul>
</div>
</div>
</nav>
Anyone know what I am doing wrong?
You problem is here
#myNavbar .nav {
position: absolute;
}
When you use position:absolute; the element will be positioned absolutely from the first positioned parent div, if it can't find one it will position absolutely from the window. You can refer to THIS question for further info. So what you can do is add #media query and set a size over certain pixels where the position will be absolute. So something like
#media(min-width: 400px) {
#myNavbar .nav {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: -10px;
}
}
just give proper reference of bootstrap.css and bootstrap.js and your code will work fine.
I need to modify the dropdown-menu provided in Bootstrap, using the navbar-fixed-top, to appear on hover not vertically but horizontally li { display: inline-block } (that's easy), but so I need the actual ul.dropdown-menu to stretch the full width of the page. I can't seem to figure it out.
Don't give me a megamenu plugin or anything, please, just how can I fix it to stretch the width of the whole page? (not the page container either, the window)
Will probably need to wrap another element too, actually, as the ul needs to be centered.
So does anyone know how to do this?
EDIT: Figured it out (like 5 minutes after posting this) and with no added elements:
.nav { margin-bottom: 0; }
.dropdown { position: static; }
.dropdown-menu { width: 100%; text-align: center; }
.dropdown-menu>li { display: inline-block; }
and there you have it!
First you shouldn't wrap the navbar component in a div.container then update the css with the following Code:
.nav > li.dropdown.open {
position: static;
}
.nav > li.dropdown.open .dropdown-menu {
display:table; width: 100%; text-align: center; left:0; right:0;
}
.dropdown-menu>li {
display: table-cell;
}
check the demo here http://www.bootply.com/8EgGsi4F7w
<li class="dropdown open" style="position: initial;">
<ul class="dropdown-menu" style="width: 100%;">
just use the position: initial in the main li(dropdown) of your nav and in the child ul dropdown-menu set width 100%
Here the alternative that worked for me.
I just tried option 2. The key is:
Adding class position-static along with (the) dropdown class, which is the parent class of dropdown-menu as follows:
<li class="nav-item dropdown position-static">
Credit to its author: VigneshKannan3 from GeeksforGeeks
It looks like this answer to a similar question has the same approach.
Check this
.nav > li.dropdown.open {
position: static;
}
.nav > li.dropdown.open .dropdown-menu {
display: table;
border-radius: 0px;
width: 100%;
text-align: center;
left: 0;
right: 0;
}
.dropdown-menu > li {
display: table-cell;
height: 50px;
line-height: 50px;
vertical-align: middle;
}
#media screen and (max-width: 767px) {
.dropdown-menu > li {
display: block;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<div class="navbar-brand">Blackcat</div>
<button class="navbar-toggle" data-toggle="collapse" data-target=".btnCollapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse btnCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">Shop</li>
<li class="dropdown">
Artist <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Rich</li>
<li>Shay</li>
<li>Jose</li>
<li>Marie</li>
<li>Simon</li>
<li>Jamie</li>
<li>Andrew</li>
<li>Teddie</li>
</ul>
</li>
<li>FAQs</li>
<li>Contact</li>
</ul>
</div>
</div>
<!-- end container -->
</div>
<!-- end navbar navbar-inverse navbar-static-top -->
<!-- container -->
<div class="container">
<div class="row">
<p>Site content here...</p>
</div>
<!-- End row -->
</div>
<!-- End container -->