Modify twitter bootstrap navbar - css

Ive been trying to modify the twitter bootstrap navbar, at the moment all the links are aligned to the left, when what i would really like is the have them central.
In a different post i read that you use this
.tabs, .pills {
margin: 0 auto;
padding: 0;
width: 100px;
}
But this did not work for me
What do i need to change in the css to make this happen, i understand i put the modified css in the bootstrap and overrides.
Any help appreciated
this is my markup
layouts/application
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand">Newbridges</a>
<% if user_signed_in? %>
<div class="nav-collapse">
<ul class="nav ">
<%= render "shared/navbarlogin" %>
</div>
<% else%>
<div class="nav-collapse">
<ul class="nav">
<%= render "shared/navbar" %>
</div>
<% end %>
I've also tried this
.nav > li {
float: none;
display: inline-block;
*display: inline;
/* ie7 fix */
zoom: 1;
/* hasLayout ie7 trigger */
}
.nav {
text-align: center;
}

You can center your nav menu by setting your menu items to display:inline-block instead of float:left like so:
.navbar .nav,
.navbar .nav > li {
float:none;
display:inline-block;
*display:inline; /* ie7 fix */
*zoom:1; /* hasLayout ie7 trigger */
vertical-align: top;
}
.navbar-inner {
text-align:center;
}
Though i suggest you create your own class to target your navbar menu that you wish to center, this way you won't bother the bootstrap default values and mess with other nav sections you may have in your page. You can do it like so:
Notice the .center class in the navbar container
<div class="navbar navbar-fixed-top center">
<div class="navbar-inner">
....
</div>
</div>
And then you can target the .center class like so:
.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;
}
Demo: http://jsfiddle.net/C7LWm/show/
Edit: Forgot to realign the submenu items to the left, this is the fix:
CSS
.center .dropdown-menu {
text-align: left;
}
Demo: http://jsfiddle.net/C7LWm/1/show/

I know this is an old post but I have also noticed this post a few times in my googling. I want to actually set the record straight for centering the navbar in twitter bootstrap as the current accepted method is not wrong, but not needed as twitter bootstrap supports this.
There is actually a better way of doing this then modifying it manually. This will cut down of code by using what is already in twitter bootstrap.
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">RIZEN</a>
<ul class="nav">
<li class="active">Home</li>
<li>Events</li>
<li>Links</li>
<li>Gallery</li>
<li>Contact</li>
<li>Forums</li>
</ul>
</div>
</div>
</div>
Breakdown
First line is pretty simple as we are creating the navbar, setting it to a fixed position on the viewport to the top and then swapping the white to black by inverting the color theme.
Next we are going to assign the navbar-inner which more or less says, if at all possible, set a minimum hight and this is where the actual color stylings come from, not the line above.
Here is the IMPORTANT line as we are dropping the fluid layout for the navbar and going with just a container. This sets a fixed with with margins that center aligns the inner content to the screen. This is done through having auto margins, and setting a width.
This method will do what you want as you actually save kb in header requests by not adding extra code and using the scaffolding properly with twitter bootstrap and not having extra code bloat. I have used this method in my own projects and continue to use it in my current project. The hazard with modifying TBS(twitter bootstrap) is that you lose code consistency and if in the future you want to change things you have to make a mental note of what you have changed otherwise things may not work as intended.
Hope this helps. Also if you want a working example just look at the homepage of twitter bootstrap as it has just that.

Ah and also, if you wish for spaces on both sides (before "Project Name" and after "Dropdown") to be symmetrical, add the following:
.navbar .nav, .navbar .nav > ul
{
float: right
}
Also, it seems that using the following (as of Bootstrap v2.0.4) doesn't center the navbar still:
<div class="navbar navbar-fixed-top center">
<div class="navbar-inner">
<div class="container-fluid">
.
.
.
</div>
</div>
</div>
You need to change to
<div class="navbar navbar-fixed-top center">
<div class="navbar-inner">
<div class="container">
.
.
.
</div>
</div>
</div>
(Extracted from the sample provided by Andres llich; just that he missed out the change)
Now it works for me...

The centering css from above works well on un-collapsed navbars only.
It's not ideal when the navbar is collapsed, though. By default, each collapsed navbar item is on its own row, like a drop down menu. But the css above tries to shove all collapsed items onto the same row, which is less than ideal.
I used this small fix of wrapping the centering css in a media query, so it gets triggered on non-collapsed navbars only:
#media (min-width: 768px) {
.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;
}
}
(tested with bootstrap 3.1.1)

Related

My collapsed menu list is not showing outside its parent. The menu list expands the parent but not he list within each menu

I have a navigation menu set up and on the desktop it works. For moblie screensthe icon expands the parent and displays all the menu items.
The challenge I have is when I click a menu item the list doesn't expand the parent box and the list is hidden within the parent.
I tried changing overflow: hidden to overflow: visible but that just expands the menu from the outset.
I've tried setting z-index but that doesn't appear to work either.
Most of the CSS is just copied from bootstrap
Just a noob so may need to provide more css.
.navbar .btn-navbar {
display: block;
}
.nav-tabs::after,
.nav-pills::after {
clear: both;
}
nav .nav li {
position: relative;
}
<nav class="navigation" role="navigation">
<div id="navigation">
<div class="navbar">
<a class="btn btn-navbar collapsed" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
</div>
<div class="nav-collapse">
<jdoc:include type="modules" name="nav" style="html5" />
</div>
</div>
<div class="row-fluid">
<div class="title">
<H1>TITLE</H1>
</div>
</div>
</div>
</nav>
I want to see the list visible over everything else when the menu name is selected. As it is I see some items but they always stop at the bottom of the parent and hide behind it.
You will have to set a fixed height on your #navigation (could be px, em, rem,...)
For example :
#navigation {
height: 52px;
}
You may also need to add a background-color on your .nav-collapse
Edit :
I added this code in the head tag which resolve some problems
<style>
#media (max-width: 979px) {
nav.navigation {
display: grid;
justify-items : center;
}
div#navigation {
max-height : 60px;
}
div#navigation a.btn-navbar {
padding: 7px 50px; }
div#navigation .nav-collapse {
max-height : 0;
transition: max-height .5s;
}
div#navigation:hover .nav-collapse,
.nav-collapse:hover
{
max-height : 130px;
}
div.title h1 {
margin:0;
padding 2px;
text-align : center;
}
}
</style>
But there are more, and to be honest, to me this is a Bootstrap question more than a CSS question because it's probably easy to solve with a good understanding of bootstrap functionalities. You should think about clarifying your question title on that point (for example "Bootstrap menu always visible on mobile devices")

How can I display text only on the collapsed navbar in Bootstrap?

I'd like to display a telephone number that appears on the navbar next to the navbar toggle button. The toggle button only appears once the navbar is in the collapsed state. I'd like the text to appear next to it.
Here's what I have now:
And here's what I am trying to achieve:
HTML from the relevant divs:
<div class="header-centered"><img src="http://placehold.it/350x150"></div>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Everything you want hidden at 940px or less, place within here -->
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active">Home</li>
<li>Link1</li>
<li>Link2</li>
<li>Link3</li>
<li>Link4</li>
<li>Link5</li>
</ul>
</div>
</div>
</div>
</div>
I don't want the text to appear unless the navbar is collapsed. How can this be achieved?
You can control the visibility of your text by CSS, something like this:
.abcde .text {
display: none;
}
.abcde.collapse .text {
display: inline-block;
}
Easy - just add the following HTML right before the 'hamburger button'
<p class="navbar-text visible-xs-inline-block">Menu</p>
Then style it to suit.
p.navbar-text {
font-size: 22px;
margin-top: 8px;
padding-bottom: 0;
text-align: right;
width: 70%;
}
There are too many ways to handle this situation.
Display: none; It's a browser friendly, they no more assume any element because its in permanently hidden state.
Visibility: hidden; it can hide any element on your document but still it has a limitation, browsers'll have to find that element on the page and then they hide that element. So for a few of few milliseconds but they have to work a bit to hide this.
By keeping font-size: 0; and after collapsing state use font-size: 1em;.
Keep opacity: 0 and later on opacity: 1.
postion: absolute; left:-99999em; and and later left:0;
You can use z-index if you have positioned element. Stacking the element in order also works.
So, finally there are a whole bunch of methods to hide and show elements on your document. It all depends on you whatever you choose.
But according to me Display: none; is the best one for your project.
I'm adding this answer for users that want to use bootstrap way of handling collapsing element using the grid-float-breakpoint , that is the point at which the navbar becomes uncollapsed.
So if you want elements that is visible only when the bar is collapsed add the collapsed-addon class and include this css.
The default gird-float-breakpoint value is setted to screen-sm-min that is 768px
#media (min-width: 768px){
.collapsed-addon {
display: none;
}
If you are using some processors use the css variable grid-float-breakpoint
#media (min-width: $grid-float-breakpoint){
.collapsed-addon {
display: none;
}
}
I searched for the answer and this is what I did - replace Menu with your desired text
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar btn-block" data-target=".navbar-inverse-collapse" data-toggle="collapse">
<span class="icon icon-white icon-align-justify"></span>
MENU</a>
<div class="nav-collapse collapse navbar-inverse-collapse">
<ul class="nav">

CSS Styling with Bootstrap

I'm trying to make a static top bar in a Bootstrap-sass environment.
Problem: Making top bar fixed while keeping it responsive.
I've read CSS documents about inheritance and nested rules, but still unsure how to apply to this case.
Right now, my top bar is fixed to top, but it's not responsive.
CSS
.fixed_pos {
position: fixed;
}
.flowing_body {
margin-top: 100px;
}
Bootstrap CSS
// Reset utility classes due to specificity
[class*="span"].hide,
.row-fluid [class*="span"].hide {
display: none;
}
[class*="span"].pull-right,
.row-fluid [class*="span"].pull-right {
float: right;
}
HTML
<div class="container-fluid">
<div class="fixed_pos row-fluid">
<div class="span9">//left long side of top bar</div>
<div class="span3">//right long side of top bar</div>
</div>
<div class="row fluid flowing_body">
<%= yield %>
</div>
</div>
This is what I tried to add the responsive feature:
CSS
.row-fluid .fixed_pos {
position: fixed;
}
HTML
<div class="row-fluid"><!-- MENUS -->
<div class="fixed_pos">
//everything else the same
</div>
</div>
But then the top bar is shrunk in size and still not responsive.
I appreciate any help with this. There's a very good reference here and I tried to solve it by myself but haven't succeeded yet: Link
This is how I solved the problem:
CSS
.row-fluid .fixed_pos {
position: fixed;
width: 80%;
}
View
<div class="container-fluid">
<!-- TOP BAR, FIXED -->
<div class="row-fluid">
<div class="fixed_pos">
<!-- MENUS -->
<div class="span12">
<ul class="nav nav-tabs">
</appropriate endings..>
Finally the top bar is fixed and responsive.
Add this code in bootstrap-responsive.css
#media (max-width: 979px){
.navbar-fixed-top, .navbar-fixed-bottom {position: fixed;}
.container-fluid{ margin-top:70px;}
}

Twitter Bootstrap Remove Margin of the Span

I am using Twitter Bootstrap. And i have used span8 and span 4 in a row. Is there anyway to remove the leading margin-left:20px from the first span of the row without needing to over ride it manually ?
That 20px margin you see on your #mainContent area is due to the setup of the bootstrap grid, which uses a container of 940px, it is supposed to be removed by the .row container with a margin-left:-20px property. In your setup, your content area is working just the way it was designed too, but your top pageHeader and mainNav sections are not appropriately inserted into the grid, you just have divs inside the .row top sections that are not being contained within the proper containers of the grid.
To fix this you can just insert all of your pageHeader and mainNav elements inside a .span12 container and everything should stack accordingly.
Fixed markup
<header class="row" id="pageHeader">
<div class="span12">
<div id="logo">Logo</div>
<div id="userDetails">userDetails</div>
</div>
</header>
<nav id="mainNav" class="row">
<div class="span12">
<ul>
<li>Home</li>
<li>Dashboard</li>
<li>Blog</li>
<li>Idea Exchange</li>
</ul>
</div>
</nav>
Also, quick tip, you can switch your mainNav background color to the proper grid container of .span12 simply by targeting it like so:
nav#mainNav .span12 {
background: url("../images/nav_bar_bg.png") repeat-x scroll 0 0 transparent;
height: 45px;
overflow: hidden;
}
you can add a class in your css with an !important:
example:
.no_margin{
margin:0px !important;
}
and add that class to your html when required.
(sorry for my bad english xD)
there is also small less utility at
http://getkickstrap.com/extras/#single-view
called flushspan
By using "row" or "row-fluid" class as parent of your span class
<div class="row">
<div class="span3">
<ul>
<li>Home</li>
<li>Dashboard</li>
<li>Blog</li>
<li>Idea Exchange</li>
</ul>
</div>
</div>

top nav bar blocking top content of the page

I have this Twitter Bootstrap code
<div class='navbar navbar-fixed-top'>
<div class='navbar-inner'>
<div class='container'>
<a class='btn btn-navbar' data-target='.nav-collapse' data-toggle='collapse'>
<span class='icon-bar'></span>
<span class='icon-bar'></span>
<span class='icon-bar'></span>
</a>
<div class='nav-collapse'>
<ul class='nav'>
<li class='active'>
<a href='some_url'>My Home</a>
</li>
<li>
<a href='some_url'>Option 1 </a>
</li>
<li>
<a href='some_url'>Another option</a>
</li>
<li>
<a href='some_url'>Another option</a>
</li>
</ul>
</div>
</div>
</div>
</div>
But when I am viewing the beginning of the page, the nav bar is blocking some of the content that is near the top of the page. Any idea for how to make it push down the rest of the content lower when the top of the page is viewed so that the content isn't blocked by the nav bar?
Adding a padding like that is not enough if you're using responsive bootstrap. In this case when you resize your window you'll get a gap between top of the page and navbar. A proper solution looks like this:
body {
padding-top: 60px;
}
#media (max-width: 979px) {
body {
padding-top: 0px;
}
}
Add to your CSS:
body {
padding-top: 65px;
}
From the Bootstrap docs:
The fixed navbar will overlay your other content, unless you add padding to the top of the body.
For bootstrap 3, the class navbar-static-top instead of navbar-fixed-top prevents this issue, unless you need the navbar to always be visible.
a much more handy solution for your reference, it works perfect in all of my projects:
change your first 'div' from
<div class='navbar navbar-fixed-top'>
to
<div class="navbar navbar-default navbar-static-top">
I am using jQuery to solve this problem. This is the snippet for BS 3.0.0:
$(window).resize(function () {
$('body').css('padding-top', parseInt($('#main-navbar').css("height"))+10);
});
$(window).load(function () {
$('body').css('padding-top', parseInt($('#main-navbar').css("height"))+10);
});
In my project derived from the MVC 5 tutorial I found that changing the body padding had no effect. The following worked for me:
#media screen and (min-width:768px) and (max-width:991px) {
body {
margin-top:100px;
}
}
#media screen and (min-width:992px) and (max-width:1199px) {
body {
margin-top:50px;
}
}
It resolves the cases where the navbar folds into 2 or 3 lines. This can be inserted into bootstrap.css anywhere after the lines
body {
margin: 0;
}
I've had good success with creating a dummy non-fixed nav bar right before my real fixed nav bar.
<nav class="navbar navbar-default"></nav> <!-- Dummy nav bar -->
<nav class="navbar navbar-default navbar-fixed-top"> <!-- Real nav bar -->
<!-- Nav bar details -->
</nav>
The spacing works out great on all screen sizes.
The bootstrap v4 starter template css uses:
body {
padding-top: 5rem;
}
As seen on this example from Twitter, add this before the line that includes the responsive styles declarations:
<style>
body {
padding-top: 60px;
}
</style>
Like so:
<link href="Z/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<style type="text/css">
body {
padding-top: 60px;
}
</style>
<link href="Z/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" />
using percentage is much better solution than pixels.
body {
padding-top: 10%; //This works regardless of display size.
}
If needed you can still be explicit by adding different breakpoints as mentioned in another answer by #spajus
with navbar navbar-default everything works fine, but if you are using navbar-fixed-top you have to include custom style body { padding-top: 60px;} otherwise it will block content underneath.
Two problems will happen here:
Page load (content hidden)
Internal links like this will scroll to the top, and be hidden by the navbar:
<nav>...</nav> <!-- 70 pixels tall -->
hello <!-- click to scroll down -->
<hr style="margin: 100px">
<h1 id="hello">World</h1> <!-- Help! I'm 70 pixels hidden! -->
Bootstrap 4 w/ internal page links
To fix 1), as Martijn Burger said above, the bootstrap v4 starter template css uses:
body {
padding-top: 5rem;
}
To fix 2) check out this issue. This code mostly works (but not on 2nd click of same hash):
window.addEventListener("hashchange", function() { scrollBy(0, -70) })
This code animates A links with jQuery (not slim jQuery):
// inline theme global code here
$(document).ready(function() {
var body = $('html,body'), NAVBAR_HEIGHT = 70;
function smoothScrollingTo(target) {
if($(target)) body.animate({scrollTop:$(target).offset().top - NAVBAR_HEIGHT}, 500);
}
$('a[href*=\\#]').on('click', function(event){
event.preventDefault();
smoothScrollingTo(this.hash);
});
$(document).ready(function(){
smoothScrollingTo(location.hash);
});
})
The best solution I've found so far, that does not involve hard coding heights and breakpoints is to add an extra <nav... tag to the markup.
<nav class="navbar navbar-expand-md" aria-hidden="true">
<a class="navbar-brand" href="#">Navbar</a>
</nav>
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
By doing it this way the #media breakpoints are identical, the height is identical (provided your navbar-brand is the tallest object in the navbar but you can easily substitute another element in the non fixed-top navbar.
Where this fails is with screen readers which will now present 2 navbar-brand elements. This points at the need for a not-for-sr class to prevent that element from showing up for screen readers. However that class does not exist https://getbootstrap.com/docs/4.0/utilities/screenreaders/
I've tried to compensate for the screen reader issue with aria-hidden="true" but https://www.accessibility-developer-guide.com/examples/sensible-aria-usage/hidden/ seems to indicate this will probably not work when the screen reader is in focus mode which is of course the only time you actually need it to work...
EDIT: This solution is not viable for newer versions of Bootstrap, where the navbar-inverse and navbar-static-top classes are not available.
Using MVC 5, the way I fixed mine, was to simply add my own Site.css, loaded after the others, with the following line:
body{padding: 0}
and I changed the code in the beginning of _Layout.cshtml, to be:
<body>
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
#if (User.Identity.IsAuthenticated) {
<div class="top-navbar">
you should add
#page {
padding-top: 65px
}
to not destroy a sticky footer or something else
<div class='navbar' data-spy="affix" data-offset-top="0">
If your navbar is on the top of the page originally, set the value to 0. Otherwise, set the value for data-offset-topto the value of the content above your navbar.
Meanwhile, you need to modify the css as such:
.affix{
width:100%;
top:0;
z-index: 10;
}
Add this:
.navbar {
position: relative;
}
You can use .stick-top which would do the same job of fixing the navbar to the top when scrolled without having to add any css padding
<div class="container-fluid mt-3">
<nav class="navbar navbar-expand-sm bg-white navbar-light sticky-top pt-0">
<a class="navbar-brand" href="/">
<img src="/images/logo-full.png" alt="logo" width="150">
</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/">Home</a>
</li>
</ul>
</nav>
<div class="row">
.....
</div>
</div>
Add to your JS:
jQuery(document).ready(function($) {
$("body").css({
'padding-top': $(".navbar").outerHeight() + 'px'
})
});
you can set margin based on screen resolution
#media screen and (min-width:768px) and (max-width:991px) {
body {
margin-top:100px;
}
#media screen and (min-width:992px) and (max-width:1199px) {
body {
margin-top:50px;
}
}
body{
padding-top: 10%;
}
#nav{
position: fixed;
background-color: #8b0000;
width: 100%;
top:0;
}

Resources