Вопрос

I have a problem with my dropdown menu ever since I moved it from a table to a div. In the table the submenus displayed perfectly fine, however now that they are in a div, wider submenu items are being wrapped underneath the menu item that is on the far right.

Here´s the fiddle, have look at the difference between the submenu underneath item 2 and item 3. The same accounts for item 4 and item 5 (far right). How do I set the width of the div such that it will take the width of the submenu into account?

HTML:

<div id="headerimg">
<div id="header">
    <div id="headercontent">    

        <div style="float: left; padding: 25px 50px 0 0;"><a href="home"><img src="" alt=""></a></div>

        <div style="float: left; padding-top: 48px">
            <nav>
                <ul style="font-weight: bold; font-size: 18px;"><!--
                --><li style="background-image: url(images/MenuBGres.png);"><a class="MenuLinks1" href="">MENU ITEM 1</a><!--
                    --><ul><!--
                        --><li><a href="">Option 1</a></li><!--
                        --><li><a href="">Much longer option 2</a></li><!--                 --><li><a href="">Longer option 3</a></li><!--
                    --></ul><!--
                --></li><!--
                --><li style="background-image: url(images/MenuBGind.png);"><a class="MenuLinks1" href="">MENU ITEM 2</a><!--
                    --><ul><!--
                        --><li><a href="">Option 1</a></li><!--
                        --><li><a href="">Much longer option 2</a></li><!--                 --><li><a href="">Longer option 3</a></li><!--
                    --></ul><!--
                --></li><!--
                --><li style="background-image: url(images/MenuBGres.png);"><a class="MenuLinks1" href="">MENU ITEM 1</a><!--
                    --><ul><!--
                        --><li><a href="">Option 1</a></li><!--
                        --><li><a href="">Much longer option 2</a></li><!--                 --><li><a href="">Longer option 3</a></li><!--
                    --></ul><!--
                --></li><!--
            --></ul>
            </nav>
        </div>

        <div style="float: right; clear: right; margin-right: 8px;" id="language">
            <form name="languagelist">
                <select name="translate" size="1" onChange="go()">
                    <option value="main" selected>English</option>
                    <option value="es/main">Espa&ntilde;ol</option>
                </select>
            </form>
        </div>

        <a href="login"><div style="float: right; clear: right; margin-top: 24px; margin-right: 16px;" id="button">LOGIN</div></a>

        <div style="float: right; clear: right; padding-top: 10px;">
            <nav>
                <ul><!--
                --><li><a class="MenuLinks2" href="">MENU 4</a><!--
                    --><ul><!--
                        --><li><a href="">Option 1</a></li><!--
                        --><li><a href="">Much longer option 2</a></li><!--                 --><li><a href="">Longer option 3</a></li><!--
                    --></ul><!--
                --></li><!--
                --><li><a class="MenuLinks2" href="support/contactus">MENU 5</a><!--
                    --><ul><!--
                        --><li><a href="">Option 1</a></li><!--
                        --><li><a href="">Much longer option 2</a></li><!--                 --><li><a href="">Longer option 3</a></li><!--
                    --></ul><!--
                --></li><!--
            --></ul>
            </nav>
        </div>

    </div>
</div>
</div>

CSS:

nav ul {
position: relative;
list-style: none;
padding: 0;
margin: 0;
font-size: 16px; 
white-space: nowrap;
}
nav ul:after {
    content: ""; 
    clear: both; 
}
nav > ul > li {
display: inline-block;
    margin-right: 10px;
background-repeat: no-repeat; 
background-position: center top;
}
nav > ul > li > a {
display: block;
padding: 10px 5px 10px 5px;
color: #ffffff; 
text-decoration: none;
}
nav ul li:hover ul li a{
    color: #ffffff;
}

/* Set properties for hiding/unhiding dropdown items */ 
nav ul > li:hover ul, nav > ul > li a:hover ul{
    height: auto;
    max-height: 250px;
    margin-left: -10px;
    margin-top: 5px;
    padding-right: 10px;
    border: 2px solid rgb(233,255,175);
    border-top: none;
    border-radius: 0 0 10px 10px; 
    -webkit-box-shadow: 0px 3px 5px 3px rgba(100, 100, 100, 0.5);
    -moz-box-shadow:    0px 3px 5px 3px rgba(100, 100, 100, 0.5);
    box-shadow:         0px 3px 5px 3px rgba(100, 100, 100, 0.5);
}

/* Set general properties for dropdown menu items */    
nav ul ul {
position: absolute;
max-height: 0; 
overflow: hidden;
background: #018c3b;
border: 2px solid rgb(233,255,175);
border-bottom: none;
border-top: none;
border-radius: 0 0 10px 10px; 
margin-top: 5px;
margin-left: -10px;
padding-right: 10px;
-webkit-transition: max-height 0.4s ease-in;
-moz-transition: max-height 0.4s ease-in;
-o-transition: max-height 0.4s ease-in;
-ms-transition: max-height 0.4s ease-in;
transition: max-height 0.4s ease-in;
}

nav ul ul li {
border: 0;
position: relative;
display: block;
padding: 0;
width: auto;
-webkit-transition: background-color 0.4s ease;
-moz-transition: background-color 0.4s ease;
-o-transition: background-color 0.4s ease;
-ms-transition: background-color 0.4s ease;
transition: background-color 0.4s ease;
}

nav ul ul li + li a{
        border-top: 1px solid #C9CCCF;
    }
nav ul ul li a {
        display: block;
        margin-left: 8px;
        padding: 8px 5px 8px 5px;
        text-decoration: none;
        font-size: 14px;
        font-weight: normal;
        color: #ffffff;
        white-space: normal;
    }   
        nav ul li:hover ul li a:hover {
            background: #a0c94c;
            color: #ffffff;
        }
        nav ul {
position: relative;
list-style: none;
padding: 0;
margin: 0;
font-size: 16px; 
white-space: nowrap;
}
nav ul:after {
    content: ""; 
    clear: both; 
}
nav > ul > li {
display: inline-block;
    margin-right: 10px;
background-repeat: no-repeat; 
background-position: center top;
}
nav > ul > li > a {
display: block;
padding: 10px 5px 10px 5px;
color: #ffffff; 
text-decoration: none;
}
nav ul li:hover ul li a{
    color: #ffffff;
}

/* Set properties for hiding/unhiding dropdown items */ 
nav ul > li:hover ul, nav > ul > li a:hover ul{
    height: auto;
    max-height: 250px;
    margin-left: -10px;
    margin-top: 5px;
    padding-right: 10px;
    border: 2px solid rgb(233,255,175);
    border-top: none;
    border-radius: 0 0 10px 10px; 
    -webkit-box-shadow: 0px 3px 5px 3px rgba(100, 100, 100, 0.5);
    -moz-box-shadow:    0px 3px 5px 3px rgba(100, 100, 100, 0.5);
    box-shadow:         0px 3px 5px 3px rgba(100, 100, 100, 0.5);
}

/* Set general properties for dropdown menu items */    
nav ul ul {
position: absolute;
max-height: 0; 
overflow: hidden;
background: #018c3b;
border: 2px solid rgb(233,255,175);
border-bottom: none;
border-top: none;
border-radius: 0 0 10px 10px; 
margin-top: 5px;
margin-left: -10px;
padding-right: 10px;
-webkit-transition: max-height 0.4s ease-in;
-moz-transition: max-height 0.4s ease-in;
-o-transition: max-height 0.4s ease-in;
-ms-transition: max-height 0.4s ease-in;
transition: max-height 0.4s ease-in;
}

nav ul ul li {
border: 0;
position: relative;
display: block;
padding: 0;
width: auto;
-webkit-transition: background-color 0.4s ease;
-moz-transition: background-color 0.4s ease;
-o-transition: background-color 0.4s ease;
-ms-transition: background-color 0.4s ease;
transition: background-color 0.4s ease;
}

nav ul ul li + li a{
        border-top: 1px solid #C9CCCF;
    }
nav ul ul li a {
        display: block;
        margin-left: 8px;
        padding: 8px 5px 8px 5px;
        text-decoration: none;
        font-size: 14px;
        font-weight: normal;
        color: #ffffff;
        white-space: normal;
    }   
        nav ul li:hover ul li a:hover {
            background: #a0c94c;
            color: #ffffff;
        }
Это было полезно?

Решение

Found the solution. In nav ul ul li a, I simply put white-space: nowrap (changed it from normal, forgot why I put it in there in the first place...). Hope it will be useful to anyone.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top