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.
Pure css solution inspired by Alexander Savin:
Optionally you may want to add the following if the target is still off the screen:
You can achieve this without an ID using the
a[name]:not([href])
css selector. This simply looks for links with a name and no href e.g.<a name="anc1"></a>
An example rule might be:
I found this solution:
This doesn't create any gap in the content and anchor links works really nice.
FWIW this worked for me:
You can do it without js and without altering html. It´s css-only.
That will append a pseudo-element before every a-tag with an id. Adjust values to match the height of your header.
For the same issue, I used an easy solution : put a padding-top of 40px on each anchor.