Skip to content
Advertisement

Add padding-top to scroll when using href=”#id”

I have an anchor tag as follows:

JavaScript

It navigates to a section that has the id ‘map_4D85448A3D4C4180A02BD6FC387ABC45’. The jumptosection function is as follows:

JavaScript

But even if I write nothing in this function, the behaviour is still the same. The problem is that I have a header strip of 92px that hides some part of the section when I click on the given anchor. How do I make it scroll to the given section while adding some pixels to escape the header?

Advertisement

Answer

It is possible. I would do it without javascript, so it works for all. Even no changes on you JS are needed.

You just need to create an empty element above the element you want to scroll to. The CSS does the magic. It creates a hidden box in the height of you offset:

HTML:

JavaScript

CSS:

JavaScript

See a working demo here: https://jsfiddle.net/eczxm1rs/1/

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement