-
-
Notifications
You must be signed in to change notification settings - Fork 79k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navbar hides initial content when jumping to in-page anchor #1768
Comments
I believe the docs say to do
if you use a |
The only, slightly automated, way around this is more javascript and I don't want that just yet. For now, you'll need to calculate your own offset or at the very least add the right amount of padding for key elements. |
I have a similar issue. I have a table where each tr has an Id specific to that row. |
Oh, I misunderstood the problem. Setting a padding on the body only means that the bar doesn't hide the content at the top of the (unscrolled) page. Once you scroll (jump to an anchor), the padding won't help you. You could solve it with JS, something like:
That code's quick-and-dirty – I think |
The javascript works.... if and only if it is registered before the page loads. In a situation where someone is loading with a hash, it does not work - what's the solution for this? |
This may be of interest to all: http://nicolasgallagher.com/jump-links-and-viewport-positioning/demo/ |
@deezer To handle hashes when the page loads, you could do:
|
Had same issue, above js code worked great. Thanks! |
Tried different ways to solve this. The JS way looks the easiest. Thanks! |
This seems to work without any js: http://stackoverflow.com/questions/9047703/fixed-position-navbar-obscures-anchors
|
Just ran into this issue. The JS code worked well for me. |
Thank you @henrik. That JS works flawlessly. |
@henrik That worked for me as well, but I wanted to show a more complete example. The code did not work for me in Firefox unless I had the below ordering.
|
Works like a charm, thanks rpedela for the hint where to put it :-) |
I'm also having this issue but wanted to avoid adding more JS so I created an ".anchor" CSS class and added the padding-top and margin-top styles as josegrad suggested. I only have anchor tags in 2 places so it's no big deal to add a few classes. It's working great, thanks for everyone's input. |
The margin-top padding-top method has a flaw: |
The JS works for me, until I click on the link a second time a second after clicking the first. That action hides the anchor under the nav bar. |
I really like @josegrad 's solution |
I found a different solution that seems to work in both cases. It will scroll to the correct spot, and the selected menu item is correct also. Basically you need to set You can do this with JS by grabbing all the elements that are anchor in the menu and applying the styling to them and their previous sibling. $("header .nav a[href!=#]").each(function(){
$($(this).attr("href")).css("padding-top", "40px").prev().css("margin-bottom", "-40px");
}); |
@henrik @rpedela The javascript didn't work for me on Safari 6 on OS X. Did work in Chrome though.
|
Thank you very much @josegrad! |
FWIW this worked best for me:
|
Thank you @mnot ... works best for me too! |
@mnot Great solution! Thanks. |
@keynote2k was the first to point out that the in-page anchor links in the Guide failed to function properly, due to Bootstrap's fixed navbar. This mixed solution of CSS and Javascript is the best solution I've been able to come up with for the problem. The CSS solution is obviously preferable, and is used herein for those anchor id attributes in the Guide that have no href of their own. Due to problems with using a pure CSS solution where the anchor includes both an href and a id attribute. The Javascript solution is specific for those cases. I took care not to have them both happen at once, as they would undoubtedly conflict. I did a inordinate amount of research about this issue. Bootstrap's own page about the fixed navbar: http://getbootstrap.com/examples/navbar-fixed-top/ doesn't discuss this issue at all, but there is a bug in Booststrap's bugtracker: twbs/bootstrap#1768 which discusses the issue. (However, I don't understand why that bug is closed, since none of the solutions I implement herein truly solve it). The most useful page I found regarding this issue is this one: http://nicolasgallagher.com/jump-links-and-viewport-positioning/demo which offers several pure CCS solutions (each with drawbacks and advantages). Unfortunately, none of those solutions consider the question of anchor links that have both href and id attributes, and none of them work properly in that situation.
If the navbar's position is fixed (e.g. > 980px screen width), then jumping to a in-page anchor link places the initial content for that anchor under the navbar where it is, therefore, hidden from the user.
The text was updated successfully, but these errors were encountered: