减小浏览器大小时关闭导航菜单

时间:2014-02-10 21:08:40

标签: html5 css3 drop-down-menu responsive-design nav

我正在制作一个自适应网站,在这个网站上我有一个下拉菜单。当我将大小降低到900px时,我会保留菜单,但它会一直按到浏览器的左侧。我给了它一个小的左边填充,所以它会离开墙壁,直到它达到600px,此时它进入列表块视图,我隐藏下拉框。基本上我需要找到一种方法来保持600px和900px之间的下拉菜单,以便它仍然会显示在他们应该的相应位置下。包括a screen shot to show what's happening.

编辑:我制作了fiddle

现在我的代码

HTML5

<nav>
    <nav class="nav-wrapper">
        <ul>
            <li><a href="index.html">Home</a>
            </li>
            <li><a href="appliances.html">Appliances</a>
            </li>
            <li><a href="Electronics/index.html">Electronics</a>

                <ul>
                    <li><a href="Electronics/computers.html">Computers</a></li>
                    <li><a href="Electronics/game_systems.html">Game Systems</a></li>
                    <li><a href="Electronics/televisions.html">Televisions</a></li>
                </ul>
            </li>
            <li><a href="Furniture/index.html">Furniture</a>

                <ul>
                    <li><a href="Furniture/bedroom.html">Bedroom</a></li>
                    <li><a href="Furniture/dining_room.html">Dining Room</a></li>
                    <li><a href="Furniture/living_room.html">Living Room</a></li>
                </ul>
            </li>
            <li><a href="location.html">Location</a>
            </li>
        </ul>
    </nav>
</nav>

CSS3

/*Default CSS*/
.main-header nav ul ul {
    display: none;
}

.main-header nav ul li:hover > ul {
    display: block;
}

.main-header nav ul {
    background: #efefef;
    background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
    background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
    background: -webkit-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
    box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
    padding: 0 20px;
    border-radius: 10px;
    list-style: none;
    position: relative;
    display: inline-table;
}

.main-header nav ul:after {
    content:"";
    clear: both;
    display: block;
}

.main-header nav ul li {
    float: left;
}

.main-header nav ul li:hover {
    background: #4b545f;
    background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
    background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
    background: -webkit-linear-gradient(top, #4f5964 0%, #5f6975 40%);
}

.main-header nav ul li:hover a {
    color: #fff;
}

.main-header nav ul li a {
    display: block;
    padding: 25px 40px;
    color: #757575;
    text-decoration: none;
}

.main-header nav ul ul {
    background: #5f6975;
    border-radius: 0px;
    padding: 0;
    position: absolute;
    top: 100%;
}

.main-header nav ul ul li {
    float: none;
    border-top: 1px solid #6b727c;
    border-bottom: 1px solid #575f6a;
    position: relative;
}

.main-header nav ul ul li a {
    padding: 15px 40px;
    color: #fff;
}

.main-header nav ul ul li a:hover {
    background: #4b545f;
}

    /*Between 600px and 900px*/

.main-header nav ul {
    position: relative;
    margin: 0 auto;
    left: 10%;
}

.main-header nav ul ul {
    position: absolute;
    top: 100%;
}

正如你所看到的,我没有搞砸过很多东西,我认为我需要的工作应该仅仅是这两行。如果有人可以帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

绝对定位的下拉列表需要位于相对定位的容器内

.main-header nav ul li {
    float: left;
    position: relative;
}

http://jsfiddle.net/UhEk4/2/

显然,要调整职位还有很多工作要做但你应该可以从这里开始