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

The Hidden Role Of Dedicated Servers In Your Favorite Apps & Websites

The Hidden Role Of Dedicated Servers In Your Favorite Apps & Websites

Whether you are watching a video, shopping, or talking with friends on social media, there’s a powerful engine working behind the scenes that helps reach the content on your browsers via the internet. Most probably, it’s the strength of the dedicated servers (Windows...

Hosting-Specific Landing Pages: Do They Convert Better?

Hosting-Specific Landing Pages: Do They Convert Better?

The web hosting industry is extremely competitive. Whether it is shared low-price hosting, VPS, or dedicated servers, having the best infrastructure won't guarantee success. The business still needs to encourage visitors to sign up. One tool that can greatly enhance...

Hostinger vs Bluehost: Our Feedback after using for 6 months

Hostinger vs Bluehost: Our Feedback after using for 6 months

Bluehost and Hostinger entered the hosting industry around the same time—Bluehost in 2003 and Hostinger following closely in 2004. These two web hosting companies have evolved considerably over the last two decades. They provide easy usability, and offer free domain...

Step-by-Step Guide to Creating an ADA-Compliant WordPress Site

Step-by-Step Guide to Creating an ADA-Compliant WordPress Site

What is ADA Compliance & Why It Matters ADA-Compliant WordPress Site Imagine visiting a website and not being able to read the text or use the navigation menu. That’s what millions of people with disabilities experience every day. ADA compliance is about making...

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

Mayank is the Founder and CEO of Web Wallah. Mayank is a Computer Science Geek, deeply passionate about WordPress, actively contributing to the community, and believing in the potential of open-source tech to create a better world.

Latest Posts