Center Bootstrap Menu Bar - css

I've been trying to tackle this for hours, but I can't understand how to get my menu bar to center, I've tried applying different margins and paddings but obviously that won't center on all devices.
Here is the page showing the menu bar: http://newton-poppleford-weather.co.uk/index.htm
I don't want to paste in the scripts as they are huge, but please could someone give me a hint as to where I should be looking and if I should remove any margins I've set?
Thanks,
William

Okay, I hope this works for you.
Add this to your custom styles:
.navbar-nav {
float: none;
}
.navbar-toggle {
position: absolute;
float: none;
right: 15px;
margin-right: auto;
}
#media screen and (min-width: 768px){
.navbar-nav > li {
display: inline-block;
float: none;
}
}
Then amend your HTML by doing the following:
add hidden-sm hidden-md hidden-lg to navbar-header
add <li class="hidden-xs">Newton Poppleford Weather</li> within navbar-collapse
so your HTML should look like this:
<div class="navbar-header hidden-sm hidden-md hidden-lg">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<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="index.htm">Newton Poppleford Weather</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav" id="main-nav">
<li class="">Newton Poppleford Weather</li>
<li class="active">Now</li>
<li><a id="click-today" href="today.htm" data-toggle="tab" data-tab-url="today.htm">Today</a></li>
<li><a id="click-yesterday" href="yesterday.htm" data-toggle="tab" data-tab-url="yesterday.htm">Yesterday</a></li>
<li><a id="click-thismonth" href="thismonth.htm" data-toggle="tab" data-tab-url="thismonth.htm">This Month</a></li>
<li><a id="click-thisyear" href="thisyear.htm" data-toggle="tab" data-tab-url="thisyear.htm">This Year</a></li>
<li><a id="click-records" href="records.htm" data-toggle="tab" data-tab-url="records.htm">Records</a></li>
<li><a id="click-trends" href="trends.htm" data-toggle="tab" data-tab-url="trends.htm">Trends</a></li>
</ul>
</div><!--/.nav-collapse -->

You could edit your .navbar CSS to:
.navbar .navbar-nav {
display: inline-block;
float: none;
}
.navbar .navbar-collapse {
text-align: center;
}

Related

Bootstrap Navigation - Space Between Menu Items

I have the following Bootstrap navigation set up. I want to reduce the space between each li in the ul under the "Coverage" dropdown-toggle. What setting do I use to do this? As a side note, when I set float: left on the ul li a, the space is reduced, but I don't need float to do this. Why does it reduce the space though?
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand mycontent5" href="/?goHome=true"><span class="glyphicon glyphicon-home"></span></a>
<button type="button" data-toggle="collapse" data-target=".navbar-collapse" class="navbar-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navbar-left">
<ul class="nav navbar-nav">
<li>Analysts</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Coverage<span class="caret"></span></a>
<ul class="dropdown-menu" id="mynavlist">
<li>All Coverage</li>
<li>Coverage Assignments</li>
<li>Portfolio Coverage</li>
<li>Portfolio Snapshot</li>
<li>Writeup Sections</li>
</ul>
</li>
<li>Funds</li>
<li>Intraday Orders</li>
<li>Reports</li>
</ul>
</div>
</nav>
EDIT: I changed my ul to have an id of "mynavlist", and added the following CSS:
#mynavlist li a {
padding-top: 0px !important;
padding-bottom: 0px !important;
margin-top: 0px !important;
margin-bottom: 0px !important;
}
This results in the following: I want to get rid of all white space above and below each item. Increasing the px sizes changes my list as expected, but how can I get rid of the extra space?
You have to reduce the top and bottom padding of each item. Try below css
.navbar-default .dropdown-menu>li>a {
padding: 0 20px;
}
/*For responsive*/
#media (max-width: 767px) {
.navbar-default .navbar-nav .open .dropdown-menu>li>a {
padding: 0 20px;
}
}
.navbar-default .dropdown-menu>li>a {
padding: 0 20px;
}
/*For responsive*/
#media (max-width: 767px) {
.navbar-default .navbar-nav .open .dropdown-menu>li>a {
padding: 0 20px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand mycontent5" href="/?goHome=true"><span class="glyphicon glyphicon-home"></span></a>
<button type="button" data-toggle="collapse" data-target=".navbar-collapse" class="navbar-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navbar-left">
<ul class="nav navbar-nav">
<li>Analysts</li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Coverage<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>All Coverage</li>
<li>Coverage Assignments</li>
<li>Portfolio Coverage</li>
<li>Portfolio Snapshot</li>
<li>Writeup Sections</li>
</ul>
</li>
<li>Funds</li>
<li>Intraday Orders</li>
<li>Reports</li>
</ul>
</div>
</nav>
If you use your browsers Console or Inspector you can view each HTML element and any CSS that is being applied to that element. In the case of Bootstrap's Navbar component you will find the following:
.navbar-nav > li > a {
padding-top: 15px;
padding-bottom: 15px;
}
Changing 15px to some other value will result in your desired output, though you should note that Bootstrap modifies the top and bottom padding for this element based on certain media breakpoints: you will need to account for this in your customizations.

Displaying left and right navbar links on separate lines on collapse

I have a question about positioning my links on navbar collapse. I want the left sided links to be displayed on one line in the collapsed menu, and the right sided links to be displayed on the next line.
Right now I can get all the links displayed on the same line using "display: inline-flex" in my css, but I don't know how to separate them.
Here is my current HTML and CSS:
<div class="container">
<nav class="navbar navbar-inverse">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#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="#">Brand</a>
</div>
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav navbar-left">
<li>Link 1</li>
<li>Link 2</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</nav>
</div>
.navbar-right {
float: right;
margin-right: -15px;
}
.collapse > .navbar-left {
display: inline-flex;
}
.collapse > .navbar-right {
display: inline-flex;
}
.navbar .navbar-nav {
float: none;
vertical-align: top;
text-align: center;
}
And a link to my jsFiddle: https://jsfiddle.net/wdeg6rko/
Any help is much appreciated!
Use a media query to contain the rules so they only apply under 768px and use inline-block against the li items and set the navbar-navs width to 100% while removing any margin so they are actually centered.
Working example Snippet.
#media (max-width: 767px) {
.navbar.navbar-default .navbar-nav {
width: 100%;
text-align: center;
margin: 0;
}
.navbar.navbar-default .navbar-right > li,
.navbar.navbar-default .navbar-left > li {
display: inline-block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#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="#">Brand</a>
</div>
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav navbar-left">
<li>Link 1
</li>
<li>Link 2
</li>
<li>Link 3
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Link 3
</li>
<li>Link 4
</li>
<li>Link 5
</li>
</ul>
</div>
</nav>
</div>
As there is float: left !important; is added to .navbar-left. To override it use following css:
.nav.navbar-nav.navbar-left {
float: none !important;
}
Fiddle

How to center text next to a responsive image in Bootstrap3

I'm using the Boostrap's navbar, inside which there is an image brand (responsive) and some text (menu items). I would like to center the text within the div, but I don't know how: if I center the text in the CSS, there is more space between the left corner of the monitor and the "Home" and and less space between the last "Link" in the right and the right corner in the monitor. It's as if my code takes the div size, subtracts the image size and center the text in the remaining space.
I thought about putting a negative left margin, but because the image is responsive I don't know what value to give.
Can you help me?
http://jsfiddle.net/a001dxn6/2/
HTML
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" 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>
<a class="navbar-brand" href="#"><img src="official_logo.png" /></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 class="active">Home <span class="sr-only">(current)</span></li>
<li class="dropdown">
Specialità <span class="caret"></span>
<ul class="dropdown-menu">
<li>
</li>
<li>La pizza napoletana</li>
<li>Le specialità della casa</li>
</ul>
</li>
<li>Menu</li>
<li>Eventi</li>
<li>Gallery</li>
<li>Contatti</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
CSS
.navbar .navbar-nav {
display: inline-block;
float: none;
vertical-align: top;
}
.navbar-default {
background-color: transparent;
border-color: darkorange;
text-align: center;
}
.navbar-brand > img {
max-height: 100%;
max-width: 100%;
}
[fixed] Just add CSS: .navbar-brand { position: fixed; left: 1vw; }
can't really test with the fiddle, since there's no image.
have you tried removing text-aling: center from .navbar classes
and adding:
.dropdown, .dropdown-menu{
text-align: center;
}
? let me know if it works.

bootstrap menu with click and reveal drop down for screen sizes 768 px and 1099

i am using bootstrap for a drop down menu, and currently if screen sizes is more than 768 px; the drop down occurs when you hover mouse and if it goes below 768PX dropdown occurs when you click the menu item. its perfect.
But i want that from screen-size 768-1099px, it should be not hovering. It should be clicked and drop menu should appear as mobile version.
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse"
data-target="#example-navbar-collapse">
<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="#">TutorialsPoint</a>
</div>
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">iOS</li>
<li>SVN</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Java <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>jmeter</li>
<li>EJB</li>
<li>Jasper Report</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
</div>
</nav>
You can do this easily: http://www.bootply.com/114896
Just wrap that CSS in whatever sized media query you want to target, for example:
#media (min-width: 992px) and (max-width: 1099px) {
.navbar-header {
float: none;
}
.navbar-toggle {
display: block;
}
.navbar-collapse {
border-top: 1px solid transparent;
box-shadow: inset 0 1px 0 rgba(255,255,255,0.1);
}
.navbar-collapse.collapse {
display: none!important;
}
.navbar-nav {
float: none!important;
margin: 7.5px -15px;
}
.navbar-nav>li {
float: none;
}
.navbar-nav>li>a {
padding-top: 10px;
padding-bottom: 10px;
}
}
Thanks

Bootstrap navbar-fixed-top breaks when increasing height

So I have a navbar-fixed-top that's set to collapse at 992px, most mobile screens wont collapse at anything lower than that anymore. Well, from what I can tell anyway. The problem is that when I try increase the navbar height at the lower screen width, I cant vertically center anything on the navbar - being the placeholder for the logo to come or the dropdown button. I'm also struggling to increase the size of the dropdown button no matter what I try. Finally as soon as I increase the height of the navbar, when I click the dropdown menu, the background image disappears yet it works fine with the default height. I'm really hoping someone knows a trick or two as it seems lots of people are struggling with navbar-fixed-top.
Here's the html setup:
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Logo</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapsed-navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="collapsed-navbar">
<ul class="nav navbar-nav">
<li><a ng-href="#link">Link 1</a></li>
<li><a ng-href="#link">Link 2</a></li>
<li><a ng-href="#link">Link 3</a></li>
<li><a ng-href="#link">Link 4</a></li>
<li class="divider"></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><img class="img-circle nav-profile-image" src="images/default_pic.png" /></li>
<li><a ng-href="#link">Profile</a></li>
<li>Logout</li>
</ul>
</div>
</div>
</nav>
And here's the CSS so far:
#media (max-width: 992px) {
.nav-profile-image {
display: none;
}
.navbar {
height: 80px;
font-size: 24px;
}
}
#media (min-width: 992px) {
.nav-profile-image {
height: 40px;
margin-top: 5px;
}
}
.navbar {
background-image: url('../images/noise-background.jpg');
background-color: #F8F8F8;
}
.navbar-default .navbar-nav > li > a:hover {
color: #555;
background-color: #E7E7E7;
opacity: 0.6;
}
.navbar-default .navbar-nav > .active > a:hover {
color: #555;
background-color: #E7E7E7;
opacity: 1;
}
Thanks in advance!

Resources