将下拉菜单与中心对齐

时间:2014-05-16 19:00:09

标签: html css

我的下拉菜单位置搞砸了。子元素未与父元素的左侧对齐。我希望子元素的中心等于我的父元素的中心。

http://jsfiddle.net/H9L4G/

例如,对于测试1,我希望将其子元素作为测试1的中心。

<html>
<head>
<link href="test.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="navBar">
    <ul id="nav">
        <table width="100%">
            <tr>
                <td>            
                    <li>
                        <a href="#">TEST</a>
                        <ul>                            
                            <li><a href="#"><table width="100%" ><tr> 
                                <td width = "10%" align="center">1</td> 
                                <td width = "60%" align="center">2</td> 
                                <td width = "30%" align="center">3</td> 
                            </tr></table></a></li>

                        </ul>
                    </li>
                </td>

                <td>            
                    <li>
                        <a href="#">TEST2</a>
                        <ul>

                            <li><a href="#"><table width = "100%"><tr> 
                                <td width = "10%" align="center">1</td> 
                                <td width = "60%" align="center">2</td> 
                                <td width = "30%" align="center">3</td> 
                            </tr></table></a></li>

                            <li><a href="#"><table width = "100%"><tr>
                                 <td width = "10%" align="center">4</td> 
                                 <td width = "60%" align="center">5</td> 
                                 <td width = "30%" align="center">6</td> 
                            </tr></table></a></li>

                            <li><a href="#"><table width = "100%"><tr> 
                                <td width = "10%" align="center">7</td> 
                                <td width = "60%" align="center">8</td> 
                                <td width = "30%" align="center">9</td> 
                            </tr></table></a></li>

                        </ul>
                    </li>
                </td>

                <td>            
                    <li>
                        <a href="#">TEST3</a>
                    </li>
                </td>

            </tr>
        </table>
    </ul>   
</div><!--End of Navigation-->
</body>
</html>

#navBar {
    width:100%
    height: 100%
}

/*------------------------------------*\
    NAV
\*------------------------------------*/
#nav{
    list-style:none;
    font-weight:bold;
    margin-bottom:10 px;
    /* Clear floats */
    width:100%;
    height:100%;
    /* Bring the nav above everything else--uncomment if needed.
    position:relative;
    z-index:5;
    */
    margin: 0 0 0 0;
    padding: 0;
    background-color: #f2f2f2;
    border-bottom: 1px solid #ccc; 
}
#nav li{
    font-size:1em;
    margin-right:10px;
    position:relative;
    width:100%;
}
#nav a{
    text-align: center;
    alignment-adjust: left;
    display:block;
    padding:2em;
    color:#fff;
    background:#333;
    text-decoration:none;
}
#nav a:hover{
    color:#fff;
    background:#6b0c36;
    text-decoration:underline;
}

/*--- DROPDOWN ---*/
#nav ul{
    background:#fff; /* Adding a background makes the dropdown work properly in IE7+. Make this as close to your page's background as possible (i.e. white page == white background). */
    background:rgba(255,255,255,0); /* But! Let's make the background fully transparent where we can, we don't actually want to see it if we can help it... */
    list-style:none;
    position:absolute;
    left:-9999px; /* Hide off-screen when not needed (this is more accessible than display:none;) */
    width:100%;

}
#nav ul li{
    width: 100%;
    padding-top:1px; /* Introducing a padding between the li and the a give the illusion spaced items */
    float:none;
}
#nav ul a{

    width: 100%;
    white-space:nowrap; /* Stop text wrapping and creating multi-line dropdown items */
}
#nav li:hover ul{ /* Display the dropdown on hover */
    border-left:0px;
    left: 0; /* Bring back on-screen when needed */
}
#nav li:hover a{ /* These create persistent hover states, meaning the top-most link stays 'hovered' even when your cursor has moved down the list. */
    background:#6b0c36;
    text-decoration:underline;
}
#nav li:hover ul a{ /* The persistent hover state does however create a global style for links even before they're hovered. Here we undo these effects. */
    text-decoration:none;
}
#nav li:hover ul li a:hover{ /* Here we define the most explicit hover states--what happens when you hover each individual link. */
    background:#333;
}

1 个答案:

答案 0 :(得分:0)

左推是来自ul填充。只需将padding: 0添加到内部<ul>即可。宽度也由锚的width: 100%扩展。

http://jsfiddle.net/H9L4G/1/