如何在响应式CSS菜单上添加子菜单

cha*_*lie 7 html css menu

我的css菜单有这个html:

<nav class="clearfix">
    <ul class="clearix">
        <li><a href="http://www.domain.co.uk/">Homepage</a></li>
        <li><a href="/services">Services</a></li>
        <li><a href="/project-gallery">Project Gallery</a></li>
        <li><a href="/contact-us">Contact Us</a></li>  
    </ul>
    <a href="#" id="pull">Menu</a>  
</nav>
Run Code Online (Sandbox Code Playgroud)

 

nav {
    height: 50px;  
    width: 100%;  
    background: #F00;  
    font-size: 14pt;  
    font-family: Arial;
    position: relative;  
    border-bottom: 5px solid #FFFFFF;  
}  
nav ul {  
    padding: 0;  
    margin: 0 auto;  
    width: 100%;  
    height: 50px;
    text-align: center;
}
nav li {  
    display: inline;
}  
.clearfix:before,  
.clearfix:after {  
    content: " ";  
    display: table;  
}  
.clearfix:after {  
    clear: both;  
}  
.clearfix {  
    *zoom: 1;  
}  
nav a {  
    color: #FFFFFF;  
    display: inline-block;  
    width: auto;

    text-align: center;  
    text-decoration: none;  
    line-height: 50px;  
}  
nav li a {
    box-sizing:border-box;  
    -moz-box-sizing:border-box;  
    -webkit-box-sizing:border-box;
    padding-left: 10px;
    padding-right: 10px;
}  
nav li:last-child a {  
    border-right: 0;  
}  
nav a:hover, nav a:active {  
    background-color: #000000;
    color:#FFFFFF;  
} 
nav a#pull {  
    display: none;  
}  
@media screen and (max-width: 600px) {  
    nav {   
        height: auto;  
    }  
    nav ul {  
        width: 100%;  
        display: block;  
        height: auto;  
    }  
    nav li {  
        width: 50%;  
        float: left;  
        position: relative;  
    }  
    nav li a {  
        border-bottom: 1px solid #FFFFFF;  
        border-right: 1px solid #FFFFFF;  
    }  
    nav a {  
        text-align: left;  
        width: 100%;  
        text-indent: 25px;  
    }  
}  
@media only screen and (max-width : 480px) {  
    nav {  
        border-bottom: 0;  
    }  
    nav ul {  
        display: none;  
        height: auto;  
    }  
    nav a#pull {  
        display: block;
        color:#FFFFFF;
        background-color: #F00;  
        width: 100%;  
        position: relative;  
    }  
    nav a#pull:after {  
        content:"";  
        background: url('nav-icon.png') no-repeat;  
        width: 30px;  
        height: 30px;  
        display: inline-block;  
        position: absolute;  
        rightright: 15px;  
        top: 10px;  
    }  
}  
@media only screen and (max-width : 320px) {  
    nav li {  
        display: block;  
        float: none;  
        width: 100%;  
    }  
    nav li a {  
        border-bottom: 1px solid #FFFFFF;  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来添加子菜单,然后在第一个子菜单上添加第二个子菜单,但仍保持其响应性.

我怎样才能做到这一点?

http://jsfiddle.net/EYjnG/

Nil*_*son 2

有很多方法可以解决这个问题。

我通常使用 隐藏子菜单uldisplay: none并使用 将它们从内容流中取出position: absolute。提供li包含子菜单,以便子菜单相对于其直接父菜单,然后使用、和属性position: relative随意定位子菜单。最后,将子菜单更改为通过 a或其他。toprightbottomleftdisplay: block:hover

这是一个简单的例子:

标记:

<nav>
  <ul>
    <li><a>Link</a>
      <ul>
        <li><a>Sub link</a></li>
        <li><a>Sub link</a></li>
        <li><a>Sub link</a></li>
      </ul>
    </li>
  </ul>
</nav>
Run Code Online (Sandbox Code Playgroud)

CSS:

nav li {
  position: relative;
}

nav li > ul {
  position: absolute;
  top: 100%;
  display: none;
}

nav li:hover > ul {
  display: block;
}
Run Code Online (Sandbox Code Playgroud)

这是带有此示例的笔。它看起来像废话,但你明白了。

您可以继续嵌套更多子菜单,但您可能希望对第二级和更低级别的子菜单使用不同的定位。

但是,请注意,移动浏览器并不真正支持:hover. 至少他们不会以同样的方式对待它。您不应该让您的子菜单只能在:hover. 考虑使用 javascript 在单击时添加某种类名切换。