This question already has an answer here:
- Fixed page header overlaps in-page anchors 28 answers
I am trying to clean up the way my anchors work. I have a header that is fixed to the top of the page, so when you link to an anchor elsewhere in the page, the page jumps so the anchor is at the top of the page, leaving the content behind the fixed header (I hope that makes sense). I need a way to offset the anchor by the 25px from the height of the header. I would prefer HTML or CSS, but Javascript would be acceptable as well.
Borrowing some of the code from an answer given at this link (no author is specified), you can include a nice smooth-scroll effect to the anchor, while making it stop at -60px above the anchor, fitting nicely underneath the fixed bootstrap navigation bar (requires jQuery):
The above methods don't work very well if your anchor is a table element or within a table (row or cell).
I had to use javascript and bind to the window
hashchange
event to work around this (demo):* Note: The hashchange event is not available in all browsers.
You can also add an anchor with follow attr:
and give the parent container a position relative.
Works perfect for me.
I had been facing a similar issue, unfortunately after implementing all the solutions above, I came to the following conclusion.
I wrote this simple scrolling js, that accounts for the offset caused due to the header and relocated the div about 125 pixels below. Please use it as you see fit.
The HTML
The JavaScript
See a live implementation here.
Adding to Ziav's answer (with thanks to Alexander Savin), I need to be using the old-school
<a name="...">...</a>
as we're using<div id="...">...</div>
for another purpose in our code. I had some display issues usingdisplay: inline-block
-- the first line of every<p>
element was turning out to be slightly right-indented (on both Webkit and Firefox browsers). I ended up trying otherdisplay
values anddisplay: table-caption
works perfectly for me.I was looking for a solution to this as well. In my case it was pretty easy.
I have a list menu with all the links:
And below that the headings where it should go to.
Now because I have a fixed menu at the top of my page I can't just make it go to my tag because that would be behind the menu.
Instead I put a span tag inside my tag with the proper id.
Now use 2 lines of css to position them properly.
Change the top value to match the height of your fixed header (or more). Now I assume this would work with other elements as well.