CSS水平子菜单

时间:2010-05-18 14:33:58

标签: html css

我正在开发一个横向CSS下拉菜单。它仍然适用于IE 7,IE 8,Firefox和Chrome。但我希望顶部<ul>位于顶层(例如z-index: 100)。我想要这个,因为顶级<ul>有一个图形背景,下拉列表只是用css设置样式,并且当前它正在破坏布局。

HTML代码:

<div id="mainMenu"> 
    <ul> 
        <li><a href="t1">TOP1<!--[if gt IE 6]><!--></a><!--<![endif]-->
        <!--[if lte IE 6]><table><tr><td><![endif]--> 
            <ul> 
                <li><a href="l1">LINK1</a></li> 
                <li><a href="l2">LINK2</a></li> 
                <li><a href="l3">LINK3</a></li> 
                <li><a href="l4">LINK4</a></li> 
            </ul>
        <!--[if lte IE 6]></td></tr></table></a><![endif]--> 
        </li>
        <li class="center"><a href="t2">TOP2<!--[if gt IE 6]><!--></a><!--<![endif]-->
        <!--[if lte IE 6]><table><tr><td></td></tr></table></a><![endif]--></li>
        <li><a name="t3">TOP3<!--[if gt IE 6]><!--></a><!--<![endif]--> 
        <!--[if lte IE 6]><table><tr><td><![endif]--> 
            <ul class="last"> 
                <li><a href="l5">LINK5</a></li> 
                <li><a href="l6">LINK6</a></li>
                <li><a href="l7">LINK7</a></li>
            </ul> 
        <!--[if lte IE 6]></td></tr></table></a><![endif]--> 
        </li> 
    </ul>
</div>

CSS代码

/* style the outer div to give it width */
#mainMenu {
    position: absolute;
    margin-left: 6px;
    margin-top: 180px;
}

/* remove all the bullets, borders and padding from the default list styling */
#mainMenu ul {
    position: absolute;
    width: 494px; 
    padding: 0;
    margin: 0;
    list-style-type: none;
    background: #FFF url(../images/mainMenu_bg.gif) no-repeat;
}

/* float the list to make it horizontal and a relative positon so that you can control the dropdown menu positon */
#mainMenu li {
    position: relative;
    float: left;
    padding-left: 5px;
    width: 160px;
    vertical-align: middle;
    text-align: left;
}

#mainMenu li.center {
    padding-left: 0px;
    text-align: center;
}

/* style the links for the top level */
#mainMenu a, #mainMenu a:visited {
    display: block;
    font: bold 12px/1em Helvetica, arial, sans-serif;
    color: #FFF;
    text-decoration: none;
    height: 42px;
    line-height: 35px;
}

/* hide the sub levels and give them a positon absolute so that they take up no room */
#mainMenu ul ul {
    visibility: hidden;
    position: absolute;
    height: 0;
    top: 35px;
    left: -5px;
    width: 165px;
}

/* style the table so that it takes no part in the layout - required for IE to work */
#mainMenu table {
    position: absolute; 
    top: 0;
    left: 0;
}

/* style the second level links */
#mainMenu ul ul a, #mainMenu ul ul a:visited {
    width: 165px;
    height: 20px;
    line-height: 19px;
    font: bold 10px Helvetica, arial, sans-serif;
    background: #EF7D0E;
    color: #FFF;
    text-align: left;
    padding: 6px 0 0 5px;
    border-top: 1px solid #C1650B;
}

#mainMenu ul ul.last a, #mainMenu ul ul.last a:visited {
    width: 162px;
}

/* style the top level hover */
#mainMenu a:hover, #mainMenu ul ul a:hover{
    color: #FFF; 
    text-decoration: underline;
}

#mainMenu :hover > a, #mainMenu ul ul :hover > a {
    color: #FFF;
    text-decoration: underline;
}

/* make the second level visible when hover on first level list OR link */
#mainMenu ul li:hover ul,
#mainMenu ul a:hover ul{
    visibility: visible; 
}

我在IE 6中显示表格仍有问题,但我的主要问题是在TOP链接下显示LINK1 ... 6.

我已尝试使用z-index进行了许多设置,但此处没有任何效果。 我希望你能帮助我;)

1 个答案:

答案 0 :(得分:0)

试试这个:

div#mainMenu ul
{
  position:relative;
  z-index:100;
}