CSS Drop-down Sub-menu Erroneously Modifying Parent Width

I have been fiddling with CSS-based drop-down navigation on my website and ran into some trouble with the sub-menus modifying the width of the parent menus. When a sub-drop-down is opened, the parent drop-down will enlarge to match the width. If sub-drop-down menus are smaller than the parent, they appear to be using the width of the parent menus for their horizontal offset instead of their own width, which creates a gap.

I have pulled together a live example here: http://jsfiddle.net/vwLf9f3w/2/

Sorry for the large quantity of CSS; I have pulled the relevant code to the top.

Here is the CSS just for my navigation:

/*    ----------- Navigation -----------    */

div.nav 
{
    display: block;
    position: absolute;
    top: 83px; 
    left: 470px;
    height: 47px; 
    width: 530px;
    background-color: transparent;
    z-index: 21;
    padding: 0px;
    margin: 0;
    font-size: 16px;
}

/*      ------    [ NAV LINKS ]    -------     */

/* Primary header links */
div.nav > ul > li > a
{
    display: block;
    background-color: transparent;
    text-align: center;

    height: 24px;
    width: 96px;
    background-color: #a9a9a9;
    border: 1px solid #666666;
    border-radius: 3px;
    margin-top: 6px;
    margin-bottom: 6px;
    margin-left: 6px;
    padding: 6px 0px 0px 0px;
}
div.nav > ul > li:hover > a
{
    background-color: #8bbbdd;
}

/* All header links */
div.nav a
{
    color: #000000; 
    text-decoration: none;
}

/* Header Sub-links */
div.nav > ul > li li > a 
{
    display: block; 
    padding: 0px 0px;

    padding-top:    6px;
    padding-bottom: 6px;
    padding-left:   20px;
    padding-right:  20px;
    margin-top:  0px;
    margin-left: 0px;
    background:  #a9a9a9;
    color:       #000000;
}
div.nav > ul > li li > a:hover, div.nav > ul ul li:hover > a
{
    background: #8bbbdd;
}

/*      ------    [ NAV LISTS ]    -------     */

/* Primary navigation items */
div.nav > ul > li 
{
    display: inline-block;
    background-color: transparent;
    vertical-align: top; /* here */
    max-width: 104px;
    overflow: show;
}

/* Primary navigation list */
div.nav > ul
{
    display: inline-block;
    width: 650px;
    margin: 0px;
}

/* Secondary navigation lists */
div.nav > ul ul
{
    z-index: 0;
    white-space:nowrap;

    border: 1px solid #666666;
    border-radius: 3px;

    padding: 0 0 0 0;
    list-style: none;
    position: relative;

    display: none;
    background-color: transparent;

    background: URL(images/blank.gif);/*#000;transparent; IE FIX*/
    padding: 0;
    left: 6px;
    top: -3px;
    position: relative; 
    margin-bottom: 0px;
    margin-left: 0px;
    width: auto;
    height: auto;
}
div.nav ul li:hover > ul 
{
    display: inline-block;
}

/* Secondary list items */
div.nav > ul > li li
{
    max-height: 31px;
}

/* Tertiary menu list */
div.nav > ul ul ul
{
    top: -36px;
    left: -100%;
    position: relative;
}

And here is the relevant HTML:

...
<div class="nav">
            <ul>
                <li><a href="?page=">Home</a>
                </li><li><a href="javascript:void(0);">Tech</a>
                    <ul>
                        <li><a href="#">Subject 1</a></li>
                        <li><a href="#">Subject 2</a>
                            <ul>
                                <li><a href="#">An Item</a></li>
                                <li><a href="#">Some Other Item</a></li>
                                <li><a href="#">And Yet Another!</a></li>
                                <li><a href="#">I Am Running Out of Ideas</a>
                                    <ul>
                                        <li><a href="#">Even More To Show Expansion</a></li>
                                        <li><a href="#">And More</a></li>
                                        <li><a href="#">And More!</a></li>
                                    </ul>
                                </li>
                                <li><a href="#">One Last Item</a></li>
                            </ul>
                        </li>
                        <li><a href="#">Subject 3</a></li>    
                        <li><a href="#">Subject 4</a></li>
                    </ul>
                </li><li><a href="javascript:void(0);">Personal</a>
                    <ul>
                        <li><a href="#">Subject 1</a>
                            <ul>
                                <li><a href="#">1111</a></li>
                                <li><a href="#">2222</a></li>
                                <li><a href="#">3333</a></li>
                                <li><a href="#">4444</a>
                                    <ul>
                                        <li><a href="#">1</a></li>
                                        <li><a href="#">2</a></li>
                                        <li><a href="#">3</a></li>
                                    </ul>
                                </li>
                                <li><a href="#">5555</a></li>
                            </ul>
                        </li>
                        <li><a href="#">Subject 2</a></li>
                        <li><a href="#">Subject 3</a></li>
                    </ul>
                </li><li><a href="javascript:void(0);">Software</a>
                    <ul>
                        <li><a href="#">Subject 1</a></li>
                        <li><a href="#">Subject 2</a></li>
                        <li><a href="#">Subject 3</a></li>
                    </ul>
                </li><li><a href="javascript:void(0);">Contact</a>
                    <ul>
                        <li><a href="#">Subject 1</a></li>
                        <li><a href="#">Subject 2</a></li>
                        <li><a href="#">Subject 3</a></li>
                    </ul>
                </li>
            </ul>
        </div>
...

To summarize: How can I get each navigation sub-menu to have its own size that will not modify the size of other menus? And, how can I get the sub-menus to reference their own width rather than their parents’ to prevent gaps?

All suggestions are welcome; my apologies for the unpolished code.


Source: css3

Leave a Reply