How To Collapse Submenus In Divi

by | Jan 5, 2022 | Divi Tutorials

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.

Before

After

For doing this you have to just add CSS & javascript to your Divi website.

CSS

Navigate to Theme Options and scroll down to bottom and add this code to Custom 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

Navigate to Theme Options > Integration > Add code to the < body >.

<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>

The Bottom Line

We hope that this method works for you. Comment down your review about this blog.

License: This snippet contains code from the Divi Theme, copyright https://elegantthemes.com.

Latest Posts

Overview of the WordPress Interactivity API

Overview of the WordPress Interactivity API

The WordPress Interactivity API is a groundbreaking feature introduced in WordPress 6.5, designed to simplify and standardize the process of adding interactive elements to WordPress websites. This comprehensive guide will walk you through what the Interactivity API...

Power Of Gutenberg And Its Plug-ins For Enhanced Content Creation

Power Of Gutenberg And Its Plug-ins For Enhanced Content Creation

In the ever changing landscape of website development, there are several frameworks and runtime environments available for creating and testing websites. PHP, HTML, Node.js etc. WordPress is a popular CMS that has built an ecosystem around it to make web development...

How Do I Earn Money From Reseller Hosting? The Major Benefits

How Do I Earn Money From Reseller Hosting? The Major Benefits

Do you have the dream of building your own web hosting company? Of course, you might have. However, you may have concerns about building your web hosting company from scratch. Thus, reseller hosting services come into existence. In reseller hosting, you are able to...

Dedicated Hosting: The Key To Optimal Performance And Scalability

Dedicated Hosting: The Key To Optimal Performance And Scalability

In the web hosting world, dedicated hosting emerges as an alternative, offering customers an isolated, robust, and scalable hosting environment. As business owners and individuals are looking to enhance their online visibility and ensure the seamless working of their...

Contributor Day Registration For WordCamp Asia 2024

Contributor Day Registration For WordCamp Asia 2024

Greetings WordPress enthusiasts! As we gear up for the highly anticipated WordCamp Asia 2024, we are thrilled to share exciting news about a pivotal event that promises to elevate your WordPress experience – Contributor Day. Mark your calendars for March 7, 2024, at...

Our Founder Mayank is speaking at WordCamp Udaipur 2023

Our Founder Mayank is speaking at WordCamp Udaipur 2023

Who is Mayank? Mayank Kumar has been a WordPress enthusiast since he first used WordPress in 2020 when he was just 17 years old. Mayank is a 20-year-old Computer Science Geek who loves WordPress so much. He is been running a Web Agency and two Ed-tech...

How To Change Twitter Icon To X in Divi Theme

How To Change Twitter Icon To X in Divi Theme

As everyone knows, Twitter has changed its name to X. Websites and tools like Divi will definitely need some time to catch up, so I wanted to provide you with some quick instructions now so that those who wish to make changes can do so before it gets updated later in...

WordPress Dashboard – Comments

WordPress Dashboard – Comments

In the Comments Screen, you can edit or delete content found in comments to your posts, and you can mark comments as spam, send comments to the trash and reply to comments. A number of rules can be defined about who can write comments and how comments are...

WordPress Dashboard : Pages

WordPress Dashboard : Pages

The Pages Screen provides the facility to manage all the Pages in a blog. Via this Screen, Pages can be edited, deleted and viewed. Filtering and searching also make it easy to quickly find pages matching certain criteria. Several powerful features allow pages to be...

WordPress Dashboard : Media

WordPress Dashboard : Media

Media → Library Media consists of the images, video, recordings and files that you upload and use in your blog. Media is typically uploaded and inserted into the content when writing a Post or writing a Page. Note that the Uploading Setting in the Settings Media...

Latest Posts