Subscribe On YouTube

Join other subscribers and enjoy other Divi video tutorials!

How To Add An Anchor Link Offset In Divi For Fixed Headers

Nelson Miller Profile Orange
In this tutorial I will show you how to add an anchor link offset distance in Divi for when using fixed headers in the Divi Theme Builder.

When you are using a fixed Theme Builder header on your Divi website alongside anchor links, you might have noticed a common issue: clicking on an anchor link causes the page to scroll to the desired section, but the fixed header overlaps the content at the top. This happens because the anchor link does not account for the height of the fixed header. This is not a good user experience, so in this tutorial, I will show you how to incorporate an offset distance for anchor links in Divi to prevent content from being hidden by the fixed header in the Divi Theme Builder.

▶️ Please watch the video above to get all the exciting details! 👆

Copy And Paste The jQuery Code Snippet Into Your Divi Site

In order to resolve the issue of fixed headers overlapping content when using anchor links in Divi, we’ve created a jQuery code snippet to adjust the offset distance when navigating to specific sections of your webpage. 

To implement this solution, simply copy the following jQuery code snippet and paste it into your Divi site. This code will adjust the scroll position when navigating to anchor links, ensuring that the target section is fully visible below the fixed header.

After pasting the code snippet into your Divi website, remember to adjust the scrollTop value in the code to match the height of your fixed header.

Where To Paste The jQuery Code

1. Divi Assistant
If you are using our Divi Assistant plugin, simply paste the code in the jQuery tab in the custom code window in the Divi Visual Builder.

2. Child Theme
If you are using a child theme, paste this code into the scripts.js file (don't forget to remove the <script> tags at the beginning and end). If you don't have a child theme, you can generate a child theme directly on your site or download our free child theme.

3. Divi Theme Options Integration
Otherwise, paste this code in your Divi>Theme Options>Integrations tab in the "Add code to the < head > of your blog" code area.

If you need help understanding where to paste the code, please check out our complete guide about where to add custom code In Divi.

<script>
	jQuery(document).ready(function($) {
		$("a").mouseenter(function() {
			let pac_da_href = $(this).attr("href");
			if (pac_da_href !== undefined && pac_da_href.startsWith("#")) {
				$(this).css('cursor', 'pointer');
				$(this).attr("pac_da_href", pac_da_href);
				$(this).removeAttr("href");
			}
		}).mouseleave(function() {
			let pac_da_href = $(this).attr("pac_da_href");
			if(pac_da_href !== undefined){
				$(this).css('cursor', '');
				$(this).attr("href", pac_da_href);
				$(this).removeAttr("pac_da_href");
			}	
		});
		jQuery('a').on('click', function(event) {
			var hash = $(this).attr('pac_da_href');
			if (hash && hash.startsWith('#')) {
				var targetScrollPosition = $(hash).offset().top - 200;
				$('html, body').animate({
					scrollTop: targetScrollPosition
				});
			}
		});
	});
</script>
how to add an anchor link offset using the Divi Assistant plugin
Categories: Tutorials

Subscribe For More Things Like This!

At the start of each month, we send out a recap newsletter from the month before with family news, Divi news, our latest tutorials, and product news. Occasionally, if the news is too exciting to wait, we will send out another email separate from the monthly newsletter. That’s what you get when you subscribe, and of course you can unsubscribe if you are no longer interested!

Blog Post Optin
Divi Logo (2)

Shop Our Divi Products

Plugins • Courses • Templates

Visit The Shop

Featured Products

Asset 4

New! Trail Guides

Follow a series of blog posts carefully arranged around a specific topic or goal! Keep track of your progress by marking posts completed, just like a free course!

View Trial Guides

Divi Tutorials On YouTube

Our videos have views! Join subscribers and enjoy over video tutorials!

Visit Our Channel

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

10 Comments

Comments By Members

These comments are highlighted because they were posted by fans who have a membership on this site.

  1. Shawn Smith <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

    Excellent post! This is an anoying problem, and you’ve come up with a good solution. Thanks for sharing both the code and the plugin solution. I appreciate your hard work and willingness to teach us. Happy Independence Day! Be well.

    Reply

Comments By Others

  1. John

    Appreciate the code, however, this code “breaks” the Easy Table of Contents plugin. Specifically, the “toggle” option when expanding the table of contents information within Blog Posts, etc.

    Do you know if there’s any way around that?

    Reply
  2. Shab

    Your are a boss ! thank you so much

    Reply
  3. Andy Gough

    This works great using Divi Assistant but it doest seem to work for the ‘Scroll Down Button’ on Fullwidth Header – this still scrolls past my fixed width header. Any ideas on how to fix this?

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Andy!

      Can you share the page URL after disabling the Divi assistant and keep the jQuery code?

      Reply
  4. Connor Freeman

    It would be awesome if there was an option for each module. For example, I have a button which has #contact-us and then on the section I have #contact-us as the id. When the button is clicked the section goes right to the top of the page. I’d prefer if went to the center of the page. However, I don’t want this to happen for everything hence the per-module control.

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Connor!

      Sorry, I’m afraid it is not possible to target the code for specific sections. However, you can change the module to which the ID is added. You can add an empty module (divider) or row just below the current one and add the ID into it. It will cause the anchor to drop a little below.

      Reply

Submit a Comment

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

Recent Posts

Shopping cart0
There are no products in the cart!
You may be interested in…
$19.00$22.50

Select options This product has multiple variants. The options may be chosen on the product page

$16.50$24.00

Select options This product has multiple variants. The options may be chosen on the product page

$16.50$24.00

Select options This product has multiple variants. The options may be chosen on the product page

$19.00$24.00

Select options This product has multiple variants. The options may be chosen on the product page

$20.50$24.00

Select options This product has multiple variants. The options may be chosen on the product page

Graphic showcasing Divi Assistant features and toolset.
From: $69.00 / year

Select options This product has multiple variants. The options may be chosen on the product page

Divi Search Helper Plugin By Pee Aye Creative
From: $29.00 / year

Select options This product has multiple variants. The options may be chosen on the product page

Continue shopping
0