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.
For modern browsers, just add the CSS3 :target selector to the page. This will apply to all the anchors automatically.
As @moeffju suggests, this can be achieved with CSS. The issue I ran into (which I'm surprised I haven't seen discussed) is the trick of overlapping previous elements with padding or a transparent border prevents hover and click actions at the bottom of those sections because the following one comes higher in the z-order.
The best fix I found was to place section content in a
div
that is atz-index: 1
:Solutions with changing position property are not always possible (it can destroy layout) therefore I suggest this:
HTML:
CSS:
Use this:
to minimize overlapping, and set font-size to 1px. Empty anchor will not work in some browsers.
This was inspired by the answer by Shouvik - same concept as his, only the size of the fixed header isn't hard coded. As long as your fixed header is in the first header node, this should "just work"