Re: Targeting Sub-menu Widths

I'm trying to make the sub menus different widths to accommodate the length of the sub-menu names. Some of them are longer than others. 

Is there a way to do this with nth:child or something similar?

I've set a width for all of the sub-menus using:

#Top_bar .menu li ul li.menu-item {
    width: 250px !important;
}

And I'm targeting the sub-menu underneath 3D Printing with this CSS:

#Top_bar .menu li ul li#menu-item-3223 {
    width: 275px !important;
}

Lastly, I'm targeting the sub-menu underneath Events using this CSS:

#Top_bar .menu li ul li#menu-item-3542, 
#Top_bar .menu li ul li#menu-item-3532 {
    width: 350px !important;
}

Is there a way to do this with nth:child or something similar?

Ref Link: Saratech.com

Comments

  • Hi,
    Sorry but I'm not sure what you want to achieve.
    But from my sight of view it's not possible, because all the structure of menu will fall and
    it will make menu work badly. I mean adding new items or just when you update your menu, all of the id's will change.

    Cheers.
  • edited August 2018
    I understand each nav link has it's own unique ID. I don't want to target by ID, I'm trying to find a way to target by class or by child element.

    I want to make the dropdown menus be different widths so that the text fits horizontally and doesn't wrap.

    I can change all of the sub-menu widths at once by using #Top_bar .menu li ul li.menu-item, but how can I change their widths individually?

    So for example, the "Software" sub-menu would be 250px wide and the "Events" sub-menu would be 350px wide, etc. without targeting the ID's?

    See this Stackoverflow post. It's what I'm trying to do.

    The answer there is below. How do I apply that to your theme?

    .navbar-nav>li>.dropdown-menu
    {

        width: yourwidth 

    }

     

    You can add display:
    inline-block so that its width will be according to its content automatically









  • This require almost recoding all the menu code.
    The size of background of li items are from ul so it will always get a width of the biggest item of structure. So it's not possible to decrease width only for one item.
    We cannot help you with it, you have to do it yourself
    or find somebody who'll be able to do it.
    Thanks

  • Again, I don't want to change the size of the individual <li> elements (size of background of li items)  I want to change the width of entire sub-menu.

    The sub-menus have a class of: <ul class="sub-menu" style="display: block;">

    How do I target the class="sub-menu" dropdown menu width? Not the width of the list items, the width of the sub-menu.

    See the images below. The two sub-menus have different widths, but I'm targeting the <li>'s. I want to target the class="sub-menu".

    <ul class="sub-menu"> is a child element of <ul id="menu-main-menu" class="menu menu-main">, so this should be possible with nth-child(4n) or something similar...?
  • So for example:
    If you want to taget the menu item 3D Printing and change its sub-menu width, use this css:
    1. ul.menu li#menu-item-195 > ul.sub-menu {
      width:200px !important;
      }
    Just change the <li> ID number so you can target different menu items.

    Cheers
  • Ahhh!! Yes, thank you Bryan. That's the syntax I was looking for. For some reason I was skipping over the ID of `#menu-item` when trying to target the sub-menu.

    Thanks very much for your help!
  • BTW, how do you format code in these comments?
  • Actually, for anyone else that may need this:

    If you change the CSS from Bryan's post above to the syntax below you can target all of the '.menu-item' in each 'ul.sub-menu' by ID number. This changes the width of the <li> link description as well as the background of the sub-menu.

    For example:

    ul.menu li#menu-item-198 > ul.sub-menu .menu-item {
      width: 650px;
    }
Sign In or Register to comment.
This website uses cookies

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you’ve provided to them or that they’ve collected from your use of their services.

Cookies are small text files that can be used by websites to make a user's experience more efficient.

The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This means that cookies which are categorized as necessary, are processed based on GDPR Art. 6 (1) (f). All other cookies, meaning those from the categories preferences and marketing, are processed based on GDPR Art. 6 (1) (a) GDPR.

This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages.

You can at any time change or withdraw your consent from the Cookie Declaration on our website.

Learn more about who we are, how you can contact us and how we process personal data in our Privacy Policy.

Please state your consent ID and date when you contact us regarding your consent.