Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Liam Hawkes
Liam Hawkes
4,094 Points

Bootstrap - fixed navbar with content above

Hi,

I've got a navbar that I'm trying to put content above. Once the user scrolls down past the navbar it becomes fixed to the top.

I found some code on stack overflow on how to achieve this but the content below the fixed navbar seems to jump up a bit when the navbar becomes fixed and I can't work out what is causing this.

Here is an example - http://www.bootply.com/118877

I'd really appreciate if someone could help out!

Thanks, Liam

Hayden Taylor
Hayden Taylor
5,076 Points

Post some code. How do you want to do this? You can do it with javascript / jquery pretty easily.

4 Answers

Hi Liam,

I don't have any experience yet with bootstrap so I can't give you a full, compete answer here but I'll try to explain what's causing this and point you in the direction of the solution.

The amount that the content is jumping up is probably equal to the height of your nav. When your nav position is changed to 'fixed' it is removed from the document flow and your content is then jumping up to occupy the space vacated by the nav.

To counteract this I think you would need to add a top margin to your content that is equal to the height of your nav. You would want to add this at the moment the nav becomes fixed and also remove that extra top margin if the user scrolls back up and the nav becomes 'unfixed'.

Take a look at the "Events" section for "Affix" on this page: http://getbootstrap.com/javascript/#affix

I think you'll want to add event listeners for a couple of those events and then inside the event handlers is where you can add/remove that extra top margin to your content.

Hope this helps.

edit:

I decided to go ahead and try to figure this out. I forked your example. http://www.bootply.com/118900

Is that what you're trying to achieve?

If so, I added this code to your javascript:

$('#nav').on('affix.bs.affix', function () {
    var navHeight = $('.navbar').outerHeight(true);
    $('#nav + .container').css('margin-top', navHeight);
});

$('#nav').on('affix-top.bs.affix', function () {
    $('#nav + .container').css('margin-top', 0);
});
Liam Hawkes
Liam Hawkes
4,094 Points

Hayden Taylor Sorry I didn't explain very well. When I said 'example - link' I meant that's a link to the example of the code I'm using along with a demo of the jumping that's happening.

Thanks for you help

Hayden Taylor
Hayden Taylor
5,076 Points

Yea I soon realized that haha I felt dumb after Check my answer does it help let me know

Hayden Taylor
Hayden Taylor
5,076 Points

Hey I didn't code it all out for you because I am a little confused as to what you wanted to happen.

$(document).ready(function() { $(window).scroll(function () { if ($(window).scrollTop() > $('#nav').offset().top) { console.log("Target Nav Now to be fixed"); } else { console.log("The Nav Is still there Don't make it fixed"); } }); });

You have to open the console to see the text I put there but anyways... that will check if the scroll bar is past your navigation and if it is... DO SOME STUFF (the stuff you want to do like add a class .css() to give it a fixed position and whatever else) ELSE the navigation is visible so do these things...

Liam Hawkes
Liam Hawkes
4,094 Points

Thanks for you help as well. I'll be honest, what you wrote means nothing to me as I know practically zero Javascript... I think some Javascript deep dive lessons are in order for me.

Hayden Taylor
Hayden Taylor
5,076 Points

I wish I was of more help.

That code up there is jquery and it tracks where the scroll bar is relative to the navigation.

Liam Hawkes
Liam Hawkes
4,094 Points

Thanks both for your time and help. Jason Anello that's is perfect, thanks for taking the time to explain and write that out as well.