Bootstrap 3: Hide main navbar on Desktop - css

I know this is fairly simple but I got stuck trying to hide the Bootstrap main navbar on screens greater than 768px.
I don't want to hide the <nav> tag. I just want to hide the <div class="collapse navbar-collapse navbar-default custom-menu" id="navbar-collapse-1">...</div>
jsFiddle
<nav class="navbar-blue" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header navbar-default">
<button type="button" class="navbar-toggle navbar-menu" data-toggle="collapse" data-target="#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>
</div>
<!-- User menu to show on screens < 768px -->
<div class="collapse navbar-collapse navbar-default custom-menu" id="navbar-collapse-1">
<ul class="nav navbar-nav client-nav navbar-right">
<li>My Profile</li>
<li>Account Settings</li>
<li>Sign Out</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>

Two things affecting your ability to control this style
The style that sets the display:block for the navbar uses !important. You need to override that with your own !important rule
The style you are using has lower specificity than the one that is making it display:block. The style in effect is .navbar-collapse.collapse (two classes specified), whereas yours is simply .custom-menu (only one class).
To remedy both of these you need a rule similar to this:
#media only screen and (min-width : 768px) {
.navbar-collapse.custom-menu {
display: none!important;
}
}
That will cause the menu to be hidden at those sizes.
Demo: http://jsfiddle.net/4jgL2hfy/

Related

Bootstrap breakpoints

I've been working with bootstrap for a few years now and I've never had the issue I'm currently having. Right now I'm having an issue with the 768px breakpoint not collapsing the navbar like it should. I'm using BS 3.3.5 minified css. It will collapse the navbar at 767px. I've started my own queries but it's bootstrap that's not allowing the navbar to collapse. I promise I know what I'm doing. But this is the first time the BS breakpoints aren't working. I looked at the unminified files and it's saying the break point is 767px but I don't understand why it would have changed from 768px in BS 3.3.2. When I put it to 767px all my columns are pulling the -xs instead of -sm but at 768px it pulls the right column size but it's not collapsing the navbar.
my navigation.
<div id="main-nav">
<nav class="navbar navbar-default">
<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="#main-navbar" 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><!-- end of navbar header -->
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse container" id="main-navbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li>Our Story</li>
<li>Sausage</li>
<li>Pork</li>
<li>Beef</li>
<li>Country Store</li>
<li>Blog</li>
<li>Recipes</li>
<li>Locations</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div><!-- end of main-nav -->

Nav Bar falls below logo while browser resizing on bootstrap

The top right fixed nav bar for bootstrap come below the logo when resizing the screen resolution. I uploaded the image on https://www.imageupload.co.uk/image/5bDD
<nav class="navbar navbar-fixed-top navbar-inverse" role="navigation" style="border:0; border-radius:0;">
<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="navbar-brand" style="padding:2px;" href="index.php"><img src="img/searchimage.png" width="51" height="45" /> </a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>
<i class="fa fa-home fa-2x"></i>
</li>
<li>
AboutUs
</li>
...
...
...
</ul>
</div>
</nav>
Please let me know how to solve this...
Thanks
You need to use media queries to specify a smaller font-size when the screen is smaller. Something like this:
#media (max-width: 900px) {
.navbar-inverse li a {
fonts size: 10px;
}
}
The 900px and 10px are for example purposes so you will have to check out which width and font-size will fix the layout problem.
Also, remove the inline-style padding for your list-item as that is not necessary for aligning the links

Apply CSS only to non collapsed menu

I am trying to apply css rules to my navbar menu, but I do not wish for them to show on the collapsed menu. Using the answer I found here I tried the following:
#media (min-width: 980px) {
/* your conditional CSS*/
}
However if my browser windows is at 900px, the collapsed menu isn't show and therefore my CSS isn't shown. Here is the code for my menu:
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#header_links">
<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="header_links">
...
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
How can I reliably only apply CSS rules to my menu when it isn't collapsed?
Have you tried applying your conditional css under #media (min-width: 900px) {... ?
Here's a sample that changes the background color of the navbar once its at least 900px wide:
http://www.bootply.com/Xt0oIHkhjs

Bootstrap nav toggle without navbar element

Is it possible to create the toggle button without using all the markup for default navigation?
I just want to create simple menu with the collapse feature. For example:
<div class="navigation">
<ul id="nav-to-collapse">
<li></li>
<li></li>
<li></li>
</ul>
<a href="#" class="toggle" data-toggle="collapse" data-target="#nav-to-collapse">
</div>
Would this be possible? Its my first time using Bootstrap (3) and Its really confusing. I dont want to use all the navbar classes because then I would have to overwrite all the navbar styles just to create simple menu that I want.
EDIT: I managed to create the toggle without all the useless navbar classes but there is an issue with the toggle animation, when nav height is changed. Here is a jsfiddle to ilustrate the problem: Jsfiddle demo
Bootstrap's toggle function is a javascript function that is triggered when the user clicks the element with an attribute of data-toggle="collapse". If there is not also a data-target="" id specified, then the element that the user clicks is collapsed. However, in this bit of markup:
<a href="#" class="toggle" data-toggle="collapse" data-target="#nav-to-collapse">
The data-target="" attribute is set to the id of #nav-to-collapse. So Bootstrap's collapse plugin is going to look for an id="nav-to-collapse" and trigger its function on that id.
So in this markup:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<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="#">Project name</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
The data-target is set to the class of .navbar-collapse, which will make everything underneath that element collapse.
Fixed the collapse animation issue by using min-height instead of height on the nav element. Here is a fiddle: http://jsfiddle.net/cfcZY/2/
nav { min-height: 100px; }

Delay (1 second) when centering navbar collapse links

I have a CSS issue that I cannot even begin to get my head around. I have a navbar with the standard collapsed button, that when clicked shows rows of links. I have a CSS media query that stretches the rows and centers the links as so:
http://imageshack.us/a/img89/4940/t678.png
Here is the CSS that does the stretching and centering:
/* when the navbar becomes a button, center and stretch the dropdown links */
#media (max-width: 992px) {
div.navbar-collapse.navbar-ex1-collapse.navbar-right.in {
width: 100%;
text-align: center;
margin: 0 auto;
}
}
Unfortunately, when you first press the collapsible button to unfold the rows, the text does not appear centered. It takes a full 1000ms (1s) for the text to center align. You can literally watch it go from the far left side of the screen to the center.
This leads me to believe I am doing something wrong with my CSS.
How can I fix this issue?
The HTML:
<!-- navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- navbar header -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-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="#">
<img class="img-responsive" src="assets/img/logo.png" >
</a>
</div>
<!-- collapse -->
<div class="collapse navbar-collapse navbar-ex1-collapse navbar-right">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Services</li>
<li>About</li>
<li>Projects</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
<!-- /navbar -->
I found a solution.
To center the text immediately without delay, change the CSS selector from
div.navbar-collapse.navbar-ex1-collapse.navbar-right.in
to
div.navbar-collapse.navbar-ex1-collapse

Resources