媒体查询和视口无效

时间:2016-08-28 18:59:23

标签: html css media-queries viewport

我试图创建一个自适应导航栏,以便当用于访问网站的设备太小而无法容纳整个导航栏时,它会折叠成一个下拉列表。奇怪的是,媒体询问我似乎没有对网站做任何事情。在我的HTML文件的头部,我有这个:<meta name="viewport" content="width=device-width, initial-scale=0.8" >

这是css:

@media screen and (max-width: 800px) {
ul.topnav li:not(: first-child) {
    display: none;
}
ul.topnav li.icon {
    float: right;
    display: inline-block;
}

ul.topnav.responsive {
    position: relative;
}
ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
}
ul.topnav.responsive li {
    float: none;
    display: inline;
}
ul.topnav.responsive li a {
    display: block;
    text-align: left;
}
}

&#13;
&#13;
var main = function () {

    function openNav() {
        var x = document.getElementById("topnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
        }
    }
}

$(document).ready(main);
&#13;
.jumbotron p {
    font-weight: 400;
    font-family: 'roboto', sans-serif;
}
.topnav {
    font-family: 'Montserrat', sans-serif;
    font-size: 16px;
    background-color: transparent;
    color: black;
    transition: all 0.25s ease;
    position: fixed;
    top: 0;
    width: 100%;
    padding: 20px;
}
ul.topnav {
    list-style-type: none;
    margin: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
}
ul.topnav li {
    float: left;
}
ul.topnav li.icon {
    display: none;
}
.topnav a {
    margin-left: 20px;
    text-decoration: none;
    color: black;
}
.topnav a:hover {
    margin-left: 20px;
    text-decoration: none;
    color: #FACD00;
}

@media screen and (max-width: 800px) {
    ul.topnav li:(not: first-child) {
        display: none;
    }
    ul.topnav li.icon {
        float: right;
        display: inline-block;
    }

    ul.topnav.responsive {
        position: relative;
    }
    ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
    }
    ul.topnav.responsive li {
        float: none;
        display: inline;
    }
    ul.topnav.responsive li a {
        display: block;
        text-align: left;
    }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!DOCTYPE html>

<body>

    <!--navbar-->
    <ul class="topnav">
        <li><a href="#">Home</a></li>
        <li><a href="#">Events</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Volunteer</a></li>
        <li><a href="#">Contact</a></li>
        <li class="icon">
            <a href="javascript:void(0);" onclick="openNav()">&#9776;</a>
    </ul>


</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

只需将媒体查询放在css文件的底部即可。 CSS规则是通过特异性应用的,当具有相同的特异性(这是您的情况)时,选择器出现的顺序。

还纠正了Artem Gorlachev指出的语法错误。

.jumbotron p {
    font-weight: 400;
    font-family: 'roboto', sans-serif;
}
.topnav {
    font-family: 'Montserrat', sans-serif;
    font-size: 16px;
    background-color: transparent;
    color: black;
    transition: all 0.25s ease;
    position: fixed;
    top: 0;
    width: 100%;
    padding: 20px;
}
ul.topnav {
    list-style-type: none;
    margin: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
}
ul.topnav li {
    float: left;
}
ul.topnav li.icon {
    display: none;
}
.topnav a {
    margin-left: 20px;
    text-decoration: none;
    color: black;
}
.topnav a:hover {
    margin-left: 20px;
    text-decoration: none;
    color: #FACD00;
}
@media screen and (max-width: 800px) {
    ul.topnav li:not(:first-child) {
        display: none;
    }
    ul.topnav li.icon {
        float: right;
        display: inline-block;
    }

    ul.topnav.responsive {
        position: relative;
    }
    ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
    }
    ul.topnav.responsive li {
        float: none;
        display: inline;
    }
    ul.topnav.responsive li a {
        display: block;
        text-align: left;
    }
}