Divi Mobile: Collapse Submenus

Last modified Jul 28, 2023
Difficulty Intermediate
Language CSS, JavaScript

Watch Tutorial

Customize the mobile menu with this easy to follow JavaScript and CSS snippet! Making submenus collapse keeps the long mobile menus clean and responsive 🙂 It works with the Divi menu module and standard Divi header.

Preview Live Demo

 

Before:

 

After:

Alternative Method

Collapse Submenus make it easier for users to navigate your website. If you are looking for the plugin method for beginners (no coding), check out our Divi Switch Plugin. You can create collapse submenu items inside burger menu on mobile with one click of the switch. The coding method is for more advanced users.

Snippet Limits

The following are the limitations of the snippet:

  • One collapsible menu can be used on the page. (Please let us know in the comments if you would like us to modify the snippet to allow multiple collapsible menus on the page)

CSS

.et_pb_menu .et_mobile_menu .menu-item-has-children > a,
#main-header .et_mobile_menu .menu-item-has-children > a {
    background-color: transparent;
    position: relative;
}

.et_pb_menu .et_mobile_menu .menu-item-has-children > a:after, #main-header .et_mobile_menu .menu-item-has-children > a:after {
    font-family: 'ETmodules';
    text-align: center;
    speak: none;
    font-weight: 600;
    font-variant: normal;
    text-transform: none;
    -webkit-font-smoothing: antialiased;
    position: absolute;
    font-size: 18px;
    content: '\4c';
    top: 11px;
    right: 13px;
}

.et_pb_menu .et_mobile_menu .menu-item-has-children.visible > a:after,
#main-header .et_mobile_menu .menu-item-has-children.visible > a:after {
    content: '\4d';
}

.et_pb_menu .et_mobile_menu ul.sub-menu,
#main-header .et_mobile_menu ul.sub-menu,
.et-db #et-boc .et-l .et_pb_menu .et_mobile_menu li ul.sub-menu {
    display: none !important;
    visibility: hidden !important;
    transition: all 1.5s ease-in-out;
}

.et_pb_menu .et_mobile_menu li.visible > ul.sub-menu,
#main-header .et_mobile_menu li.visible > ul.sub-menu,
.et-db #et-boc .et-l #main-header .et_mobile_menu li.visible > ul.sub-menu,
.et-db #et-boc .et-l .et_pb_fullwidth_menu .et_mobile_menu li.visible > ul.sub-menu,
.et-db #et-boc .et-l .et_pb_menu .et_mobile_menu li.visible > ul.sub-menu {
    display: block !important;
    visibility: visible !important;
}

JavaScript

<script>
jQuery(document).ready(function($){
    function ds_setup_collapsible_submenus() {
    	var $menu = $('.et_mobile_menu'),
    	top_level_link = '.et_mobile_menu .menu-item-has-children > a';
        
        $menu.find('a').each(function() {
            $(this).off('click');
            if ( $(this).is(top_level_link) ) {
                $(this).attr('href', '#');
            }

            if ( ! $(this).siblings('.sub-menu').length ) {
                $(this).on('click', function(event) {
                    $(this).parents('.mobile_nav').trigger('click');
                });
            } 
            else {
                $(this).on('click', function(event) {
                    event.preventDefault();
                    $(this).parent().toggleClass('visible');
                });
            }
    	});
    }
    setTimeout(function() {
    	ds_setup_collapsible_submenus();
    }, 700);
 });
</script>

 

Does this snippet (still) work?

Please let us know in the comments if everything worked as expected. We have tested this code with the Version: 4.9.4 of the Divi Theme and Divi friendly hosting with PHP 7+.
If you think this code saved you time, we will be happy to receive a comment and would motivate us to create more! ?

Don't forget to subscribe to our youtube channel because there’s more to come!

License: This snippet contains code from the Divi Theme, copyright https://elegantthemes.com modified by Divi Space, May 7th, 2021. Licensed under the GNU General Public License v3, no warranty; click here for details.

 

Your Comments

158 Comments

  1. fortespyproductions

    I can’t save the javascript code. i get the spinning wheel but it never saves. Could Wordfence be preventing me from saving the javascript code? How do i solve this problem

    Reply
  2. Mario

    Where does i have to put the javascript? Is that the reason why the submenu on my page doesnt work?

    Reply
  3. Suriya

    Hello, I need some help. I put the codes as shown in the video but my mobile menu is showing only + icons which are not clickable. Like does it work only if I use the default menu? I’m using a custom menu. Please help me.
    Website: https://buyofuel.com/
    Thank You.

    Reply
  4. Jeremiah

    Awesome tutorial – worked like a charm! Thanks so much!

    Reply
  5. Alexander

    code is still running fine.
    DIVI 4.22.1
    WP 6.3.1

    Thank you very much
    Alexander

    Reply
  6. Cheryl Fecteau

    I added the snippet. It collapses the menu, but it won’t expand to sub-items. Instead, when I click a menu item it navigates to the page without expanding. https://stivers.com/

    Reply
    • Jessica Grybek

      I’m having the same issue. How did you resolve it?

    • Zohreh

      Hi How did you fix this issue? and How did you change the chevron icon? Mine is showing letter “L”!

    • Anna Kurowska

      Hi, we have a seperate snippet created to achieve that. This feature is also available in the Divi Switch plugin.

    • Anna Kurowska

      Zohreh, with the performance update ( Divi 4.10.x) by default, the “Dynamic Icons” option is enabled in Divi. If icons in Divi modules on the page are not used, they are not loaded and lead to the display of inappropriate characters, like “L” in your case. You can disable “Dynamic Icons” and it will work again.

      You can always load the font with the CSS and that will allow you to enable the “Dynamic Icons” option.

      @font-face {
          font-family: ETmodules;
          font-display: swap;
          src: url(/wp-content/themes/Divi/core/admin/fonts/modules.eot);
          src: url(/wp-content/themes/Divi/core/admin/fonts/modules.eot?#iefix) format("embedded-opentype"),url(/wp-content/themes/Divi/core/admin/fonts/modules.ttf) format("truetype"),url(/wp-content/themes/Divi/core/admin/fonts/modules.woff) format("woff"),url(/wp-content/themes/Divi/core/admin/fonts/modules.svg#ETmodules) format("svg");
          font-weight: 400;
          font-style: normal
      }
  7. deecos

    I can confirm that the code snippet(s) work perfectly on phone & tablet for Divi ver 4.19.2
    Thanks for sharing this code.. Finding it made my day.

    Reply
  8. Phantom2390

    I added the snippet. It collapses the menu, but it won’t expand to sub items. Instead, when I click a menu item it navigates to the page without expanding.

    Reply
    • Anna Kurowska

      I checked the site you linked and everything works fine for me. Is the issue resolved or issue happens in a specific browser or device?

  9. Emmanuel

    Hello , I need some help. I put the codes as shown in the video but my mobile menu is showing only icons and when i click on the icon it loads the parent page. Please help me. Thank You.

    Reply
    • Anna Kurowska

      Hello, can you send a link to your site?

  10. kaylapalframan

    Hello 🙂

    I inputted the code according to the tutorial but the navigation has not changed. Would you mind having a look?
    https://ddldesign.co.za/

    Reply
    • Anna Kurowska

      Hello, CSS is not loading on your site, there is syntax issue in your CSS code:

      .et_pb_equal_columns>.et_pb_column {
          margin-top: auto;
          margin-bottom: auto .et_pb_menu .et_mobile_menu .menu-item-has-children>a,#main-header .et_mobile_menu .menu-item-has-children>a {
              background-color:transparent;
              position: relative
          }
      
      

      is missing the closing semicolon. Change it to:

      .et_pb_equal_columns>.et_pb_column {
          margin-top: auto;
          margin-bottom: auto
      }
       .et_pb_menu .et_mobile_menu .menu-item-has-children>a,#main-header .et_mobile_menu .menu-item-has-children>a {
              background-color:transparent;
              position: relative
          }
      
      
    • kaylapalframan

      Thank you so much!! It works now

  11. Kayla

    Hello, 🙂 I have added the code and nothing has changed

    I am getting an error in my CSS on line 1 that says Expected RBRACE so not sure if that is relevant.

    Reply
    • Anna Kurowska

      I have the same issue with the current Divi version, it seems not to be related to the code.

  12. Matthias

    Hi, thanks for the snippet. It is still working perfectly.

    Reply
  13. Jason

    Thanks for posting this. Worked great!

    Reply
  14. Logan

    Hi there! Trying to enable the collapsible dropdown for our business (on both prices & programs – submenu & 2ndary submenu as well as under parties & groups – submenus). I’ve installed both the CSS and Java and can’t seem to see the difference on mobile devices after trying in excess. Help?

    Reply
    • Anna Kurowska

      Hi, I checked your site and everything seems working fine on chrome 🙂

  15. Fanny

    Awesome thanks

    Reply
  16. Claudio

    Excellent!!! Thanks!!!!

    Reply
  17. Jon

    I added the CSS and Java script and I don’t see any difference in my mobile menu. Any help would be appreciated. Thanks

    Reply
    • Anna Kurowska

      Hello, looks like you are using the Fullwidth Menu module. Replace in the code et_pb_menu to et_pb_fullwidth_menu.

  18. Michael

    The Divi Switch is brilliant.

    Reply
  19. veronika

    Hi there, I am not sure if I am doing something wrong but the code does not do anything in version 5.9. Used both CCS and JavaScript as explained, the menu does not collapse on mobile. Thanks!

    http://samc45.sg-host.com/

    Reply
    • Anna Kurowska

      Hi Veronika, CSS is not loading on your site – when I added it through browser inspector, everything is working 🙂

  20. Constantine

    Works fine for me, thx a lot. Clean Divi build.

    Reply
    • Constantine

      Hello, thx again for your code, but I have discovered an issue there. How could I make a parent link to be clickable after I open the submenu? For example, I have a parent/sub/children. What should I modify in the code to make my Sub clickable too? Or how could I make only “+” to expand, but the titles to use as links? Thx a lot!

    • Anna Kurowska

      We have a seperate snippet created to achieve that. This feature is also available in the Divi Switch plugin.

  21. Thomas

    Hello! Thank you for this code.

    Is there a way to make the whole link clickable, and not only the “+”?

    Reply
  22. Queenie

    Hello! It’s working when I use Inspect mode on our website and choose the Iphone X dimension. But when I open it on my phone, the menu is not collapsible anymore

    Reply
  23. Basharat Nazeer

    Hi. I just checked it out. Divi theme is not even saving this code. Please help

    Reply
    • Anna Kurowska

      Hi Basharat, I can’t think about the reason why it doesn’t get saved. You may need to contact Elegant Themes support and maybe they can troubleshoot. Unlikely code is the reason. There are a few ways of adding code to the Divi, so maybe you can try alternative options?

  24. Kyle

    Hi I have tried this on our website but it hasn’t. Any chance you could take a look.

    Reply
    • Anna Kurowska

      Seems to be working now 🙂

  25. Lai Jung Ying

    Hi, I wonder if anyone could help?

    I tried the solution but unfortunately, I couldn’t expand the child menu, any idea?

    Reply
    • Anna Kurowska

      Hi, I see the snippet is working fine on your site, the only issue is, it takes above 5 seconds to load the DOM content, and after that JavaScript starts working. More information you can find here about the method used: https://api.jquery.com/ready/

      You can consider optimising your site or simply change the first line of the JavaScript that defines when the JavaScirpt is executed.

      jQuery(document).ready(function($)
  26. Guido

    With the latest performance update (4.10.x) there is a small problem. By default, the “Dynamic Icons” option is enabled in Divi. If otherwise on the page the icons (ETModules) are not used, they are not loaded and lead to the display of inappropriate characters for + and x . You can disable “Dynamic Icons” and it will work again. Maybe the info helps someone. (I think the problem Ajay reports is caused by the divi new options).

    Reply
    • Anna Kurowska

      Thanks for sharing your solution. You can always load the font with the CSS and that will allow you to enable the “Dynamic Icons” option.

      
      @font-face {
          font-family: ETmodules;
          font-display: swap;
          src: url(/wp-content/themes/Divi/core/admin/fonts/modules.eot);
          src: url(/wp-content/themes/Divi/core/admin/fonts/modules.eot?#iefix) format("embedded-opentype"),url(/wp-content/themes/Divi/core/admin/fonts/modules.ttf) format("truetype"),url(/wp-content/themes/Divi/core/admin/fonts/modules.woff) format("woff"),url(/wp-content/themes/Divi/core/admin/fonts/modules.svg#ETmodules) format("svg");
          font-weight: 400;
          font-style: normal
      }
  27. Ajay

    Re font not loading on chrome, the site is buddhistimages.co.uk. Looks fine on an android phone, but not a google phone…don’t know about iphone…

    Reply
    • Anna Kurowska

      Can you check the solution provided by Guido and disable the “Dynamic Icons” option from Divi settings or add the CSS provided below

      @font-face {
          font-family: ETmodules;
          font-display: swap;
          src: url(/wp-content/themes/Divi/core/admin/fonts/modules.eot);
          src: url(/wp-content/themes/Divi/core/admin/fonts/modules.eot?#iefix) format("embedded-opentype"),url(/wp-content/themes/Divi/core/admin/fonts/modules.ttf) format("truetype"),url(/wp-content/themes/Divi/core/admin/fonts/modules.woff) format("woff"),url(/wp-content/themes/Divi/core/admin/fonts/modules.svg#ETmodules) format("svg");
          font-weight: 400;
          font-style: normal
      } 

      I checked your site on Nexus 7 + Chrome and fonts are loading fine. I would need more information to replicate the issue

    • Ajay

      Many thanks – I found the suggestion from Guido exactly right. I appreciate your time on this

  28. Jon C

    This worked for our site. Thank You!!!

    Reply
    • Anna Kurowska

      Great!!

  29. Dawn

    Hello. Thank you for this code. I am having the same problem as Guido, the mobile menu works but when I tap on a sub item with a ‘+’ sign it does not open that menu item, but makes the menu close again.
    I am testing this using the CSS in the Divi Theme Options, in both Safari and Chrome. I tried using the Javascript in the ‘Head” section of the Integrations tab but that didn’t work at all. I also tried using the code on this page https://wpzone.co/wordpress-and-divi-code-snippets/divi-mobile-collapse-submenus-with-clickable-parent-menu-items/ but neither the Javascript nor the CSS worked for me at all. I did watch your video on how to add Javascript correctly and it was the method I was already using.
    Any input you have would be very appreciated. Thank you.

    Reply
    • Dawn

      Hi, Thank you for the reply. I had previously read that article, and tried the Javascript in the ‘Head’ section of Integrations. That did not work at all. What am I missing from that article… any more specific help you could offer would be appreciated.

    • Dawn

      Hi again, I just added the Javascript back into the Head area, and left the CSS in place too. That works. I appreciate all your free articles, and your great replies, but I have to say that it was not at all clear from either this page or that other article that BOTH needed to be in place. My understanding from these two resources was either add one method or the other. Perhaps on this page you could say up at the very top “Add both the CSS and Javascript in order for this to work”. Just my humble opinion. Thanks for the code, very appreciated.

    • Anna Kurowska

      Great that it worked! Using the snippets need at least a basic understanding of CSS and JavaScript. We also recommend two articles before you get started with snippets:

      – How to Add JavaScript and jQuery to Divi https://wpzone.co/tutorials/how-to-add-javascript-and-jquery-to-divi/
      – How to Add CSS to Divi https://wpzone.co/tutorials/5-ways-to-add-css-to-divi/

      If you are not feeling comfortable using the code, check the alternative method, that requires no coding – just install, activate and toggle a switch in a Divi Switch Plugin.

  30. Ajay

    Hi, after hours of searching, trying plugins, I finally have almost got your solution to work. Only problem is that instead of the + symbol I get an L – can’t work out why!
    Any suggestions please?

    Reply
    • Ajay

      Actually, it just seems to be Chrome/google that this happens….

    • Anna Kurowska

      Hi, there must a problem with the fonts not loading. We used icons from Elegant Themes

      font-family: 'ETmodules';

      You can send link to your site and I will take a look

  31. Gino

    Hi, I used the code and it works fine on mobile, but now the desktop menu is all messed up… Any thoughts? Help is much appreciated.
    Thank you otherwise already for great work!

    Reply
    • Anna Kurowska

      Hi, can you send more information about the issue?

  32. Gregory

    Hi, Divi Theme Options does not like the ‘>’ on the css portion of code. Gives ‘special characters must be escaped’. I am assuming it wants to close the signs. Thoughts?

    Reply
    • greg

      solved my problem. put css in integration > header section, not custom css in general. Thanks!

    • Joshua

      Thank you for sharing your solution with everyone.

  33. luc

    Thank you so much for both of the code snippets, they worked like a charm. It will be great if we could achieve an event listener that could close the upper opened submenu.
    For instance saying we have 4 menus and each of it has 2 submenus, when I open second menu, the submenu that it is collapsed, it will close. I really think we can achieve that… It will be of a great help.

    Again thank you very much Anna.

    Reply
    • Anna Kurowska

      Hi luc, can you send me more information about your use case? I don’t understand the issue and your menu(s) structure. We could have it sorted out and adjust the snippet.

  34. Ruud

    works fine, thank you!

    Is it possible to collapse only when pushing the +, and to go to the menuitem when clicked on the menuitem itself?

    Reply
  35. Bobby

    Works perfectly! THANK YOU THANK YOU!!!! 🙂

    Reply
  36. Kevin

    Hallo,

    I insert the code and everything is working only the sub items are not clickable anymore.

    Reply
    • Anna Kurowska

      Hi, can you send link to your site?

  37. Guido

    Currently it does not seem to work with Firefox Android (Firefox iOS works just like Chrome, Safari, Edge, Opera). We can also reproduce the problem also with Firefox windows desktop (small window). Clicking on one of the submenus or “+” closes the complete mobile navigation. Menu items without submenus work. Our Website is linked with our comment. We have also tested some of the other site links from previous comments of other users here (e.g. https://bserver.es/sjd/) and there is the problem as well. Any idea? We use Divi 4.9.4 an PHP 7.4 (right now caching is activated – but problem is the same with all other plugins deactivated).

    Reply
    • Anna Kurowska

      Hi Guido, We updated CSS, can you update it on your site as well and let us know if that fixes the issue?

    • Guido

      Hi Anna, thanks for the quick reply. I have changed the CSS (only CSS not Script) but it did not fix the problem. With Firefox Android and Firefox Windows (small window) the problem is still there. Click on menu items with submenus and the navigation closes completely. Additionally, with the new CSS in all browsers, “+” and “x” are now missing to expand and collapse. Therefore, we have currently inserted the “old” CSS again.

    • Anna Kurowska