Skip to main content
Galeodan
Known Participant
September 7, 2021
Answered

How do I hyperlink to a point 70px above an Anchor?

  • September 7, 2021
  • 1 reply
  • 1023 views

My page has a sticky menu. Let's say it occupies the top 70px of the screen. To hyperlink to a particular heading, I have to set the anchor 70mm above the heading so the heading is not hidden by the menu. I can do this easily by inserting a 70px high <div> between the anchor and the heading I want to display. But this means I have a 70px space in the page which doesn't look great, especially on mobile. Is there a smarter way to jump to a point 70px above my anchor so I don't leave big gaps everywhere? Or jump to the anchor and auto-scroll up 70px to reveal the heading?

    This topic has been closed for replies.
    Correct answer osgood_
    quote

     

    I'm not sure how that helps. It moves all content down 70px down and leaves a 70mm gap at the top of the page, above the header and menu. If body padding was a solution, it would not work for mid-page anchors. (The first "70mm" was a typo)


    By @Galeodan

     

     

    I'm assuming you have some content before the sticky position takes effect when the page is scrolled and the navigation reaches the top of the browser window, otherwise the padding-top on the body suggestion should work IF the navigation was sticky by default.

     

    Try the below (not quite sure what browsers support 'behavior: smooth' that seems to be a bit vague when you try and look it up on the 'can I use' website.

     

    <script>
    const topLinks = document.querySelectorAll('#mySubnav a[href^="#"]');
    topLinks.forEach(link => {
    link.onclick = function(event) {
    event.preventDefault();
    let anchorHash = this.getAttribute('href');
    let scrollTo = document.querySelector(anchorHash);
    let offsetTopPosition = 100;
    let elementPosition = scrollTo.offsetTop;
    let offsetPosition = elementPosition - offsetTopPosition;
    window.scrollTo({
    top: offsetPosition,
    behavior: "smooth"
    });
    };
    });
    </script>

     

     

    1 reply

    Nancy OShea
    Community Expert
    Community Expert
    September 7, 2021

    To give some breathing room to your page, add CSS padding-top: 70px to your body selector. 

     

    MM & CM are print measures, not digital screen measures.

     

    Hope that helps.

     

    Nancy O'Shea— Product User & Community Expert
    Galeodan
    GaleodanAuthor
    Known Participant
    September 8, 2021
    quote

    To give some breathing room to your page, add CSS padding-top: 70px to your body selector. 


    By @Nancy OShea

     

    I'm not sure how that helps. It moves all content down 70px down and leaves a 70mm gap at the top of the page, above the header and menu. If body padding was a solution, it would not work for mid-page anchors. (The first "70mm" was a typo)

    Legend
    September 10, 2021
    quote

    Any reason you have not tried the solution I provided?

    ...

    If you add the ids to your h2 headings it should work as you require.


    By @osgood_

    The first time I tried it nothing happened. So I was scouring the details trying to figure out if I had a typo, changing the script etc. to try and make it work, without luck. So I decided I should publish the complete page, so you could see it in context. But when I inserted the code this time, it worked (even without adding the id to the h2 headings) . I have no idea why - Maybe something to do with how I cut and pasted it. So now I am going to incorporate it in all the pages that have internal hyperlinks as I would rather not have excess space between sections. I will mark this question as solved, thanks.

     

    PS - In my Reply to Nancy (above) I mentioned that with smoothscrolling on (for the whole page)  the internal  links were inactive for some time. Now, if I deactivate smoothscrolling for the page and have it only in the script, there does not seem to be the same delay in the button's function. I'm not 100% sure of this because I haven't tried it both ways on the same page.

     


    quote

     

    it worked (even without adding the id to the h2 headings) . I have no idea why -

     

     

    It will work if you have left the anchor tags with the ids above the h2 tags BUT I would just get rid of the anchor tags and put the ids directly on the h2 headings, you dont really want unecessary code as it bloats your page.