One-Page Menu Navigation with Active Anchor Links in Divi

aniaromanska

Written by Ania Romańska

Published on . Last edited on .

One-page menu with the F.A.Q. link highlighted and underlined while the F.A.Q. section is on screen

TL;DR

The script adds the class active to the link inside .dl-anchor-menu whose URL ends with # plus the ID of the section that has crossed the middle of the screen.

It works in Divi 5 and Divi 4: Divi 5 sections still render as div.et_pb_section with your ID, the Menu module keeps the .et-menu-nav > ul.et-menu markup, and jQuery is still loaded.

In Divi 5, add the class dl-anchor-menu and the section IDs in Advanced → Attributes → Add Attribute (Divi 4: Advanced → CSS ID & Classes).

Add the JavaScript in Divi → Theme Options → Integration or in a Code module (wrapped in script tags); add the CSS in Theme Options → General → Custom CSS or your child theme.

WordPress marks every same-page anchor link as current-menu-item, so the Menu module's Active Link Color highlights all of them: style the active class instead.

Updated for Divi 5: I tested both versions of this effect on Divi 5.18 and they work: Divi 5 still renders sections as div.et_pb_section with the ID you give them, the Menu module keeps the Divi 4 menu markup, and jQuery is still loaded. The steps now show where to add the CSS class and the code in Divi 5 (with the Divi 4 path in brackets), and the Extended Version has its complete JavaScript code.

Ready to take your website's navigation to the next level? This is an in-depth walkthrough tutorial on creating a sleek One-Page Menu Navigation with Active Anchor Links in Divi. This advanced tutorial uses jQuery and CSS, but don't worry, I'll explain everything step by step.

In the video, you'll learn how to set up a smooth scrolling navigation menu that highlights active sections as you scroll. And in this blog post, I've included code snippets that you can easily copy and paste to get this effect on your own site.

This tutorial was streamed live inside the Divi Stylist Community, an online space for Divi Stylist Academy students.

The video was recorded in Divi 4. The code works the same way in Divi 5; what changed is where you add the CSS class and the section IDs (Advanced → Attributes in Divi 5), and the Extended Version below uses slightly updated code.

Excited to see this in action? Check out my Divi child theme, Identity. It features a one-page design using this exact navigation setup. See the demo and experience the seamless scrolling for yourself!

Identity Divi child theme one-page design shown in light and dark versions on two tablets

And if you're ready to start learning, let's go!

How do active anchor links work in a Divi one-page menu?

A short jQuery script watches the scroll position, and when the top of a section passes the middle of the screen, it adds an active CSS class to the menu link whose URL ends with # plus that section's ID. You then style that class with CSS, so the highlighted link always matches the section your visitor is reading.

Divi doesn't do this on its own. The Menu module's Active Link Color is meant for the page currently being visited, not for the section in view, and Divi's built-in smooth scrolling only takes care of the jump when someone clicks an anchor link.

What you need before you start

You need a page where every section has a CSS ID, and a menu whose links point to those IDs.

  • Section IDs: open each section's settings and give it a unique ID, for example about. In Divi 5: Advanced → Attributes → Add Attribute → ID (in Divi 4: Advanced → CSS ID & Classes → CSS ID).
  • Anchor links: in Appearance → Menus, add Custom Links with URLs like #about or /your-page/#about. The script only checks that the link URL ends with #about, so both work.

Where to add the code in Divi 5

Add the JavaScript site-wide in Divi → Theme Options → Integration, or on a single page with a Code module, and wrap it in <script> tags in both places. In a child theme JavaScript file, use it without the tags.

  • JavaScript, whole site: Divi → Theme Options → Integration → "Add code to the < head > of your blog" (same in Divi 4).
  • JavaScript, one page: a Code module anywhere on the page.
  • CSS, whole site: Divi → Theme Options → General → Custom CSS, or your child theme's style.css.
  • CSS, one page: in Divi 5, the gear icon top-left → Page Settings → Advanced → Custom CSS (in Divi 4: Page Settings → Advanced → Custom CSS).

Simple version: highlight the active link with a CSS class

The simple version adds an active class to the link itself, so you can change its color or show an underline. It works with a Menu module or a Text module full of links.

Step 1: Add the CSS class

Add a CSS class of dl-anchor-menu to a Menu or a Text module. In Divi 5: Advanced → Attributes → Add Attribute → CSS Class (in Divi 4: Advanced → CSS ID & Classes).

Step 2: Add the JavaScript code

Add the following JavaScript code to your child theme. If you'd rather use the Theme Options Integration tab or a Code module, wrap the code inside <script> tags:

jQuery(document).ready(function($) {

	// Cache the link elements
	var navLinks = $('.dl-anchor-menu a');

	// Add a scroll event listener to the window
	$(window).scroll(function() {

		// Get the current scroll position
		var scrollPosition = $(this).scrollTop();
		var windowHeight = $(window).height();

		// Calculate the center of the screen
		var screenCenter = scrollPosition + windowHeight / 2;

		// Loop through each visible section (hidden sections report a position of 0)
		$('div.et_pb_section:visible').each(function() {
			// Get the position of the current section
			var sectionTop = $(this).offset().top;
			var sectionId = $(this).attr('id');

			// Check if the section crosses the center of the screen
			if (sectionTop <= screenCenter) {

				// Remove the "active" class from all links
				navLinks.removeClass('active');

				// Add the "active" class to the link corresponding to the current section
				$('.dl-anchor-menu a[href$="#' + sectionId + '"]').addClass('active');
			}
		});
	});

	// Highlight the right link on page load too
	$(window).trigger('scroll');

});

This code applies an active CSS class to each link within the module that has the dl-anchor-menu CSS class, but only if the link's URL contains the ID of the section currently visible in the viewport.

Two small updates compared to the original code: sections hidden on the current device are skipped (a hidden section reports a position of 0, so it would always "win"), and the script runs once on page load, so the right link is highlighted before your visitor scrolls.

Step 3: Style the active link

Use the active CSS class to style the highlighted menu item. In the example below, we created a pseudo-element:

.dl-anchor-menu a::before {
	content:'';
	width:100%;
	height:3px;
	background: #000;
	display: block;
	position: absolute;
	bottom: -20px;
	opacity: 0;
	transition: all .3s;
}

.dl-anchor-menu a {
	position:relative;
}
.dl-anchor-menu a.active::before {
	bottom: 0;
	opacity: 1;
}

.dl-anchor-menu.et_pb_menu a.active {
    color: lightsalmon!important;
}

Extended version: an animated highlight that slides between links

The extended version adds the active class to the menu item (the LI) and moves one rounded background shape behind it, so the highlight slides from link to link as you scroll. It needs a Menu module.

Step 1: Add the CSS class to a Menu module

Add a CSS class of dl-anchor-menu to a Menu module. In Divi 5: Advanced → Attributes → Add Attribute → CSS Class (in Divi 4: Advanced → CSS ID & Classes).

Step 2: Add the JavaScript code

Add the following JavaScript code to your child theme. If you prefer to use the Theme Options Integration tab or a Code module, make sure to wrap the code inside <script> tags:

jQuery(document).ready(function($) {

	// Add the animated highlight element to the menu
	var menuList = $('.dl-anchor-menu nav > ul');
	menuList.append('<div id="dl-anchor-active"></div>');
	var highlight = $('#dl-anchor-active');

	// Cache the top-level list items
	var navItems = menuList.children('li');

	// Add a scroll event listener to the window
	$(window).scroll(function() {

		// Get the current scroll position
		var scrollPosition = $(this).scrollTop();
		var windowHeight = $(window).height();

		// Calculate the center of the screen
		var screenCenter = scrollPosition + windowHeight / 2;

		// Loop through each visible section (hidden sections report a position of 0)
		$('div.et_pb_section:visible').each(function() {
			// Get the position of the current section
			var sectionTop = $(this).offset().top;
			var sectionId = $(this).attr('id');

			// Check if the section crosses the center of the screen
			if (sectionTop <= screenCenter) {

				// Remove the "active" class from all list items and hide the highlight
				navItems.removeClass('active');
				highlight.css({ opacity: 0 });

				// Add the "active" class to the LI of the link that points to the current section
				var currentItem = navItems.children('a[href$="#' + sectionId + '"]').parent('li');
				currentItem.addClass('active');

				// Move the highlight behind the active list item
				if (currentItem.length) {
					highlight.css({
						left: Math.round(currentItem.offset().left - menuList.offset().left),
						width: currentItem.outerWidth(),
						opacity: 1
					});
				}
			}
		});
	});

	// Highlight the right link on page load too
	$(window).trigger('scroll');

});

The JavaScript code above adds an active CSS class to the parent LI element of each link within the module that has the dl-anchor-menu CSS class, but only if the link's URL contains the ID of the section currently in the viewport. Additionally, it creates a new element with the ID dl-anchor-active, which will be positioned behind the active list item using CSS.

Step 3: Style the animated highlight

Add the following CSS code to style the animated highlight element, adjust the margin and height to match the size of your navigation.

.dl-anchor-menu .et-menu {
	position: relative;
}
.dl-anchor-menu .et-menu > li {
	z-index: 3;
}
#dl-anchor-active {
	position: absolute;
	transition: all .3s linear;
	top: 50%;
	margin-top: -20px;
	height: 40px;
	z-index: 2;
	background: #eee;
	border-radius: 30px;
	opacity: 0;
}

The highlight takes the full width of the menu item, including Divi's default 11px padding on each side, so you don't need a negative margin to center it. margin-top should always be minus half of the height.

Why are all my menu links highlighted at once?

WordPress gives every menu link that points to the current page the current-menu-item class, and anchor links count, so the Menu module's Active Link Color colors all of them. Set Active Link Color (Design → Menu Text) to the same color as your normal links and style only the active class from this tutorial.

A few other things to check if the highlight doesn't behave:

  • Nothing gets highlighted: check that the section ID and the end of the link URL match exactly (IDs are case-sensitive) and that the module has the dl-anchor-menu class.
  • The highlight disappears between sections: a section without an ID removes the highlight while it's in the middle of the screen. Give it an ID, or leave it like that on purpose for sections that aren't in the menu.
  • The last link never gets highlighted: a section becomes active when its top reaches the middle of the screen. If your last section (plus the footer) is shorter than half the screen, the page can't scroll far enough. Add some bottom padding or make the section taller.

And that's it!

I hope you enjoyed this tutorial and found it helpful! If you have any questions, share them in my Divi Lovers Facebook group, and I'll do my best to help you out.

If you have trouble with this tutorial or any other part of designing a website, I offer 1-on-1 web design assistance: check the options here!

And if you'd like to build a one-page website with this type of navigation fast, Identity is a great way to kickstart your project and save hours upon hours while gaining pixel-perfect, easily customizable design.

This article has been tagged with: Divi CSS, JavaScript & PHP, Menus & Headers

Tags

One-page menu with the F.A.Q. link highlighted and underlined while the F.A.Q. section is on screen

Table of Contents

Popular questions

Does Divi have a built-in way to highlight the active anchor link?

No. Divi 5's Menu module has an Active Link Color setting, but it is meant for the page being visited, and WordPress marks every anchor link to the current page as current-menu-item. To highlight the link of the section in view, you need a small script like the one in this tutorial.

Does this one-page menu code work in Divi 5?

Yes. I tested both versions on Divi 5.18. Divi 5 still outputs sections as div.et_pb_section with the ID you set in Advanced → Attributes, the Menu module keeps the .et-menu-nav > ul.et-menu structure, and jQuery is still loaded, including for scripts in a Code module.

Why are all my anchor links shown in the active color?

WordPress adds current-menu-item to every menu link that points to the current page, including anchor links, and the Menu module's Active Link Color styles that class. Set Active Link Color (Design → Menu Text) to your normal link color and style .dl-anchor-menu a.active instead.

Why doesn't the last menu link get highlighted?

A section counts as active when its top reaches the middle of the screen. If the last section and the footer together are shorter than half the screen, the page can't scroll that far. Make the last section taller or add bottom padding.

About the Author

Ania Romańska

I'm a Divi expert from Poland, with over 10 years building the templates and courses that make up Divi Lover. I'm also a mom of five, which taught me a thing or two about explaining things clearly the first time.

Psst!Black Friday is here.

Black Friday
40% off templates.
Early-bird prices on Divi 5 courses.

Your next Divi project just got a little cheaper.All templates and plugins are 40% off. Plus, grab my new Divi 5 Self-Study Courses at special early-bird prices before they open in January.

No code needed

Sale prices are already applied. Ends November 30.