CSS子菜单以父母为中心

时间:2013-11-22 14:48:12

标签: html css html5 css3

我试图将子菜单放在我的父列表元素下面,它在某种程度上起作用,但我觉得我的代码过于复杂,试图使其工作。在我将这个应用到每个元素的子菜单的网站上,每个菜单项向左推进一点,即。在菜单结尾(最右边),子菜单被非常明显地推到左边

我创建了以下jsFiddle:http://jsfiddle.net/daveyates/jCT7C/

HTML:

 <ul id="menu">
    <li class="active arrow-up" >
        <a href="/admin/">Menu Header 1</a>

        <ul>
            <li><a href=""></a>Sub Menu 1</li>
            <li><a href=""></a>Sub Menu 2</li>
            <li><a href=""></a>Sub Menu 3</li>
        </ul>
    </li>
     <li class="active arrow-up" >
        <a href="/admin/">Menu Header 2</a>

        <ul>
            <li><a href=""></a>Sub Menu 1</li>
            <li><a href=""></a>Sub Menu 2</li>
            <li><a href=""></a>Sub Menu 3</li>
        </ul>
    </li>
     <li class="active arrow-up" >
        <a href="/admin/">Menu Header 3</a>

        <ul>
            <li><a href=""></a>Sub Menu 1</li>
            <li><a href=""></a>Sub Menu 2</li>
            <li><a href=""></a>Sub Menu 3</li>
        </ul>
    </li>
     <li class="active arrow-up" >
        <a href="/admin/">Menu Header 4</a>

        <ul>
            <li><a href=""></a>Sub Menu 1</li>
            <li><a href=""></a>Sub Menu 2</li>
            <li><a href=""></a>Sub Menu 3</li>
        </ul>
    </li>
     <li class="active arrow-up" >
        <a href="/admin/">Menu Header 5</a>

        <ul>
            <li><a href=""></a>Sub Menu 1</li>
            <li><a href=""></a>Sub Menu 2</li>
            <li><a href=""></a>Sub Menu 3</li>
        </ul>
    </li>
 </ul>

CSS:

#menu, #menu ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

#menu {
    height:auto;
}

/*clearing......*/

#menu:before,
#menu:after {
    content: "";
    display: table;
}

#menu:after {
    clear: both;
}

#menu {
    zoom:1;
}
#menu li {
    float: left;
    position: relative;
}

#menu a {
    float: left;
    padding:17px 15px;
    color: #A1AAB3;
    font-family: ProximaNova, sans-serif;
    font-size:10px;
    text-decoration: none;
}
#menu a.active{
    color:#fafafa;


}
#menu li:hover > a {
    color: #fafafa;
}

*html #menu li a:hover { /* IE6 only */
}


#menu ul {
    margin: 20px 0 0 0;
    _margin: 0; /*IE6 only*/
    opacity: 0;
    visibility: hidden;
    position: absolute;
    top: 40px;
    left: -28px;
    z-index: 1;    
    background: #354351;    
    transition: all .2s ease-in-out;  
}

#menu li:hover > ul {
    opacity: 1;
    visibility: visible;
    margin: 0;
}

#menu ul ul {
    top: 0;
    left: 150px;
    margin: 0 0 0 0;
    _margin: 0; /*IE6 only*/
}

#menu ul li {
    float: none;
    display: block;
    border: 0;
    _line-height: 0; /*IE6 only*/
}

#menu ul li:last-child {   
    box-shadow: none;    
}

#menu ul a {    
    padding: 10px;
    width: auto;
    _height: 10px; /*IE6 only*/
    display: block;
    white-space: nowrap;
    float: none;
    text-transform: none;
}

#menu ul a:hover {
}


#menu ul li:first-child > a {
    border-radius: 3px 3px 0 0;
}

#menu ul ul li:first-child a:after {
    left: -6px;
    top: 50%;
    margin-top: -6px;
    border-left: 0; 
    border-bottom: 6px solid transparent;
    border-top: 6px solid transparent;
    border-right: 6px solid #3b3b3b;
}

#menu ul li:first-child a:hover:after {
    border-bottom-color: #04acec; 
}

#menu ul ul li:first-child a:hover:after {
    border-right-color: #0299d3; 
    border-bottom-color: transparent;   
}

#menu ul li:last-child > a {
    border-radius: 0 0 3px 3px;
}

2 个答案:

答案 0 :(得分:2)

您可以在此处执行的操作是为您的left修改属性top width#menu ul

#menu ul {
  position: absolute;
  top:100%;
  left:0;
  width:100%;
}

演示 http://jsfiddle.net/jCT7C/7/

答案 1 :(得分:0)

在{c}中#menu ulleft: -28px;更改为left: 28px; ....应该这样做! :)