Subscribe On YouTube

Join other subscribers and enjoy other Divi video tutorials!

How To Set An Auto-Adjusting Divi Menu Dropdown Width

Nelson Miller Profile Orange
In this tutorial I will show you how to set an auto-adjusting Divi dropdown menu width for the Divi menu module.

▶️ Please watch the video above to get all the exciting details! 👆

Add The CSS Snippet To Your Site

This is a very simple tutorial with only one step. You simply need to copy and paste the CSS snippet below into your website. The video above will give more details, but in general this snippet is targeting the menu dropdown container, the list items in the dropdown menu, and the links themselves in the list items. All three of these are necessary to target to get this to work properly, since by default they have a fixed value assigned to them. The code snippet overrides that fixed default value and makes it flexible instead to fix the content of your menu item text.

Where To Paste The CSS Code

1. Divi Assistant
If you are using our Divi Assistant plugin, simply paste the code in the CSS tab in the custom code window in the Divi Visual Builder.

2. Child Theme
If you are using a child theme, paste this code into the style.css file. If you don't have a child theme, you can generate a child theme directly on your site or download our free child theme.

3. Divi Theme Options Integration
Otherwise, paste this code in your Divi>Theme Options>Custom CSS code box.

If you need help understanding where to paste the code, please check out our complete guide about where to add custom code In Divi.

/*set the Divi menu dropdown auto width*/
@media only screen and (min-width: 981px) {
	.nav li ul {
		width: fit-content;
		display: flex;
		flex-direction: column;
	}

	.nav li li {
		white-space: nowrap;
	}

	.nav li li a {
		width: auto !important;
	}
}

The code is placed inside a media query, just to be sure to only target the desktop dropdown menu. We kept the selector generic, so there are no custom classes required – it simply uses the classes built into Divi. The code is pretty simple in what it is doing. It tells the submenu width to fit the content, which is the key here. The content in this case is the list items and list item links, which also have an auto width. So it all works together to create a flexible width dropdown menu in Divi.

Subscribe For More Things Like This!

At the start of each month, we send out a recap newsletter from the month before with family news, Divi news, our latest tutorials, and product news. Occasionally, if the news is too exciting to wait, we will send out another email separate from the monthly newsletter. That’s what you get when you subscribe, and of course you can unsubscribe if you are no longer interested!

Blog Post Optin
Divi Logo (2)

Shop Our Divi Products

Plugins • Courses • Templates

Visit The Shop

Featured Products

Asset 4

New! Trail Guides

Follow a series of blog posts carefully arranged around a specific topic or goal! Keep track of your progress by marking posts completed, just like a free course!

View Trial Guides

Divi Tutorials On YouTube

Our videos have views! Join subscribers and enjoy over video tutorials!

Visit Our Channel

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

43 Comments

Comments By Members

These comments are highlighted because they were posted by fans who have a membership on this site.

      • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        Hi Sean!

        Can you please try this code and see if it helps?

        .et-menu li:not(.mega-menu) ul, #top-menu li:not(.mega-menu) ul,#top-menu li li, .et-menu li li {
        width: fit-content !important;
        }

      • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        Hi Everyone!

        I have updated the code above. Please use the new one and see if it helps!

      • Dax Davis <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        It looks like his issue is the same as mine. When I look at his site, I see his menu items on one line extending beyond the dropdown’s bounding colors. I’m on a Mac with Chrome—the latest versions of both.

      • Ralf krieger <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        Hello Nelson and a happy new year to you and your family!
        Its funny… I deleted all caches (at wp-rocket, and even on the hosting area at siteground… The backend shows the dropdown background beeing to short for long menu items. BUT on the frontend (live site) the css doesńt seem to work??? No idea what the reason is? Can I send you a login for my website?

  1. Dax Davis <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

    Thanks for this; I’ve been dealing with the issue for a while.

    One issue for me. The white background on my dropdown does not extend with the content. So, I have the end of the text of longer menu items floating over a transparent background.

    Can you help me with this issue?

    Reply
      • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        I’m glad the issue is resolved. Thanks for sharing the solution.

      • Dax Davis <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        I was able to fix it myself.

        I had this CSS block that was overriding things.

        #top-menu li li a {
        padding: 6px 20px;
        min-width: 200px;
        }

        I modified your CSS to this:

        .nav li li a,
        #top-menu li li a {
        width: auto;
        }

        Now I just need to figure out the button padding.

      • Dax Davis <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

        Sure, check out https://hsmna.rubicontv.com/. There are menu items on each dropdown. On the dropdown for Membership, I’ve made one of the menu items a button, and I’ve noticed it does not have padding on the right. It’s probably the same issue as the others, but I’m unsure.

      • reed

        I also had the same issue, but if i copy/paste the code directly into the “advanced > css” inside the module itself. it fixes it. Is there a big reason why we should not do this? I tried both options #2 and #3 (child theme + theme options) but the same result occurred.

Comments By Others

  1. Caco Martín

    Finally a great solution! Thanks!

    Reply
  2. Shaun Wolfson

    Thanks for the CSS. It all worked to change the menu width and background color but the dropdown under LENDING RESOURCES is so narrow that it looks funky. Is it possible to make that menu wider or have a mini width on the dropdown? Here is a link to the page – https://fairviewstage.wpengine.com/about-us/

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Shaun!

      Please change the display from flex to block in ul of the container.

      Reply
  3. Jan

    Hi Nelson,

    The initial issue is solved, but I now have the following problem. There is a small gap between the main menu and sub-menu dropdown list/menu, which the mouse can’t jump. The result, I can’t press any sub-menu button. 😅
    Any suggestions?

    Reply
  4. Laurie Debattista

    Sorry if this was mentioned already and I missed it! This works for the sub menu but the default arrow icon for the dropdown is not moving and is appearing halfway through my menu item. Also, the sub sub menu overlaps the sub menu…if that makes sense. I don’t have a link because the website is in maintenance mode.

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Laurie!

      It seems some other CSS is overlapping with ours. Do you have a staging site to share?

      Reply
      • Laurie Debattista

        It ended up sorting itself out? Hahaha Not sure how but it’s working fine now. Thanks!

  5. Jess

    Hi there, Thank you for this code it worked on my website. What Would I change to ensure it does the same for the mobile menu? I having an issue where the mobile menu is very skinny on the side.
    Thank you

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Jess!

      The above code is for desktop drowdown menu. However, I can provide you the solution for the mobile dropdown. Please check the page URL with me.

      Reply
  6. Robert

    This isn’t working on my phone. It works great on my desktop.

    Reply
  7. MDS

    Great thank you! How do we adjust the spacing between each dropdown menu item (for me there is just too much space 😉

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi there!

      Please use the following code and change the padding value as per your requirement:

      .nav li li:not(:last-child){
      padding-bottom: 5px !important;
      }

      Reply
  8. Matt Hollingsworth

    Thanks Nelson. This is great.

    Do you know how to have this ONLY affect the dropdown for the primary menu? On my site, I have a “buy it now” button with a dropdown menu, and this code is also affecting that.

    My site’s in progress, but if you look at the link, you’ll see “comics galleries” at the top of the page, which is in the Primary menu. I want this code to work only for that, but can’t figure it out.

    If you scroll down just a little, you’ll see a “buy it now” with a nested dropdown, and this code is also affecting that and conflicting with some sizing of image icons.

    https://matthollingsworth2.mystagingwebsite.com/seven-to-eternity/

    Your tutorials are great. Thanks!

    Reply
    • Nelson Lee Miller (aka The Divi Teacher) <span class="comment-author-role-label author-label">Author</span>

      Hi Matt,
      Sure with any tutorial, you can make it specific by using a custom CSS class. In this case, you can place the CSS class in the WordPress menu item (you may need to enable that in the “Screen Options” first) and then use that same selector in the code.

      Reply
      • Matt Hollingsworth

        I found a solution. If I input the code below, it makes the text stay on one line for the dropdown, and it no longer conflicts and causes sizing problems with the “buy it now” dropdown on the page.

        .nav a{
        white-space: nowrap;
        }

        I checked it on mobile as well, and it looks good with no change on the mobile that I can see.

        Anyway, thanks for the great tutorials, Nelson.

      • Matt Hollingsworth

        Thanks, Nelson.

        I’m having a helluva time with this. I tried assigning custom css classes to both the dropdown in my primary menu, and that “buy it now” menu, but can’t get it to work.

        I tried changing the .nav to the new custom css class with no effect. It seems I’m not understanding correctly after doing a ton of reading and trying things out.

        So, the custom css I put in the dropdown up in the primary is “comics-galleries-menu.” And, for instance, in your code, I changed .nav to match that, like so:

        @media only screen and (min-width: 981px) {
        .comics-galleries-menu li ul {
        width: fit-content;
        display: flex;
        flex-direction: column;
        }

        .comics-galleries-menu li li {
        white-space: nowrap;
        }

        .comics-galleries-menu li li a {
        width: auto !important;
        }
        }

        This has no effect on the menu at all. Conversely, if I put the custom css class for the “buy it now” menu, it DOES affect the “buy it now” menu.

        When I inspect the menu item up in the primary menu (the one that now has the custom css class of “.comics-galleries-menu”), and I scroll up a little, I see:

        I’m a total noob, so I’m sorry if this all sounds super ignorant. But it looks like that’s a navigational css class, and that your code is using that “.nav” to implement this. Maybe that .nav is a css class for the contrainer holding that menu, and all menu items? When I inspect my “buy it now” menu, I don’t see any nav id, so I guess it uses the default navigational css class? I couldn’t find any way to add a custom nav id or nav class, so I’m probably totally wrong here.

        I’m sorry to bother you, but I’ve been fiddling with this for hours with no luck. Any insights on how to go about implementing this only in that comics-galleries-menu, and NOT the buy-it-now-menu would be greatly appreciated, even if it’s a “go read this site, and it explains .nav” or whatever.

        Thanks!

  9. Lilah

    I’m having the same issues as the other commenters where the background width of fit-content isn’t being recognized. There’s a 240px width with an important tag overwriting it for me. Any ideas on how to fix it?

    Reply
    • Scott

      Lilah,
      Try the visibility solution in the column that the menu module is in within the row….so open the settings on the row and then settings on the column in the row that contains the menu.

      Reply
    • Nelson Lee Miller (aka The Divi Teacher) <span class="comment-author-role-label author-label">Author</span>

      Hi Lilah,
      I am not sure what you mean, as the only other commenter with this issue had CSS overriding it, which is exactly what it sounds like if you are seeing 240px with important, that is not from Divi or us, so you would have to remove that other code from your custom CSS.

      Reply
  10. Gui

    This CSS will break mega-menus. Here’s a fix for the mega-menus :

    “`/*set the Divi menu dropdown auto width*/
    @media only screen and (min-width: 981px) {
    .nav>li:not(.mega-menu) ul {
    width: fit-content;
    display: flex;
    flex-direction: column;
    }

    .nav>li:not(.mega-menu) li {
    white-space: nowrap;
    }

    .nav>li:not(.mega-menu) li a {
    width: auto !important;
    }
    }“`

    Reply
  11. Neil Forrester

    Hi Nelson, Thanks for another great tutorial. I have been approaching this using specific widths but I’ll be using this more flexible approach in future. Great job and thank you 🙂

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

0

Your Cart