Fixed Sidebar Navigation in Fluid Twitter Bootstrap 2.0

Fixed sidebar navigation in fluid twitter bootstrap 2.0

Note: There is a bootstrap jQuery plugin that does this and so much more that was introduced a few versions after this answer was written (almost two years ago) called Affix. This answer only applies if you are using Bootstrap 2.0.4 or lower.


Yes, simply create a new fixed class for your sidebar and add an offset class to your content div to make up for the left margin, like so:

CSS

.sidebar-nav-fixed {
padding: 9px 0;
position:fixed;
left:20px;
top:60px;
width:250px;
}

.row-fluid > .span-fixed-sidebar {
margin-left: 290px;
}

Demo: http://jsfiddle.net/U8HGz/1/show/
Edit here: http://jsfiddle.net/U8HGz/1/

Update

Fixed my demo to support the responsive bootstrap sheet, now it flows with the responsive feature of the bootstrap.

Note: This demo flows with the top fixed navbar, so both elements become position:static upon screen resize, i placed another demo below that maintains the fixed sidebar until the screen drops for mobile view.

CSS

.sidebar-nav-fixed {
position:fixed;
top:60px;
width:21.97%;
}

@media (max-width: 767px) {
.sidebar-nav-fixed {
width:auto;
}
}

@media (max-width: 979px) {
.sidebar-nav-fixed {
position:static;
width: auto;
}
}

HTML

<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<div class="well sidebar-nav sidebar-nav-fixed">
...
</div><!--/.well -->
</div><!--/span-->
<div class="span9">
...
</div><!--/span-->
</div><!--/row-->

</div><!--/.fluid-container-->

Demo, edit here.

minor note: there is about a 10px/1% difference on the width of the fixed sidebar, its due to the fact that since it doesn't inherit the width from the span3 container div because it is fixed i had to come up with a width. It's close enough.

And here is another method if you want to keep the sidebar fixed until the grid drops for small screen/mobile view.

CSS

.sidebar-nav-fixed {
position:fixed;
top:60px;
width:21.97%;
}

@media (max-width: 767px) {
.sidebar-nav-fixed {
position:static;
width:auto;
}
}

@media (max-width: 979px) {
.sidebar-nav-fixed {
top:70px;
}
}

Demo, edit here.

Fixed Sidebar on the Right in fluid layout twitter bootstrap

Just got how to do it without actually using jquery. It is really simple.

I was just missing to add "right:0" to kick it all the way up to the right. So the following code should do it.

<div class="span2" style="position:fixed; right:0">  
CONTENT
</div>

Fixed layout position:fixed sidebar

It's not possible to align the entire page in the center using twitter bootstrap or css.

But possible with a bit of jquery.

function sizing() {
var windowwidth=$(window).width();
var containerwidth=$('.container').width();
var diff=windowwidth-containerwidth+40;
$('#leftmargin').text("window="+ windowwidth+",container="+containerwidth);
if(diff>0) {
$('#leftmargin').css('margin-left', (diff/2) +'px');
} else {
$('#leftmargin').css('margin-left', '20px');
}
}
$(document).ready(sizing);
$(window).resize(sizing);

And add id="leftmargin" like below (scroll right):

<div class="row">
<div class="span3" style="position:fixed;background-color:green;height:100%;top:0px;" id="leftmargin">
position fixed navbar (out of .container)
</div>
</div>
<div class="container">
<div class="row">
<div class="span3" style="background-color:red">empty to avoid overflow</div>
<div class="span9 offset3" style="background-color:blue;">
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
</div>
</div>
</div>​

http://jsfiddle.net/baptme/NzqfL/

twitter bootstrap scrolling side bar

I realised that this is the solution to my question just I looked at it the wrong way:

Fixed sidebar navigation in fluid twitter bootstrap 2.0

I missed it the first time because I wanted my content/map to be static and the sidebar to scroll. I just added to the html template:

<style type="text/css">
.map-fixed {
padding: 9x 0;
position: fixed;
width : 510;
}
</style>

so in the body of my template I was able to fix the map instead of the sidebar

<div class="container">
<div class="row">
<div class="span12">
<div class="row">
<div class="span7 map-fixed">
{{ form.map }}
</div>

<div class="span5 offset7">
<div>{{ string|safe }}</div>
</div>
</div>
</div>
</div>
</div>

Fixed sidebar in bootstrap that starts at a certain position

Lucky for you that is built into Bootstrap. It is a feature called 'Affix' see: http://twitter.github.io/bootstrap/javascript.html#affix for how to use it!

How to fix a slide toolbar in twitter-bootstrap

I have answered this question before and what it entails is a set of new classes to fix your sidebar and another to push your content container away from it.

Twitter Bootstrap Two Column Responsive Layout - Sidebar 100% height with fixed width

Check this out.

<div class="row">
<div class="span3" style="position:fixed;background-color:#46a546;height:100%;top:0px;" id="leftmargin">
position fixed navbar (out of .container)
</div>
</div>
<div class="container">
<div class="row">
<div class="span9 offset3" style="background-color:#049cdb;">
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>bla bla bla<br>
</div>
</div>
</div>

And a bit of jQuery

function sizing() {
var windowwidth=$(window).width();
var containerwidth=$('.container').width();
if(windowwidth<1200) {
var diff=windowwidth-containerwidth+40;
}
else {
var diff=windowwidth-containerwidth+60;
}
$('#leftmargin').text("window="+ windowwidth+",container="+containerwidth);
if(windowwidth<=767) {
$('#leftmargin').css('margin-left', '0px');
$('#leftmargin').css('position', 'relative');
}
else {
$('#leftmargin').css('position', 'fixed');
if(diff>0) {
$('#leftmargin').css('margin-left', (diff/2) +'px');
} else {
$('#leftmargin').css('margin-left', '20px');
}
}
}
$(document).ready(sizing);
$(window).resize(sizing);

http://jsfiddle.net/NzqfL/3/

inspired by my previous post https://stackoverflow.com/a/10972425/1416911

Twitter Bootstrap 2: How to get responsive design to place sidebar on the bottom instead of top?

You can achieve this effect by flipping the containers for the sidebar and content area and just floating them the way you want to. This can be done cleanly without messing around too much with the bootstrap stylesheet by assigning your own ids to the sidebar and content area and doing something like this:

CSS

.sidebar-nav {
padding: 9px 0;
width:100%;
}

#sidebar {
float:left;
margin-left:0;
}

#content {
float:right !important;
margin-left:auto;
}

@media (max-width: 767px) {
#sidebar {
display: inline-block;
margin-top: 20px;
width: 100%;
}

#content {
float:none !important;
margin-left:0;
}
}

Then all you have to do is flip the container divs like so:

<div id="content" class="span9"> ... </div> /* item you want up top first */
<div id="sidebar" class="span3"> ... </div> /* last item to be rendered below */

Demo: http://jsfiddle.net/andresilich/YEUwN/1/show/
Edit here: http://jsfiddle.net/andresilich/YEUwN/1/

Fluid twitter bootstrap layout with min-width columns on left and right (sidebars)

finally found a very well working solution (for me) here also at StackOverflow:
How do I get a three column layout with Twitter Bootstrap?

Anyway thank you very much for your help guys !



Related Topics



Leave a reply



Submit