Fix Divi Anchor Links Stopping at Wrong Place

Written by Dan Mossop

The Divi Theme has a nice feature whereby you can use CSS IDs as anchor links. If you give one of your Divi Builder components (a section, module, etc) a CSS ID of, say, "jumptohere", then you can create a link of the form https://www.mysite.com/mypage/#jumptohere and when clicked it will take your user straight to the part of the page with that component.

Unfortunately Divi doesn't always scroll to the right place. Sometimes it will scroll a bit past the top of the target section. Here's what to do about it.

Fixing the Anchor Link Scrolling with Divi Booster

Divi Booster has had a feature to address the issue since version 2.1.8. It operates in a somewhat similar way to the option (mentioned later in this post) that was available in earlier versions of Divi and is now implemented in Divi by default. However, I've had some reports where the Divi Booster feature has worked where the built-in Divi option hasn't, so you may find it works for you.

You'll find the option at:

Divi > Divi Booster > Site-wide Settings > Links > Fix Divi anchor link scrolling

Fixing the Anchor Link Scrolling with JavaScript

If you don't have Divi Booster, here is the JavaScript code it uses to implement the anchor link scrolling fix:

<script>
document.addEventListener('DOMContentLoaded', function(event){ 
    if (window.location.hash) {
        // Start at top of page
        window.scrollTo(0, 0);
		
        // Prevent default scroll to anchor by hiding the target element
        var db_hash_elem = document.getElementById(window.location.hash.substring(1));
        window.db_location_hash_style = db_hash_elem.style.display;
        db_hash_elem.style.display = 'none';
		
        // After a short delay, display the element and scroll to it
        jQuery(function($){
            setTimeout(function(){
                $(window.location.hash).css('display', window.db_location_hash_style);
                et_pb_smooth_scroll($(window.location.hash), false, 800);
            }, 700);
        });		
    }
});
</script>

[Deprecated] Fixing the Anchor Link Scrolling in the Divi Theme Options

Update: The Divi 4.6.5 update removed this alternative scroll-to-anchor option from Divi Theme Options and made it the default behavior, so this section can be ignored unless you are on an older version of Divi. – Thanks to Randy for pointing this out.

Divi itself recognizes the problem and now has a setting which attempts to solve it. The setting is disabled by default, but you can enable it at:

Divi > Theme Options > Navigation > General Settings > Alternative scroll-to-anchor method

Resolve Scrolling Issues with Divi Booster

Say goodbye to frustrating anchor link scrolling problems with Divi Booster. Our feature directly addresses and fixes the anchor link issue, ensuring a seamless user experience on your Divi site.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

92 Comments

  1. Still having this issue with DIVI 3.0 + :P Had to use this patch

    Reply
    • Interesting, I thought it was fixed in 3.0. I'll look into that. Thanks, Juan!

      Reply
      • Yeah, it seems the Divi fix I was referring to didn't fully solve the issue and the fix given in the post is still required. Thanks again, Juan.

        Reply
  2. Hi I'm trying to link a button to a Bloom email signup field at the bottom of my page. I've #insideroptin in both places, but when I click the button, the link automatically ends with #insideroptin%20 – I can't figure out why. When I manually delete the %20 in the link, it connects to the right place. How do i remove that %20? Thank you!

    Reply
    • Hi evie, %20 is the code for including a space character (i.e. " ") in URLs. I suspect you have a space after the link in the button's settings (i.e. "#insideroptin " instead of just "#insideroptin"). Deleting that extra space should hopefully get it working.

      Reply
  3. hi i got this issue also,

    already use latest divi boster and do "fix divi anchor link scrolling,
    they like jump to link not do smooth scroll,
    i use not on button but on menus

    Reply
    • Hi sapiduduk, are you able to send me a link to the site / page you're working on so that I can take a look? Thanks!

      Reply
  4. Hi Dan,

    I've used it and it does bring me to the right space of my page, not hidden by my header. BUT it does so by opening a new tab !

    Can you help me? Thanks,

    Best,
    Victoria

    Reply
    • Hi victoria, looking on Google Chrome, it's not opening in a new tab for me (so hopefully you were able to fix that.. possibly the button module had the option to open in a new tab set?), but it does reload the page. You can prevent the page from reloading by putting just the id in as the Button URL. I.e. try setting the button to "#pro" instead of "http://www.marchand-normandie.fr/menuiserie-miroiterie/#pro", etc. Hope that helps!

      Reply
  5. Worked like a charm. Thank you kindly! I sure hope they fix this in the regular release.

    Reply
  6. Hi Dan. I tried posting this code into a current Divi release. When I try to save the code I've added to the "head" area on the Integrations tab, it just spins with the "loading" animation. It just hangs.

    I tried a simple alert('test') and that works great. But your code, pasted exactly as above, is not allowed to save…it just "loads" forever. I've tried all kinds of things with your function, including minify, beautify, etc and no matter what it will not save in Integrations.

    Reply
    • Hi Dan. I went through this code line by line, hitting save after each section or line, in different combinations. Literally every line of the code can save EXCEPT for one half of one line:

      var db_hash_elem = document.getElementById(window.location.hash.substring(1));

      This will not save. If I change it to:

      var db_hash_elem = 12345

      …it will save but of course makes no sense. So, this non-working version of the function will save in Integrations (note the "12345" nonsense):

      document.addEventListener('DOMContentLoaded', function(event){

      if (window.location.hash) {
      // Start at top of page
      window.scrollTo(0, 0);

      // Prevent default scroll to anchor by hiding the target element
      var db_hash_elem = 12345;
      window.db_location_hash_style = db_hash_elem.style.display;
      db_hash_elem.style.display = 'none';

      // After a short delay, display the element and scroll to it
      jQuery(function($){
      setTimeout(function(){
      $(window.location.hash).css('display', window.db_location_hash_style);
      et_pb_smooth_scroll($(window.location.hash), false, 800);
      }, 700);
      });
      }
      });

      …but this will not (same line edited to have the real getElementById line:

      document.addEventListener('DOMContentLoaded', function(event){

      if (window.location.hash) {
      // Start at top of page
      window.scrollTo(0, 0);

      // Prevent default scroll to anchor by hiding the target element
      var db_hash_elem = document.getElementById(window.location.hash.substring(1));
      window.db_location_hash_style = db_hash_elem.style.display;
      db_hash_elem.style.display = 'none';

      // After a short delay, display the element and scroll to it
      jQuery(function($){
      setTimeout(function(){
      $(window.location.hash).css('display', window.db_location_hash_style);
      et_pb_smooth_scroll($(window.location.hash), false, 800);
      }, 700);
      });
      }
      });

      Reply
      • Hey Douglas, I've just sent you an email about this. As I say in it, I've just tested on my own site with the latest Divi and I'm not having this problem. I can't see any obvious error with the code itself (nor should the code matter as it isn't actually being run, just saved, at the point the error occurs. But clearly something is going on (I'm thinking a PHP error is being triggered in the background save for some reason). If you're able to let me log in and take a look, hopefully I can track the problem down. Cheers!

        Reply
    • Hey Douglas, I've just sent you an email about this. As I say in it, I've just tested on my own site with the latest Divi and I'm not having this problem. I can't see any obvious error with the code itself (nor should the code matter as it isn't actually being run, just saved, at the point the error occurs. But clearly something is going on (I'm thinking a PHP error is being triggered in the background save for some reason). If you're able to let me log in and take a look, hopefully I can track the problem down. Cheers!

      Reply
  7. That worked great for me! Thanks! Wonder why this hasn't made it into a Divi update by now.

    Reply
  8. Just found this article via Google, as I had this problem on one website. It works great, thank you!

    Reply
  9. very useful – thank you for sharing.

    Reply
    • Thanks hindy!

      Reply
  10. Hi – I checked this option using divi booster but it doesnt seem to work. Its not scrolling to the top of the "About Us" section and when I click on "about us" from another page – it brings me to the home page but the "ABout us" section remains hidden…appreciate any help you can give me with this…ET didnt answer – i posted a question there.

    Reply
    • Hi hindy, I think the problem is that you have an ampersand (&) in your CSS ID. This is triggering a JavaScript error and stopping the linking from working. If you remove it from your CSS ID, that should hopefully fix things.

      Reply
        • Hi Hindy, if I take a look at one your pages, e.g. http://fountainviewcare.forsitewd.com/care/#posthospital, I can see that there is no module with the CSS ID of "posthospital". As a result, it's not able to scroll there. Make sure that each page you link to with an anchor link (i.e. using the #somecssid) has a module with the same CSS ID (e.g. "somecssid" in this example, which you can set in the Custom CSS tab of the module settings), and it should work correctly – let me know if you're still having problems with it though.

          Reply
  11. I have purchased the plugin for this options, but this is not…!

    Reply
    • Hi Ilaria, the option can be found on the Divi Booster settings page at "Site-wide Settings > Links > Fix Divi anchor link scrolling". Let me know if you're having problems with it.

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest From Divi Booster

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Fix Divi Text Module "Regular" Font Weight Not Working

Are you encountering an issue where the font weights in Divi's Text Module don't seem to apply as expected? In particular, you might find that when you set the text module's body text font weight to "Regular", your font is actually assigned a weight of 500 instead of...

Make the Divi Gallery Module Swipeable

The Divi Gallery Module comes with a slider mode that lets you display images in a slider with clickable left/right arrows to scroll through the images. For mobile / touchscreen users, you can improve the user experience by allowing your user to swipe to navigate to...

How to Use Divi Dynamic Content in Woo Modules Product Selector

Divi's Dynamic Content is a powerful feature that lets you populate your Divi modules and theme builder templates with data pulled in various sources, such as custom fields. This allows for efficient organization and maintenance of your sites.Unfortunately, Divi's...

Random Divi Posts