Smooth Scroll Offset Anchor Links with CSS

14 Comments

  1. Hey,

    Granted, I know very little about CSS or any code, but it looks like you have given us an example snipet of what you are saying is not the best way, but haven’t given the example of what you suggest. Like you say, I have seen the above in many places. I still can’t get it to work.

    1. Hi Lindsay. Sorry if I wasn’t clear, but the first method works for me (try this link) and is the one I recommend using. The second is just something that I found being posted quite a bit and its not recommended.

  2. commenting to follow

  3. Atomic

    Like Lindsay, I can’t get this to work. Refreshing the page gives different results. Sometimes scrolls too far and the offset doesn’t always apply.

    1. If you’d like to post the code you’re using here or an example URL I’d be happy to take a quick look.

  4. Andrew

    Awesome! Thanks for this Bryan :)

    1. No problem Andrew!

  5. Andrew

    Actually, I thought this was working but now it’s not. I have a position fixed header that has a height of 100px. I have a link within the first div following the header that has a link to a div with the id of “about” . When I click the anchor it isn’t adding the required 100px of space to offset the height of the header. Am I implementing this incorrectly?

    html {
    overflow-x: hidden;
    overflow-y: auto;
    scroll-behavior: smooth;
    }

    :target:before {
    content: “”;
    display: block;
    height: 100px;
    margin: -100px 0 0;
    }

    1. Do you have a URL I could take a look at?

      1. Andrew

        Sure. https://boydells.oxwebdevelopment.com.au/ Click on the white arrow in the first section to scroll down :)

      2. Hi Andrew. I’m not quite sure what you are expecting to happen… It seems to be working okay for me. Here’s a screen recording where it’s behaving as expected: https://share.getcloudapp.com/BluQkLvr

        If you’re expecting something else, let me know – you an also jump on a chat during work hours Mountain time USA here: https://cinchws.com

      3. Andrew

        Gday Bryan, thanks for your time. As currently coded, the scroll-to doesn’t scroll to the very top of the #about div including its top padding.

      4. Hey Andrew. I think to get it the way you want it to work, you’ll need to move the target link inside the current div you’re targeting. Here’s what I mean: https://share.getcloudapp.com/yAuZd0Yp

  6. Thanks for the job done, Bryan!
    Found interesting detail. If target element has top padding, this method doesn’t work. Some strange behavior, but it is what it is.
    So, to have this work, add id=”target” to the element without paddings, maybe some heading. In my case both section and heading have top padding, so I just wrapped section content with div and moved my section top padding to it. Works!

Leave a Reply to Lindsay Whitehead Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.