Vertical Menu with Collapsible Submenus: Step-by-Step Divi Tutorial

aniaromanska

Written by Ania Romańska

Published on . Last edited on .

Vertical Divi menu with collapsible submenus and arrow toggles

TL;DR

Add the class <code>dl-v-menu</code> to a Divi Menu module: in Divi 5 via Advanced → Attributes → Add Attribute → CSS Class, in Divi 4 via Advanced → CSS ID & Classes.

The CSS sets the menu wrappers to <code>display: block</code>, hides <code>.et_mobile_nav_menu</code> (the hamburger) and gives <code>ul.sub-menu</code> <code>position: relative; display: none</code>, so the links stay a vertical list on every screen width and Divi's hover dropdown never opens.

The jQuery adds a <code>span.dl-sub-toggle</code> (character 3 in the ETmodules icon font = arrow down) before each sub-menu and opens it with <code>slideToggle()</code>; the <code>toggled</code> class rotates the arrow by 180 degrees.

Parent menu items with the custom link URL <code>#</code> open their sub-menu on click too.

Tested on Divi 5.18: works unchanged on desktop and touch; add <code>text-align: left</code> to the menu items, because the Divi 5 Menu module right-aligns text on tablets and phones by default.

Updated for Divi 5: The CSS and jQuery from this tutorial still work in Divi 5, because the Menu module keeps its Divi 4 class names. I tested them on Divi 5.18 (desktop clicks and phone taps) and added one line, text-align: left, because the Divi 5 Menu module aligns its text to the right on tablets and phones by default.

How to create a vertical menu with collapsible submenus in Divi

Add a CSS class to a standard Divi Menu module, paste a short CSS snippet that turns the menu into a vertical list (on every screen size) and hides the sub-menus, then add a few lines of jQuery that insert an arrow next to each parent item. A click on the arrow slides its sub-menu open or closed.

In this Divi tutorial, we’re going to learn how to customize the Divi Menu Module to turn it vertical, to add collapsible submenus, and to keep the menu visible on any screen size. This layout will be super helpful for footers and sidebars, for example. This could be accomplished using a Text Module, but using a Menu module gives us a few advantages: for example, if we use plugins to enhance our Menu modules, this module will be positively affected as well.

We already have one tutorial on creating a vertical menu in Divi, but in this tutorial, we’ll focus on adding collapsible submenus. We’ll be using custom CSS and jQuery.

You can watch the video for a step-by-step explanation (it’s a replay of a live session from my Divi Stylist Community), read the article and copy the code, or do a combination of both. I hope you have fun and find the steps easy to follow!

The video was recorded in Divi 4. The CSS and jQuery are the same in Divi 5; only the place where you add the CSS class moved (see Step 1), and the CSS below has the extra text-align: left line.

Heads up! If you don't feel up to coding with CSS and jQuery or just want a quick solution to get a beautiful vertical menu for Divi, you can download this free layout! It contains four vertical menu options, and you can use them for any project, personal or commercial, for free. See them on the demo page.

Free download

Vertical Menus with Collapsible Submenus Layout

Four styles of a vertical Divi Menu module with collapsible sub-menus that open on click, with the CSS and jQuery included. Works in Divi 5 and Divi 4.

Get the free download

Step 1: Create a Menu module with a CSS class

Add a standard Divi Menu module, select your menu in Content → Menu, and give the module the CSS class dl-v-menu. In Divi 5 the class goes in Advanced → Attributes → Add Attribute → CSS Class (in Divi 4: Advanced → CSS ID & Classes).

We'll be using a standard Divi Menu module for our navigation. The class is what the CSS and the jQuery look for, so only the menus with this class turn vertical, and your header menu stays as it is.

Divi Menu module settings, Advanced tab, with the CSS class dl-v-menu
The CSS class in the Divi 4 Menu settings. In Divi 5: Advanced → Attributes → Add Attribute → CSS Class.

Tip: if a parent item doesn't need its own page, add it in Appearance → Menus as a Custom Link with the URL #. With the jQuery from Step 3, a click on that link opens its sub-menu too.

Step 2: Add the CSS snippet

The CSS shows the menu links as a vertical list on every screen width, hides the hamburger version, and turns the dropdowns into regular, hidden lists that sit inside the menu (instead of floating over the content). Because the sub-menus are hidden with display: none, Divi's own hover dropdown no longer opens them: only the toggle does.

Place the CSS code in your Theme Options (Divi → Theme Options → General → Custom CSS) or child theme stylesheet. You can also paste it into a Code module on the page, wrapped in <style> tags.

.dl-v-menu.et_pb_menu .et_pb_menu_inner_container,
.dl-v-menu.et_pb_menu .et_pb_menu__wrap,
.dl-v-menu.et_pb_menu .et_pb_menu__menu,
.dl-v-menu.et_pb_menu .et_pb_menu__menu>nav,
.dl-v-menu.et_pb_menu .et_pb_menu__menu>nav>ul {
    display: block;
}

.dl-v-menu.et_pb_menu .et_mobile_nav_menu {
    display: none;
}

#page-container .dl-v-menu ul.sub-menu {
    position: relative;
    visibility: visible;
    opacity: 1;
    top: 0;
    left: 0;
    width: auto;
    border-top: none;
    box-shadow: none;
    display:none;
    transition: none;
}

#page-container .dl-v-menu ul.sub-menu a {
    text-transform: none;
    background:none;
}

#page-container .dl-v-menu li {
    display: block;
    width: fit-content;
    text-align: left;
}

#page-container .dl-v-menu li a {
    width:fit-content;
    display: inline-block;
}

.dl-v-menu .menu-item-has-children>a:first-child:after {
    display: none;
}

span.dl-sub-toggle {
    font-family: 'ETmodules';
    display: inline-block;
    line-height: 18px;
    text-align: center;
    width: 20px;
    height: 20px;
    border: 1px solid #666;
    transform: scale(1.2);
    transition: .3s ease;
    cursor:pointer;
}

.toggled > span.dl-sub-toggle {
    transform: scale(1.2) rotate(180deg);
}

What changed for Divi 5: the first two rules start with .dl-v-menu.et_pb_menu, so they always win over Divi's own rule that hides the menu links at 980px and below (no matter which CSS loads first), and the menu items got text-align: left. Without it, a parent link on a phone jumps to the right when its sub-menu opens, because the Divi 5 Menu module aligns text to the right on tablets and phones. The same CSS works in Divi 4.

Step 3: Add the jQuery code

The jQuery adds a small arrow button before every sub-menu and opens or closes the sub-menu with a slide when you click (or tap) the arrow. The script can be placed inside a Code module directly on your page, or (if it's a functionality you'd like to use globally in various places on your website) in Divi → Theme Options → Integration, in the "Add code to the < body >" field.

<script>
jQuery(document).ready(function($) {
	
	$('.dl-v-menu ul.sub-menu').before('<span class="dl-sub-toggle">3</span>');
	$('.dl-v-menu .menu-item-has-children').off('touchend');
	$(document).on('touchend click', '.dl-sub-toggle, .dl-v-menu li.menu-item-has-children > a[href="#"] ', function(e){
		e.preventDefault();
		$(this).siblings('ul').slideToggle();
		$(this).parent().toggleClass('toggled');
	});

});
</script>

How the jQuery works

  • $('.dl-v-menu ul.sub-menu').before(…) adds a span.dl-sub-toggle before every sub-menu. Its content, the number 3, is the "arrow down" icon of Divi's icon font (ETmodules), which the CSS sets as the span's font.
  • .off('touchend') removes Divi's own touch handler from the parent items, which would otherwise treat the first tap as "show the dropdown" and the second tap as "open the link".
  • The click and touch handler toggles the sub-menu with slideToggle() and adds the class toggled to the parent item. The CSS uses that class to rotate the arrow by 180 degrees.
  • Parent links with the URL # trigger the same toggle (and preventDefault() stops them from jumping to the top of the page).

In Divi 5, an inline script like this one works in a Code module even though Divi loads jQuery at the end of the page: Divi collects jQuery(document).ready() calls made before jQuery is loaded and runs them as soon as it is. I tested the code exactly as above on Divi 5.18.

Does Divi 5 have collapsible sub-menus built in?

Only for the mobile (hamburger) menu. The Divi 5 Menu module has a Collapsible Mobile Menu option in its Design tab: when you turn it on, parent items in the mobile dropdown get an expand/collapse control, and you can pick the open and close icons and their colors.

It doesn't change the desktop menu, though, and the menu still turns into a hamburger icon on smaller screens. If you want a menu that is always visible as a vertical list (like in a footer or a sidebar) with sub-menus that open on click on every device, the CSS and jQuery from this tutorial are still the way to go.

How to style the sub-menus and the arrow

Change the look of the arrow in the span.dl-sub-toggle rule, and the sub-menus with the Menu module's own settings (Design tab) or a little extra CSS.

  • Arrow border: change border: 1px solid #666, or replace it with a background, for example background: #000; color: #fff; border-radius: 3px;.
  • Arrow size: the arrow is 20 × 20px and scaled with transform: scale(1.2). Change the scale in both rules (the normal one and the .toggled > span.dl-sub-toggle one).
  • Sub-menu indent: Divi indents sub-menu items with its own padding. Add padding-left to #page-container .dl-v-menu ul.sub-menu to change it.
  • Colors: menu link color, active link color and the dropdown background are regular Menu module settings, in Divi 5 and Divi 4.

If you'd like four ready-made styles (with filled arrows, link backgrounds, borders or numbered items), grab the free vertical menu layout.

That's how you add vertical sub-menus to a Menu module in the Divi Builder!

Here's the demo page with four Menu modules that use the code shared in this tutorial, each styled a little differently.

I hope you found this tutorial helpful and easy to understand. If you have questions, want advice or clarification, or want to show me your vertical menu, hit me up in my Divi Lovers Facebook group.

This article has been tagged with: Divi 5, Divi CSS

Tags

Vertical Divi menu with collapsible submenus and arrow toggles

Table of Contents

Popular questions

How do I make Divi sub-menus open on click instead of hover?

Hide the sub-menus with CSS (display: none plus position: relative, so they stay inside the menu) and toggle them with a small jQuery script. The script in this tutorial adds an arrow before each ul.sub-menu and runs slideToggle() on click or tap. Because the sub-menu is hidden with display: none, Divi's hover dropdown no longer shows it.

Does this collapsible menu work in Divi 5?

Yes. The Divi 5 Menu module keeps the Divi 4 class names (.et_pb_menu__menu, .et_mobile_nav_menu, .sub-menu, .menu-item-has-children), and the CSS and jQuery were tested on Divi 5.18 with mouse clicks and phone taps, including nested sub-menus. Add text-align: left to the menu items, because the Divi 5 Menu module aligns text to the right on tablets and phones by default.

Does Divi 5 have collapsible sub-menus without code?

Only in the mobile hamburger menu: the Menu module's Design tab has a Collapsible Mobile Menu option that adds expand/collapse controls to parent items in the mobile dropdown. It doesn't change the desktop menu. For a menu that is always shown as a vertical list with click-to-open sub-menus, you still need the CSS and jQuery from this tutorial.

Where do I add the jQuery code in Divi?

Paste it, with its <script> tags, into a Code module on the page, or into Divi → Theme Options → Integration ("Add code to the < body >") to use it on every page. The CSS goes in Theme Options → General → Custom CSS, your child theme stylesheet, or a Code module wrapped in <style> tags.

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.

Hey there!Sorry to interrupt,but this one's important!

Divi Lover is turning 10!
Let’s celebrate with 40% off everything.

Ten years of Divi templates, plugins and tips, and a brand-new website to celebrate it. To say thank you, every product in the shop is 40% off until Cyber Monday.

Your code

Paste it at checkout. Valid until November 30.