Css ID不会改变媒体查询

时间:2015-05-11 16:42:06

标签: html css media-queries

我的媒体查询未正确更新。这是测试html:

 <!DOCTYPE HTML>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="main.css">
        <title>Test </title>
    </head>
    <body>
        <div class="row">
            <div id="verde" class="col-5">
                <h2>Lorem Site</h2>
                <h3>Test Site</h3>
            </div>
        </div>
        <div class="row">
<div class="col-12">
    <p>Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
</div>
        </div>
        <div class="row">

<div id="verde" class="col-4">
                <p> Col 4Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
            </div>
            <div id="verde" class="col-4">
                <p>Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
            </div>
            <div class="col-4">cOL4
                <p>Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
            </div>
        </div>
    </body>

</html>

这是我的CSS

    @media screen and (max-width:760px){
     #verde{
        color:red;
    }
}
*{
    border: 1px solid red !important;
}
*{
    box-sizing:border-box;
}

p {
    margin: 10px;
}
#verde{
    color:green;
}
.row{
    width: 100%;
    margin:auto;
    display: flex;
    flex-wrap:wrap;
}
.col-1{
    width: 8.33%;
}
.col-2{
    width: 16.66%;
}
.col-3{
    width: 25%;
}
.col-4{
    width: 33.33%;

}
.col-5{
    width: 41.66%;
    margin-left:auto;
}
.col-6{
    width: 50%;
}
.col-7{
    width: 58.33%;
}
.col-8{
    width: 66.66%;
}
.col-9{
    width: 75%;
}
.col-10{
    width: 83.33%;
}
.col-11{
    width: 91.66%;
}
.col-12{
    width: 100%;
    margin-left:20%;
}

现在,在媒体查询中,我想将id #verde更改为红色。 这种方法不起作用,只有在我更具体的情况下才能使用

 body #verde

或者我具体说明了这个类和id。 问题是 为什么只有在我更具体的情况下才有效?

先谢谢

2 个答案:

答案 0 :(得分:9)

您需要将媒体查询放在样式表的末尾,因为样式的方式是&#34;级联&#34;

目前您的媒体查询是第一位的,因此在此之后的任何内容都不会被覆盖。

...Everything before...

 @media screen and (max-width:760px){
     #verde{
        color:red;
    }
}

Here's a JSFiddle where I rearranged your CSS.

答案 1 :(得分:1)

问题是您已将媒体查询放在CSS的开头,因此它被&#34; generic&#34;覆盖。稍后出现在CSS中的样式。解决方案是将媒体查询移动到CSS的末尾,或者在窗口宽度大于760px时创建第二个媒体查询以设置#verde样式。