I have seen this topic somewhat covered in posts here and here, but they don’t really help me out. The situation is fairly similar though: a scrolling page and a sticky menu bar (fixed div) at the top of the page, with anchor points scattered through the long scrolling text.
Something like this HTML:
<a name="hd1" class="anchor"><h1>Heading Foo</h1></a>
<p>this is some text, and a lot of it
...
<a href="#hd1">jump to Heading Foo</a>
...
<a name="hdx" class="anchor"><h1>Heading Bla</h1></a>
<p>and then there is more text
...
<a href="#hdx">jump to Heading Bla</a>
...
with some CSS that, for now, is empty because I still just stumble around this issue
.anchor {
color: green;
}
Take a look at this fiddle to see how it works at the moment.
Now, whenever I click on a link it takes me to the anchor. (Yay!) Alas, this also means that the anchor, now at the top of the page, is covered by the sticky menu. (Nay!) It would be great if it would show below the sticky menu.
I’ve tried the solutions that were given in the other posts to no avail. For example, adding padding around the anchor does actually add visible padding in the text and create empty space; which is not what I want. The text ought to appear unmodified visually.
Thanks in advance for hints and tips 🙂
EDIT: I should make it a little more clear. I do not need space on top of the page. I do need every anchor point to be below the menu bar. Try my original fiddle, and click the anchors: you will see how they are positioned so that they are covered by the menu bar.
Thanks for the input, and I found a working solution based on this discussion. The hint from there was “empty anchors”; on my original fiddle the anchors enclosed the heading and that prevented me from positioning the anchors.
Changing to empty anchors
allows me to move the anchors on the page without affecting any of the (visible) text flow. So the CSS for the anchors now becomes
That way, the anchor for a heading is positioned above the heading, and jumping to that anchor causes the heading to show up below the menu bar.
I’ve updated the fiddle to work correctly. (Two things: the anchors use A1, .. text to show their position but you can empty them. Also, I had to explicitly close the
<p>tags to ensure “position:relative” works ok.)