Skip to content Skip to sidebar Skip to footer

Space Behind Bootstrap Navbar

Using bootstrap's navbar, I am trying to figure out how to make it not to hide the top of the body section. Actually, it is very well solved using what is recommended here: Twitter

Solution 1:

By adding the :target psuedo-selector to a linked CSS you can apply a defined padding to your selected link (as long as it displays in your browser as: www.example.com#link1).

I spent the last 2 hours trying to search the web trying to find Jquery or JS to apply padding to the target div and after modifying my Google search terms found this

There might be a way to fool proof this with older browsers, but for the time being, this makes me a happy camper.

Solution 2:

Put some top padding on your <h4>'s to account for your navbar's height. I.e. put this in your CSS:

h4 {
  padding-top: 30px;
}

Of course, change 30px to you actual navbar height.

Solution 3:

I've had the same problem and haven't found a real solution, but just more workarounds (like the "padding-top" work-around itself, that seems very hackish to me). It looks like the topbar issue is still unsolved, people just hack around it somehow.

One non-invasive work-around that works for me (as good as it can) is to add the following JavaScript to the page (apart from the "padding-top" hack):

var shiftWindow = function() { scrollBy(0, -50) };
if (location.hash) shiftWindow();
window.addEventListener("hashchange", shiftWindow);

I found this in a comment to a bootstrap GitHub issue. However, the function isn't executed in all necessary occasions (e.g. when going to the same anchor again).

A more reliable work-around is moving the anchor positions using CSS relative positioning (found in this StackOverflow answer:

a.anchor{display: block; position: relative; top: -250px; visibility: hidden;}

However, this is more invasive because you have to give your anchors a class (to distinguish it from links). So you have to update all your anchor HTML code (which might not always be possible, e.g. when you are dealing with a rigid CMS that generates your anchors):

<a class="anchor"id="top"></a>

Post a Comment for "Space Behind Bootstrap Navbar"