菜单随响应式CSS3设计而消失

时间:2014-08-22 01:50:05

标签: html5 css3

当我调整窗口大小而不是折叠到单个下拉菜单时,菜单会完全消失。我的代码出了什么问题,我该如何解决?

HTML

</head>
<body>
<div id="logo">
        <a href="index.html"><img src="http://i.imgur.com/cosDXx1.png"/></a>
    </div> 
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a>
                <ul>
                    <li><a href="#">History</a></li>
                    <li><a href="#">Brothers</a></li>
                </ul>
            </li>
            <li><a href="#">Philanthropy</a>
                <ul>
                    <li><a href="#">Greek God</a></li>
                    <li><a href="#">Kovacs Walk</a></li>
                </ul>
            </li>
            <li><a href="#">Membership</a></li>
            <li><a href="#">Photos</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>

CSS

* {
    -webkit-box-sizing: border-box;
    /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;
    /* Firefox, other Gecko */
    box-sizing: border-box;
    /* Opera/IE 8+ */
}

body {
    margin:0px;
    padding:0px;
}


#logo{
    width:100%;
    height:110px;
    background-color:black;
    text-align:center;
}

#logo img{
    display:inline-block;
    margin-right:auto;
    margin-left:auto;
}

nav {
    display: block;
    position: relative;
    min-height: 40px;
}
nav, nav a {
    color: white;
    text-decoration: none;
}
nav a {
    display: block;
    padding: 10px;
}
nav a:hover {
    background: gray;
}
nav ul {
    padding:0;
    margin: 0;
}
nav ul li {
    float:left;
    display:inline;
    position:relative;
    background: #505050;
    border-right: 1px solid white;
    text-align:center;
}
nav ul li ul {
    left:0;
    top:100%;
}
/*Hide all submenus*/
nav ul ul {
    display:none;
}
nav li:hover > ul {
    display:block;
    position:absolute;
    z-index:1000;
    width: 120px;
}
nav ul ul li:hover > ul {
    right: -120px;
    top: 0;
    left: auto;
}

nav > input,
nav > label {
  display: none;
}

nav > label {
  width: 100%;
  float: left;
  padding: 10px;
  padding-right: 0;
  cursor: pointer;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.footer {
    padding-top: 19px;
    color: #777;
    border-top: 1px solid #e5e5e5;
}


/* Responsive: Portrait tablets and up */
/*Media query*/
@media (max-width: 400px) {
    nav > label, nav ul li ul {
        display: block;
    }
    nav > ul {
        display: none;
        clear: both;
    }
    nav > input:checked + ul { /* <================ This is the key */
        display: block;
    }
    nav ul ul li a:before {
        content:'↪ ';
        display: inline;
    }
    nav ul li {
        float: none;
        display: block;
        border-right: none;
        padding-left: 10px;
    }
    nav li:hover ul {
        position: relative;
        width: auto;
    }
    nav ul ul li:hover > ul {
        right:auto;
        top: auto;
        left: auto;
    }

继承我的JSFiddle

http://jsfiddle.net/ybd8hs0v/

1 个答案:

答案 0 :(得分:0)

问题是您在媒体查询中隐藏了<ul>元素:

/* Responsive: Portrait tablets and up */
/*Media query*/
@media (max-width: 400px) {
    nav > label, nav ul li ul {
        display: block;
    }
    nav > ul {
        display: none;
        clear: both;
    }

删除display: none;,然后您就可以恢复菜单了。

如果您将来尝试调试,请右键单击并选择&#34; Inspect Element&#34;。您可以单击每个元素以查看其CSS属性。