92

Is it possible to make sidebar navigation stay always fixed on scroll in fluid layout?

Quentin Pradet
  • 4,691
  • 2
  • 29
  • 41
sl_bug
  • 5,066
  • 5
  • 21
  • 22

7 Answers7

168

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.

Andres I Perez
  • 75,075
  • 21
  • 157
  • 138
  • For me on both Chrome and Firefox, that fiddle always has a horizontal scrollbar (Chrome) below a width of ~1,016 pixels. (Easier to see here: http://jsfiddle.net/U8HGz/130/show/ which is just /1 but displaying the window width and height below "Hello World; I don't know what /2 - /129 are.) – T.J. Crowder Apr 18 '12 at 20:44
  • 1
    @T.J.Crowder that is due to the `margin-left:290px` declaration on the `.span-fixed-sidebar` class made to make way for the sidebar, if it was not there, the content would overlap the sidebar on smaller screens. You can eliminate the horizontal scrollbar though by explicitly hiding it on the `body` tag, like so: `body { overflow-x: hidden; }`. – Andres I Perez Apr 18 '12 at 20:51
  • Thanks. That might work with a media query or something (so it's there on *really* narrow windows); but at that point one probably wants to relocate the sidebar anyway. – T.J. Crowder Apr 18 '12 at 21:01
  • @T.J.Crowder i think that is a good idea, the sidebar can be positioned relatively on window resize as to support narrow windows. Will play around with that later on and post back. – Andres I Perez Apr 18 '12 at 22:25
  • This doesn't seem to add scrollbars on the sidebar if my browser window is too small. If I wanted that should I just stick it in a scrollable div or something? – Ibrahim May 09 '12 at 10:25
  • @IbrahimIn order to get scrollbas on the fixed sidebar if your viewport is smaller than the height of the sidebar you would need to add an specific height (or something like `height:100%` if that works for you) to the `.sidebar-nav-fixed` class i created in conjunction with the property `overflow-y:auto`. – Andres I Perez May 09 '12 at 10:31
  • Breaks responsiveness. le sigh – mauris May 15 '12 at 07:54
  • 1
    @mauris fixed my demo ti support the responsive feature of the bootstrap. – Andres I Perez May 18 '12 at 15:33
  • @AndresIlich **[This overlap](http://i.imgur.com/YnG7K.png)** is the only problem with your last demo. – Xenon Jun 11 '12 at 04:23
  • @AndresIlich It seems like this breaks the fluidity of the design. Because the sidebar isn't scaling, the text is cropped as the window gets narrow and doesn't take up the full width as the window gets wide. – dleavitt Sep 03 '12 at 22:04
  • @Xenon just noticed your comment. Will come up with a fix sometime tomorrow as i don't have the time. – Andres I Perez Sep 04 '12 at 00:50
  • @dleavitt Just tried it out with the latest bootstrap sheet and the sidebar scales for me, does it not expand for you with the browser? – Andres I Perez Sep 04 '12 at 00:51
  • @Xenon issue has been fixed, check out thi fiddle to test: http://jsfiddle.net/ukMqq/94/ – Andres I Perez Sep 04 '12 at 00:58
  • @AndresIlich it's only an issue with the very first one you posted (with the fixed-width sidebar). The responsive ones work great. – dleavitt Sep 05 '12 at 02:56
  • @AndresIlich In your last fix, because you've changed the z-index to -1, nav links are no longer clickable. – miguelcobain Feb 04 '13 at 12:00
  • @miguelcobain on a mobile device you mean? I can click them just fine on a desktop – Andres I Perez Feb 04 '13 at 23:15
  • @AndresIlich When I open this fiddle http://jsfiddle.net/ukMqq/94/ in my Google Chrome in Windows, I can't click any of links in the sidebar. :( – miguelcobain Feb 05 '13 at 09:23
  • 1
    @miguelcobain here is a [fixed version](http://jsfiddle.net/ukMqq/232/), not fully tested though but worked just fine under my test environment. Another solution would be to use the [affix](http://twitter.github.com/bootstrap/javascript.html#affix) bootstrap plugin, which brings native support for fixed sections on the bootstrap....this solution was written at a time when we had none. – Andres I Perez Feb 05 '13 at 12:50
  • I'm using the affix plugin now. The offset support is really useful. Thanks. – miguelcobain Feb 05 '13 at 15:53
  • FYI, your demos are broken – Blowsie Nov 15 '13 at 14:53
46

The latest Boostrap (2.1.0) has a new JS "affix" feature specifically for this type of application, FYI.

Joyrex
  • 1,103
  • 14
  • 24
27

this will screw up the responsive Webdesign. Better wrap the fixed sidebar in a media query.

CSS

@media (min-width: 768px) { 
  .sb-fixed{
    position: fixed;
  } 
}

HTML

<div class="span3 sb-fixed">
   <div class="well sidebar-nav">
      <!-- Sidebar Contents -->
   </div>
</div>

Now the sidebar is only fixed, if the viewpot is bigger then 768px.

HaNdTriX
  • 28,732
  • 11
  • 78
  • 85
1

This isn't possible without javascript. I find affix.js too complex, so I rather use:

stickyfloat

Zuhaib Ali
  • 3,344
  • 3
  • 20
  • 32
0

I started with Andres' answers and ended up getting a sticky sidebar like this:

HTML:

<div class="span3 sidebar-width">
  <div class="well sidebar-nav-fixed">
    Sidebar
  </div>
</div>

<div class="span9 span-fixed-sidebar">
  Content
</div> <!-- /span -->

CSS:

.sidebar-nav-fixed {
  position:fixed;
}

JS/jQuery:

sidebarwidth = $(".sidebar-width").css('width');
$('.sidebar-nav-fixed').css('width', sidebarwidth);
contentmargin = parseInt(sidebarwidth) + 60;
$('.span-fixed-sidebar').css('marginLeft', contentmargin);

I'm assuming I also need JS to update the 'sidebarwidth' variable when the viewport is resized.

mdashx
  • 1,335
  • 2
  • 10
  • 12
0

Very easy to get fix nav or everything tag you want. All you need is to write your fix tag like this, and put it in your body section

   <div style="position: fixed">
       test - try  scroll again. 
   </div>
verdier
  • 17
  • 2
0

With the current Bootstrap version (3.3.2) there is a nice way to achieve a fixed sidebar for navigation.

This solution also works well with the re-introduced container-fluid class, meaning it is easily possible to have a responsive full-screen layout.

Normally you would need to use fixed widths and margins or the navigation would overlap the content, but with the help of the empty placeholder column the content is always positioned in the right place.

The below setup wraps the content around when you resize the window to less than 768px and releases the fixed navigation.

See http://www.bootply.com/ePvnTy1VII for a working example.

CSS

@media (min-width: 767px) { 
    #navigation{
        position: fixed;
    }
}

HTML

<div class="container-fluid">
 <div class="row">
  <div id="navigation" class="col-lg-2 col-md-3 col-sm-3">
    <ul>
      <li><a href="#">Link 1</a></li>
      <li><a href="#">Link 1</a></li>
      <li><a href="#">Link 1</a></li>
    </ul>
  </div>

  <div class="col-lg-2 col-md-3 col-sm-3 hidden-xs">
    <!-- Placeholder - keep empty -->
  </div> 

  <div id="main" class="col-lg-10 col-md-9 col-sm-9 fill">
     ...
     Huge Content
     ...
  </div> 
 </div>
</div>
Christian.D
  • 879
  • 1
  • 9
  • 21