Sticky (Fixed) SideNav Layout with CSS

By Soh Tanaka | Published August 3rd, 2009 in Tutorials

Today I would like to go over how to create a fixed sidenav layout for your blog or website.

Having a fixed sidenav comes in handy when dealing with blog style websites where the content is extremely tall and there is a need for good amount of scrolling. The fixed navigation allows the user to cruise through the content without scrolling back up to the top to navigate through the rest of the site.

View Demo of Fixed SideNav

Fixed SideNav with CSS

The Base Wireframe – HTML/CSS

First center align your the main container, then add the two main sections (the sidenav and the content).

Fixed SideNav with CSS

HTML

<div class="container">
<div id="sidenav&gt;&lt;!--Fixed Sidenav Goes Here--&gt;&lt;/div&gt; &lt;div id="><!--Content Goes Here--></div>
</div>

Fixing the sidenav is quite simple, just add a fixed position to your sidenav, then float your main content to the right.

CSS

.container {
    width: 980px;
    margin: 0 auto;
    overflow: hidden;
    background: url(container_stretch.gif);
    font-size: 1.2em;
    position: relative;
}
#sidenav {
    width: 300px;
    position: fixed; /*--Fix the sidenav to stay in one spot--*/
    float: left; /*--Keeps sidenav into place when Fixed positioning fails--*/
}
#content {
    float: right; /*--Keeps content to the right side--*/
    width: 640px;
    padding: 0 20px 20px;
}

Unfortunately the fixed position property is not supported in IE6. There is a workaround I stumbled across here that allowed IE6 to behave.

*html #sidenav {
    position: absolute;
    left: expression( ( 0   ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) )   'px' );
    top: expression( ( 0   ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) )   'px' );
}

For those who are not very familiar with how fixed positioning works, do check out the resources below.

Building the SideNav – HTML/CSS

Place the logo first, followed by a level 2 heading, then the navigation as an unordered list.

HTML

<div id="sidenav">
    <a href="/"><img src="logo.gif" alt="" /></a>
    <h2 class="categories">Categories</h2>
    <ul>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
        </ul>
        <h2 class="sites">Other Sites</h2>
        <ul>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
    </ul>
</div>

You will notice that the level 2 headings are replaced with a background image. Take a look at the CSS below.

#sidenav h2 {
    text-indent: -99999px; /*--Push the text off of the page--*/
    height: 41px;
    padding: 0; margin: 15px 0 5px;
    background-position:  20px top; /*--Set position of each heading background--*/
}
h2.categories {background: url(h2_categories.gif) no-repeat ;} /*--Background image for "category" heading--*/
h2.sites {background: url(h2_othersites.gif) no-repeat ;} /*--Background image for "other sites" heading--*/
#sidenav ul {
    margin: 0; padding: 0 20px 30px 20px;
    list-style: none;
    background: url(sidenav_hr.gif) no-repeat right bottom; /*--thin break line underneath the navigation--*/
}
#sidenav ul li{
    margin: 0; padding:  0;
    display: inline; /*--Fixes IE6 bug of double margin--*/
}
#sidenav ul li a{
    display: block;
    margin: 0; padding:  5px 0 5px 15px;
    background: url(sidenav_arrow.gif) no-repeat left center;
    text-decoration: none;
    color: #333;
}
#sidenav ul li a:hover {
    color: #999;
}

Building the Main Content – HTML/CSS

This part is pretty simple and self explanatory. To replace the level 1 heading, use the same text replacement technique as the sidenav level 2 heading.
HTML

<h1>Articles &amp; Resources for Web Designers</h1>
    <h2>Opto tego, distineo luptatum</h2>
    <p>Commoveo wisi nulla pala illum melior quis. Et luptatum validus wisi ingenium humo quidne, eros lucidus dolore ea vel amet. Capto, praemitto singularis tation duis consequat. Jus vulputate ingenium mauris ut, vero. Enim suscipit exerci eligo dolus decet elit transverbero.</p>
</div>

CSS

#content h1 {
    background: url(h1_home.gif) no-repeat center top; /*--Background image of heading--*/
    text-indent: -9999px; /*--Push text off of page--*/
    height: 145px;
    margin: 0 0 0 -20px; /*--Since the #content has a padding of 20px, we will push this to the left -20px so it can align--*/
    padding: 0;
}
#content h2 {
    color: #7f0708;
    margin: 10px 0;  padding: 10px 0;
    font-size: 2em;
    font-weight: normal;
}
#content p {
    line-height: 1.8em;
    padding: 7px 0;
    margin: 7px 0;
}

Potential Accessibility Work Around

One potential accessibility issue of having a fixed sidenav is when the sidenav is taller than the user’s viewport. As you can see in this example, the fixed positioning allows the sidenav to go past the viewport, not allowing the user to view the entire navigation. We will be using a few lines of jQuery to move around this issue.

Do keep in mind, that if you plan on having a meaty sidenav, it may be wise to re-think using this fixed sidenav method. From my experience I feel the best fit scenario is usually a sidenav that is short and simple (Not recommended for a giant sidenav).

jQuery Fix

For those who are not familiar with jQuery, do check out their site first and get an overview of how it works. I’ve shared a few tricks that I have picked up along the way, you can check those out as well.

Initial Step – Call the jQuery file
You can choose to download the file from the jQuery site, or you can use this one hosted on Google.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

The following script contains comments explaining which jQuery actions are being performed.

$(document).ready(function() {

    function staticNav() {
        var sidenavHeight = $("#sidenav").height(); //Get height of sidenav
        var winHeight = $(window).height(); //Get height of viewport
        var browserIE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false; //Check for IE6

        if (browserIE6) { //if IE6...
            $("#sidenav").css({'position' : 'absolute'});  //reset the sidenav to be absolute
        } else { //if not IE6...
            $("#sidenav").css({'position' : 'fixed'}); //reset the sidenav to be fixed
        }

        if (sidenavHeight > winHeight) { //If sidenav is taller than viewport...
            $("#sidenav").css({'position' : 'static'}); //switch the fixed positioning to static. Say good bye to sticky nav!
        }
    }

    staticNav(); //Execute function on load

    $(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
        staticNav();
    });

});

View Demo of Fixed SideNav

Fixed SideNav with CSS

Conclusion

Nice and simple! If you have any questions, concerns, or suggestions, please do not hesitate to let me know.

About the Author:

Soh Tanaka is a Los Angeles based Web Designer and blogger, who recently launched a CSS Web Gallery called Design Bombs. For more front-end web development tutorials, check out his web design blog or you can follow him on twitter @SohTanaka


78 Comments
  1. Janko on August 3rd, 2009

    Very nice tutorial Soh, I like the demo!

  2. John Campbell on August 4th, 2009

    Great tutorial. Good demo and explanation.

  3. Ryan on August 4th, 2009

    Gah! Expressions, UA sniffing! Please if you need to target IE utilise conditional comments.

    I think we are safely at a stage where awful hacks to make it work in IE6 are not really needed, progressive enhancement. Let’s leave IE6 in the dust where it belongs.

  4. Dzinepress on August 4th, 2009

    really great effort. thanks

  5. All for Design on August 4th, 2009

    Waho… Great tutorial !
    Your demo page is very nice !
    Thanks for sharing

  6. Jim on August 4th, 2009

    It appears that the Firefox 3.5.2 update that came through tonight broke the demo. I saw it work earlier, though, and I was very impressed. Hopefully the fix is a simple one.

  7. insic on August 4th, 2009

    wow! very nice. thanks for the tut

  8. Designicle on August 4th, 2009

    It’s really great article. Love the demo and explanation. And very helpfull for us.

  9. [...] Ways to Use Twitter for Business Drop-Down Menu: 30+ Useful Scripts to Enhance Header Navigation G Sticky (Fixed) SideNav Layout with CSS portal shit! : Mac MagMe – Online Magazines Publish and Read Magazines Online SocialSafe: Get [...]

  10. Soufiane on August 4th, 2009

    pretty sample. but the IE6 fix suck :)

  11. Khan on August 4th, 2009

    Really Nice Technique!

    This will also be really useful in web application interfaces.

    Thanks

  12. [...] Ways to Use Twitter for Business Drop-Down Menu: 30+ Useful Scripts to Enhance Header Navigation G Sticky (Fixed) SideNav Layout with CSS portal shit! : Mac MagMe – Online Magazines Publish and Read Magazines Online SocialSafe: Get [...]

  13. Web Design Mumbai on August 4th, 2009

    this looks very neat

  14. [...] Sticky (Fixed) SideNav Layout with CSS [...]

  15. Snoupix on August 4th, 2009

    Very interesting, great tutorial.

  16. Diane on August 4th, 2009

    Nice idea, well done article, except for the use of the expressions. Expressions have a very nasty habit if chewing up a pc’s resources in record time until there’s nothing left as the browser continuously executes those expressions.

    I agree that conditional comments should be used to target IE6. I’d love to leave it in the dust, but I can’t. I work for one of those companies that won’t upgrade IE due to legacy programs that were written to only work with THAT browser – and many in the corporate world are similarly stuck.

  17. [...] (Fixed) SideNav Layout with CSS 4. Aug 2009 Autor: Gerhard | Abgelegt in: Allgemein via [...]

  18. [...] Sticky (Fixed) SideNav Layout with CSS [...]

  19. [...] Stick (Fixed) Sidenav Layout with CSS @ DesignM.ag — good mag, good tutorial [...]

  20. ah_fui on August 5th, 2009

    coolio….. appreciate your effort.

  21. Sam Logan on August 6th, 2009

    I have seen this on a number of website recently and have been wondering what would be the best way to recreate it, thanks for helping me out.

  22. Soh Tanaka on August 7th, 2009

    Thank you all for the feedback!

    If anyone has questions feel free to just let me know~ :-)

  23. Web design tutorials on August 7th, 2009

    Great tutorial – many thanks for sharing!

  24. vsync on August 11th, 2009

    basically IE expression should never be used, is sake of performances.

    I’ve wrote a jQuery plugin for dealing with floating
    menues and such, its cross-platform and
    also highly flexable with project demands.

    check it out:
    http://plugins.jquery.com/project/stickyfloat

    demo:
    http://plugins.jquery.com/files/stickyfloat_0.htm

  25. John Goodwin on August 12th, 2009

    @vsync – I did something similar on my site: http://www.getnorthern.co.uk – but haven’t constrained the navas it’s not necessary.

    My contact details and Twitter posts also flow with the page however, and I’ve notice that, on longer posts in IE, they can sometimes ‘break out’ of the container. I’ll be having a look at your solution to see if it could help, so thanks.

    Great article by the way!

  26. vsync on August 12th, 2009

    @John Goodwin – its not just on IE, but your right side breaks on Firefox also.. when I scroll all the way down.

    you have a well-known bug.
    try to scroll all the way down, then resize your window height to a smaller one, and then scroll down even more.
    it gets into infinite scroll-down.

    my plugin is built for high performance, and should solve your problems.

  27. [...] Sticky (Fixed) SideNav Layout with CSS Awesome tutorial for Sticky SideNav with CSS (tags: design webdesign web inspiration tutorial webdev tutorials jquery css howto javascript layout html web-design navigation hack menu !pm:webdev ie6) [...]

  28. Russell Bishop on August 16th, 2009

    Would have been nicer to keep that top red bar position:fixed too?

  29. W3Spor on August 21st, 2009

    Thank you very much! Good tutorial.

    Best Regards

  30. Alan on August 24th, 2009

    I’m going to add this to me site, as i feel the scrolling will become rather large on my blog section.

    Don’t need the jQuery script though, as my navigation is at the top. :P

    Great work.

  31. adrian on August 31st, 2009

    wow great tutorial
    helped me a lot

  32. [...] Sticky (Fixed) SideNav Layout with CSS [...]

  33. [...] Sticky (Fixed) SideNav Layout with CSS By Soh Tanaka, August 3rd, 2009 Site: DesignM.ag [...]

  34. [...] Sticky (Fixed) SideNav Layout with CSS [...]

  35. landscribe on September 8th, 2009

    You can’t scroll to the bottom of the left hand nav if your screen/browser window is shorter than the length of the nav. Annoying. In FF3.5.2 anyway.

  36. [...] Sticky (Fixed) SideNav Layout with CSS [...]

  37. Ruana on November 13th, 2009

    Hi,

    I tried to incorporate the menu into one of my websites but it didn”t work in IE7. Some time ago I faced a similar problem when I followed another tutorial on how to code a sticky footer.
    Whatever the reason may be…. YOUR demo page AND MY website both don’t work.

    Can anyone help please?

  38. Zimtgruen on November 16th, 2009

    Is there a possibility to get a “head” fixed?

  39. Steven Snell on November 16th, 2009

    Zimtgruen,
    Yes, it’s possible. Here is one example – http://davidwalsh.name/

  40. [...] Sticky (Fixed) SideNav Layout with CSSThis tutorial teaches you how to build a frame-like fixed sidebar navigation menu that stays in place when the page is scrolled. [...]

  41. [...] wrote an article for DesignM.ag on how to create a fixed navigation layout with CSS. You can check out the article here! Today I would like to go over how to create a fixed sidenav layout for your blog or [...]

  42. [...] Sticky (Fixed) SideNav Layout with CSS This tutorial teaches you how to build a frame-like fixed sidebar navigation menu that stays in place when the page is scrolled. [...]

  43. [...] Sticky (Fixed) SideNav Layout with CSS This tutorial teaches you how to build a frame-like fixed sidebar navigation menu that stays in place when the page is scrolled. [...]

  44. [...] Sticky (Fixed) SideNav Layout with CSS This tutorial teaches you how to build a frame-like fixed sidebar navigation menu that stays in place when the page is scrolled. [...]

  45. [...] Sticky (Fixed) SideNav Layout with CSS This tutorial teaches you how to build a frame-like fixed sidebar navigation menu that stays in place when the page is scrolled. [...]

  46. [...] DesignM.ag originally published this tutorial earlier in the year, but I thought it deserved a fresh look! Find out how to create the popular sticky-sidebar effect on your website with this brilliant walkthrough and demo. [...]

  47. [...] DesignM.ag originally published this tutorial earlier in the year, but I thought it deserved a fresh look! Find out how to create the popular sticky-sidebar effect on your website with this brilliant walkthrough and demo. [...]

  48. greenvalue on January 5th, 2010

    nice and very useful in general – EXCEPT:
    in your demo the spacing of the list item in the navigation is so generous that on my screen resolution “Folio Focus” is cut off – the items below I can only see once I press F11 for full screen view (which average user knows about that function?).
    That won’t do! Why? Because with this “nice layout” I cannot scroll down to the last navigation links. [Stupid - ain't it]
    I’m using an ASUS notebook from 2007, set to its maximum resolution; it would be cut off even more on my netbook’s screen.

  49. [...] Criando um menu de navegação lateral fixo e página principal rolante [...]

  50. shoaib on January 12th, 2010

    this is really helpful,you gave me something to play with ,thnx

  51. mark rushworth on January 12th, 2010

    Great post but why does the menu have a h2 at the begining, for semantic reasons shouldnt headings etc be exclusively for the content panel. also for SEO reasons its best to not use heirarchy for side content (it usually messes us the h1 -> h5 flow of headings as theres always some joker who has a h2 or less higher in the code which again doesnt make semantic sense.

  52. ekly on January 13th, 2010

    If you resize your browser window, the sidebar doesn’t stick anymore, even if you put it back to full size. Am I the only one experiencing that?

    FF 3.5.8
    Opera 10.10 on linux

  53. Steven Snell on January 13th, 2010

    ekly,
    It stays sticky for me when the browser (Firefox) is re-sized and after it’s back to the original size.

  54. [...] going to share with you a fast, lightweight method for how we’ll use CSS to do it.”Sticky SideNav Layout with CSSLearn how to create a fixed sidenav layout for your blog or website. Having a fixed sidenav comes in [...]

  55. [...] Sticky SideNav Layout with CSS Learn how to create a fixed sidenav layout for your blog or website. Having a fixed sidenav comes in handy when dealing with blog style websites where the content is extremely tall and there is a need for good amount of scrolling. The fixed navigation allows the user to cruise through the content without scrolling back up to the top to navigate through the rest of the site. [...]

  56. [...] Sticky (fixed) SideNav layout with CSS – DesignM.ag – Link. [...]

  57. LX on January 29th, 2010

    The CSS expressions are not really good. Use JS and the onscroll/onresize Events if you really need it in IE6 (though it would not hurt not to show this trick in IE6).

    If you have full control over the HTML, you could use layered divs, one of them with overflow: auto, though the sizes are a bit tricky.

    Greetings, LX

  58. laxman on February 13th, 2010

    Cooooooool Dude

    Worth a 100+ Bucks

    Gret Tut

  59. [...] Sticky (Fixed) SideNav Layout with CSS [...]

  60. [...] Sticky (Fixed) SideNav Layout with CSS This tutorial teaches you how to build a frame-like fixed sidebar navigation menu that stays in place when the page is scrolled. [...]

  61. [...] surely it's just going to be something like this: Untitled Document Sticky (Fixed) SideNav Layout with CSS – DesignM.ag [...]

  62. Johnny on March 21st, 2010

    good example, but it doesn’t work perfectly.

    I mean, if you resize the window manually to a lower size (600px width, for instance) and scroll horizontally, the non-fixed part overlays the fixed part.

    Please fix it :)

  63. Web Design on April 22nd, 2010

    Nice tutorial I always like the step by step tutorials great share and great stuff thanks

  64. [...] Tutorial Tutorial Page [...]

  65. alisha on May 24th, 2010

    it dont work for me im new to JQuery is there anyway you can do it without Jquery ?? or with javascript or php ?? x

  66. sivaranjan on May 24th, 2010

    Thats one fine example of what CSS can do ! I am so impressed, I am adding this tutorial to my CSS aggregator site. Hope you dont mind.

  67. hwany on June 9th, 2010

    Wow, never realised that something so cool was so simple =D

  68. betty on July 27th, 2010

    YESSS!! Thanks so much for this tutorial. I was trying to avoid the JS and Jquery tidbit for a fixed sidebar and this is perfect :) Definitely retweeting this!

  69. Chris Tanner on July 27th, 2010

    Excellent stuff. Been looking for a clear way to handle this in our app for a while.

  70. Alex Righetto on August 6th, 2010

    I’ve made my own version of this technique, maybe it could be interesting compare whith your.

    here the link: http://www.mendelspeck.it/

  71. Wierzbicki on August 8th, 2010

    It looks great! Very useful, Thanks!

  72. lv bags on August 8th, 2010

    never realised that something so cool was so simple =D

  73. santosh kumar on August 20th, 2010

    nice tutorial