Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS drop down menu - Making children at least the width of parents

I have a three-tier CSS drop down menu that is working fine, but I'm currently specifying an actual width for the second and third tier list items. I've been fiddling around for hours trying to get it to look the way I want but with no success.

What I would like is for the items in the second tier to have a minimum width that is equal to the width of its parent, and extends past that width if the content of the item is longer than the content of its parent.

I would also like the items in the third tier to have no minimum width, but rather have a width that is just the width of the content of the longest list item for that particular nested unordered list.

My current HTML and CSS is here: http://jsfiddle.net/kBVYD/1/

If you'd prefer to just see the code, here's the HTML:

<div id="menu1">
    <ul class="menu">
        <li><a class="haschild" title="" href="">Home</a>
            <ul class="sub-menu">
                <li><a class="haschild" title="" href="">Sub Link 1</a>
                    <ul>
                        <li><a title="" href="">Sub Sub Link 1</a></li>
                        <li><a title="" href="">Sub Sub Link 2</a></li>
                        <li><a title="" href="">Sub Sub Link 3</a></li>
                    </ul>
                </li>
                <li><a title="" href="">Sub Link 2</a></li>
                <li><a class="haschild" title="" href="">Sub Link 3</a>
                    <ul>
                        <li><a title="" href="">Sub Sub Link 1</a></li>
                        <li><a title="" href="">Sub Sub Link 2</a></li>
                        <li><a title="" href="">Sub Sub Link 3</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><a class="haschild" title="" href="">About Us</a>
            <ul class="sub-menu">
                <li><a title="" href="">Sub Link 1</a></li>
                <li><a title="" href="">Sub Link 2</a></li>
                <li><a class="haschild" title="" href="">Sub Link 3</a>
                    <ul>
                        <li><a title="" href="">Sub Sub Link 1</a></li>
                        <li><a title="" href="">Sub Sub Link 2</a></li>
                        <li><a title="" href="">Sub Sub Link 3</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><a class="haschild" title="" href="">Our Services Etc Etc Etc</a>
            <ul class="sub-menu">
                <li><a title="" href="">Sub Link 1</a></li>
                <li><a class="haschild" title="" href="">Sub Link 2</a>
                    <ul>
                        <li><a title="" href="">Sub Sub Link 1</a></li>
                        <li><a title="" href="">Sub Sub Link 2</a></li>
                        <li><a title="" href="">Sub Sub Link 3</a></li>
                    </ul>
                </li>
                <li><a class="haschild" title="" href="">Sub Link 3</a>
                    <ul>
                        <li><a title="" href="">Sub Sub Link 1</a></li>
                        <li><a title="" href="">Sub Sub Link 2</a></li>
                        <li><a title="" href="">Sub Sub Link 3</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    <div class="clear"></div>
</div>

And here's the CSS:

#menu1 *
{
    margin: 0;
    padding: 0;
}

#menu1 ul.menu
{
    float: left;
    font-family: "Arial", sans-serif;
    font-size: 14px;
    font-weight: bold;
    text-transform: uppercase;
}

#menu1 ul.menu li
{
    position: relative;
    float: left;
    list-style-type: none;
}

#menu1 ul.menu li a
{
    position: relative;
    display: block;
    line-height: 21px;
    font-size: 14px;
    padding: 14px 21px;
    text-decoration: none;
    z-index: 100;
}

#menu1 ul.menu > li:first-child > a
{
    border-left: 0;
}

#menu1 ul.menu > li:last-child > a
{
    border-right: 0;
}

/** Sub Menu - Tier 2 **/

#menu1 ul.menu li ul.sub-menu
{
    position: relative;
    display: none;
    margin: 0;
    padding: 0;
}

#menu1 ul.menu li:hover ul.sub-menu
{
    display: block;
    float: left;
    position: absolute;
    z-index: 200;
}

#menu1 ul.menu li ul.sub-menu > li:first-child
{
    margin: 5px 0 0 0;
}

#menu1 ul.menu li ul.sub-menu li a
{
    width: 140px;
    display: block;
}

#menu1 ul.menu li ul.sub-menu li:first-child > a
{
    border-top: 0;
}

#menu1 ul.menu li ul.sub-menu li:last-child > a
{
    border-bottom: 0;
}

/** Sub Menu - Tier 3 **/

#menu1 ul.menu li ul.sub-menu li ul
{
    position: relative;
    display: none;
    left: 100%;
}

#menu1 ul.menu li ul.sub-menu li ul li
{
    margin: 0 0 0 5px;
}

#menu1 ul.menu li ul.sub-menu li:hover ul
{
    display: block;
    float: left;
    position: absolute;
    top: 0;
}

#menu1 ul.menu li ul.sub-menu li ul li a
{
    width: 140px;
    display: block;
}


/** Colour Styles **/

#menu1 ul.menu li a
{
    background: #09F;
    color: #FFF;
}

#menu1 ul.menu > li > a
{
    border-left: 1px solid #26A8FF;
    border-right: 1px solid #0082D9;
}

#menu1 ul.menu li:hover > a, #menu1 ul.menu li a:hover
{
    color: #09F;
    background: #ddd;
}

#menu1 ul.menu li ul.sub-menu li a
{
    border-top: 1px solid #26A8FF;
    border-bottom: 1px solid #0082D9;
}
like image 287
Jordan Avatar asked Feb 19 '23 05:02

Jordan


1 Answers

Delete the custom width you added:

#menu1 ul.menu li ul.sub-menu li a {
    width: 140px;
}

The problem now is that the ul.sub-menu's dimension are based on it's relative parent <li>. The trick here is to use white-space: nowrap to push it out of it's parent's dimensions. Then use min-width to set the minimum width allowed (or as you stated "at least the width of parents").

#menu1 ul.menu li ul.sub-menu {
    min-width: 100%;
    white-space: nowrap;
}
like image 123
John Smith Avatar answered Feb 27 '23 09:02

John Smith