针对不同大小的多个媒体查询不起作用

时间:2015-08-17 00:59:13

标签: css css3 media-queries

我目前正致力于使我的网站具有响应能力,并且能够通过使用以下媒体查询来响应中/大表:

@media (max-width: 1280px) {
    .nav-tab {
        display: none;
    }

    .nav-drawer-button {
        display: block;
        float: left;
        padding-top: 30px;
        margin-left: 80%;
        cursor: pointer;
    }

    .panel-title {
        width: 40%;
        font-size: 24px;
    }

    .panel-text {
        width: 70%;
    }

    .footer-text {
        padding-top: 15px;
    }

    .social:first-child {
        margin-right: 1em;
    }
}

在尺寸为600px - 650px的屏幕上出现问题,因此我创建了另一个查询,看起来像:

@media (min-width: 600px) and (max-width: 650px) {
    .nav-drawer-button {
        margin-left: 70%;
    }
}

这似乎不起作用。它应用第一个媒体查询。我尝试更改查询的顺序,但它不起作用。我做错了什么?

以下是整个CSS:

@import url(http://fonts.googleapis.com/css?family=Roboto:500);
@import url(http://fonts.googleapis.com/css?family=Open+Sans);

body {
    margin: 0;
    background-color: #eeeeee;
}

footer {
    margin: 0;
    height: 3em;
    background-color: #333333;
    /*border-top: 3px solid #338a6c;*/
}

.navbar {
    height: 6em;
    margin: 0;
    background-color: #333333;
    border-bottom: 5px solid #338a6c;
    box-shadow: 2px 2px 5px #333333;
    list-style: none;
}

.nav-drawer-button {
    display: none;
}

.nav-drawer {
    display: none;
}

.nav-tab {
    height: 100%;
    float: left;
    color: #eeeeee;
    padding-left: 2em;
    padding-right: 2em;
    text-align: center;
    font-family: "Open Sans", sans-serif;
}

.nav-tab:nth-child(2) {
    margin-left: 55%;
}

.nav-tab:hover {
    border-left: 1px solid #555555;
    border-right: 1px solid #555555;
    background-color: rgba(85, 85, 85, .3);
    cursor: pointer;
}
.nav-logo {
    margin-left: 1em;
    padding-top: 25px;
    float: left;
}

.nav-logo:hover {
    cursor: pointer;
}

.nav-text {
    padding-top: 5px;
}

.bg {
    fill-opacity: 0.1;
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-size: cover;
    width: 100%;
    height: 500px;
    text-align: center;
}

.bg-1 {
    background-image: url("../img/para1.jpg");
}

.bg-2 {
    background-image: url("../img/para2.jpg");
}

.bg-3 {
    background-image: url("../img/para3.jpg");
    height: 300px;
}

.bg-text {

    margin: 0;
    padding-top: 250px;
    font-family: "Roboto", sans-serif;
    font-size: 36pt;
    color: #eeeeee;
    text-shadow: 2px 2px 2px #222222;
}

.bg-3>.bg-text {
    padding-top: 150px;
}

.fg {
    border-top: 3px solid #338a6c;
    border-bottom: 3px solid #338a6c;
    background-color: #eeeeee;
    width: 100%;
    height: 610px;
}

.panel-title {
    width: 20%;
    color: #222222;
    font-family: "Roboto", sans-serif;
    font-size: 36px;
    border-bottom: 3px solid #aaaaaa;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 1em;
    padding-left: auto;
    padding-right: auto;
    padding-top: 0.5em;
    padding-bottom: 20px;
    text-align: center;
}

.panel-text {
    /* SEE: util.js */
    /*opacity: 0;*/
    width: 50%;
    color: #333333;
    font-family: "Open Sans", sans-serif;
    font-size: 16px;
    font-weight: 500;
    margin-left: auto;
    margin-right: auto;
    text-align: justify;
}

.button {
    width: 8em;
    height: 2em;
    font-size: 16pt;
    font-family: "Roboto", sans-serif;
    text-align: center;
    padding-top: 1em;
    margin-top: 3em;
    margin-left: auto;
    margin-right: auto;
    border: 2px solid #338a6c;
    border-radius: 5px;
    background-color: transparent;
    color: #338a6c;
}

.button:hover {
    border: 2px solid #338a6c;
    background-color: #338a6c;
    color: #eeeeee;
    cursor: pointer;
}

.gap {
    margin-left: 5px;
}

.social-icons {
    height: 100%;
    list-style: none;
    margin: 0;
}

.social {
    float: right;
    font-size: 14pt;
    color: #eeeeee;
    height: 68%;
    width: 3em;
    padding-top: 15px;
    padding-bottom: 0;
    text-align: center;
}

.social:hover {
    border-left: 1px solid #555555;
    border-right: 1px solid #555555;
    color: #338a6c;
    background-color: rgba(85, 85, 85, .3);
    cursor: pointer;
}

.social:first-child {
    margin-right: 4em;
}

.footer-text {
    margin: 0;
    float: left;
    font-size: 10pt;
    font-family: "Open-Sans", sans-serif;
    color: #eeeeee;
    width: 20em;
    text-align: center;
    padding-top: 20px;
}

.footer-gap {
    border-top: 5px solid #338a6c;
    background-color: #eeeeee;
    height: 0.3em;
}

/* Small Tablet / Phablet */
@media (min-width: 600px) and (max-width: 600px) {
    .nav-drawer-button {
        margin-left: 10%;
    }
}

/* Tablet Stuffz */
@media (max-width: 1280px) {
    .nav-tab {
        display: none;
    }

    .nav-drawer-button {
        display: block;
        float: left;
        padding-top: 30px;
        margin-left: 80%;
        cursor: pointer;
    }

    .panel-title {
        width: 40%;
        font-size: 24px;
    }

    .panel-text {
        width: 70%;
    }

    .footer-text {
        padding-top: 15px;
    }

    .social:first-child {
        margin-right: 1em;
    }
}

1 个答案:

答案 0 :(得分:3)

首先,您的HTML需要在viewport部分设置<head>

接下来 - 您将所有第一个媒体查询应用于最大宽度为1280像素的屏幕,因此1280像素或更小。请改用@media screen (max-width: 651px) and (min-width:1280px)

对于较小的屏幕尺寸,您的代码似乎是正确的。

修改

当多个查询应用于相同的屏幕设置时,该顺序仅对显示有效。从最大的屏幕设置开始并按照最小的方式工作是很常见的,这样您每次只反映额外的更改,而不是包括每个媒体查询中的每个更改。