I am using Twitter Bootstrap to play around with the responsive side of a website. I am having a problem however with the smaller widths where the collapsed menu is going over the content of the page, rather than pushing it down.
I used this example to build my navigation:
http://getbootstrap.com/examples/navbar-fixed-top/
Looking at the example, it doesn't push the content down either.
I've seen some answers to this to use padding on the body, but this has not worked for me. I've also tried putting overflow on some elements but its made no difference.
My code for the navigation is:
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<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>
<h1 class="logo-title">
<a href="index.html"><span>Logo</span></a>
</h1>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="index.html">item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item3</a></li>
<li><a href="#">item4</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
I'm new to Responsive Design and have seen many websites with the collapsed menu pushing down the content. Is it good practice to have collapsed menus like this or is it a pure preference thing?
My main question is how can I get the content to be pushed when the collapsed menu is active?
Thank you in advance for the help.
Mention one "id" in
data-target
and add that "id" in the "div" which have collapse navbar code. As below:This
data-target
calls a particular "id" and on the base of that your navbar will toggle.THIS WORKS!!
Had the same issue and all the answers here revert to a static nav bar which is not what you're looking for.
Throw a blank div tag (with height) after the nav bar but before the start of your content. This empty div pushes the content down and remains hidden behind the nav bar. Use javascript/jquery to toggle on the div for the two mobile widths.
HTML
CSS
Javascript/jquery
If you are using a
fixed
navbar expanding the menu won't push down the content. For that you have to usestatic
header itself. Check the bootstrap example link you gave as reference.part 1: What I did was wrap all the elements you want to move
with a div Then give it an id which we will get elements by id later with javascript After that make an empty class in the same div
part 2: Go to your css and add a transition. Don't worry, if you don't know I have the source code here--->
Although this class is not in the div with the id yet, javascript will handle this soon. tip: If you want to get fancy with how the elements will revert back to orignial position, add another transition for when it will come back up--->
The translate measurements are based on twitter bootstrap collapse menu with 4 elements inside.
part 3: The javascript! Find the button for the collapse menu. Should look like this-->
As you can see something is different from the getbootstrap.com example. I have added an icon-bars class. This is so I can find it with-->
You will also need to find the div which wraps the elements needed to be pushed down.
Now this is the juicy part!
I am going to show you how to do it in jquery. Here's the code and the explanation is at the bottom--->
Explanation: -First of we declare this is jquery.
-Then I will check whether the toggle collapse button is clicked, by adding the event handler of click to the first element in the icon list. Because I created only one icon-bars class, the first element of the list will always be the one I targeted.
-Next, I will check if I should make the elements slide up or down depending on if the classes match the conditional statements. Don't need much explaining on the logic as it is pretty straightforward even if you have never tried the hasClass func.
Thats it.
Butt....... It is not good yet. There is a bug where if you double click the toggle button, the collapse menu transition has not yet finished so our push transition goes back up reversing the order. Try it here. Hope there will be some help.
ps. Ignore all the styling in my snippet
-Jack beginner front end web developer
There are many ways you could do that.
One would be to toggle a class on
.container
or whatever element wraps your content below the navigation.For example:
Also to avoid the white space created below the navbar, add the following line to the first "div" after the navbar, e.g: jumborton