我的媒体查询不适用于Google Chrome和Firefox

时间:2018-11-05 08:23:48

标签: html css

我尝试使用Google Chrome的移动响应工具(F12:ctrl + shift + M)在我的项目网页(localhost)上检查媒体查询的功能

我已将此媒体查询放置在CSS的底部。

@media screen and (max-width: 480px) {

header{
background-color: black;
}

}

不幸的是,当最大宽度已经为480px时,结果没有将标题颜色更改为黑色

Screenshot of Header

请单击链接以查看输出。 这是我在HTML中完成元数据的方式:

<!DOCTYPE html>
<html>
    <head>  
            <meta charset="utf-8">
            <title>I Love Gems and Crystals</title>
            <link href="style.css" rel="stylesheet" type="text/css">
            <meta name="viewport" content="width=device-width">

    </head>

我希望当我达到480px最大宽度时,标题颜色将变为黑色。但事实并非如此。

Google Chrome版本= screenshot

这是我的完整CSS。

   *{
font-family: Garamond;
}

body, ul, li{
    margin: 0;
    padding: 0;
}

.wrapper{
    width: 96%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 2%;
}

#main-menu li{
    list-style-type: none;

}

header{
    background-color: #20e4cb;
    padding: 1px 0px;
    margin: 0 auto;

}




header h1{
    width: 130px;
    margin-left: 50px;
     float: left;
}

#logo{
    background-image: url(images/Logo.png);
    background-repeat: no-repeat;
    text-indent: -10000px;
    padding: 18px 0px;

}


#main-menu{
    float: right;
}

header:after{
    display: block;
    content: "";
    clear: both;
}

#main-menu h2{
    text-indent: -10000px;
    line-height: : 0;
    margin: 0;
}

#main-menu li{
    float: left;
    margin-left: 20px;
    margin-top: 20px;
    margin-right: 20px;
}

#main-menu li a{
    text-decoration: none;
    text-transform: uppercase;
    font-weight: bold;
    font-size: 14pt;
    color: #0f4ced;

}

#main-banner img{
    width: 100%;
    max-height: 330px;
    max-width: : 1200px;
    overflow: hidden;
}

#featured-post-header{
    text-align: center;
    margin-top: 50px;
}

.featured-post-content{
    width: 23%;
    margin-left: 100px;
    margin-right: 0px;
    padding: 0%;
    float: left;
}

.featured-post-content p{
    text-align: center;
    width: 200px;
    font-weight: bold;
}


#knowledge{
    text-align: center;
    margin-top:70px;
}

#content-grid:after{
    display: block;
    content: "";
    clear:both;
}

#knowledge-content{
    width: 23%;
    float: left;
    margin-left: 70px;
    background-color:#20e4cb;
    max-height: 280px;
    padding: 7px;
    overflow: hidden;

    -webkit-transition: max-height 0.7s;
    -moz-transition: max-height 0.7s;
    transition: max-height 0.7s;
}

#knowledge-content2{
    width: 23%;
    float: left;
    margin-left: 70px;
    background-color:#20e4cb;
    max-height: 280px;
    padding: 7px;
    overflow: hidden;

    -webkit-transition: max-height 0.7s;
    -moz-transition: max-height 0.7s;
    transition: max-height 0.7s;
}

#knowledge-content3{
    width: 23%;
    float: left;
    margin-left: 70px;
    background-color:#20e4cb;
    max-height: 280px;
    padding: 7px;
    overflow: hidden;

    -webkit-transition: max-height 0.7s;
    -moz-transition: max-height 0.7s;
    transition: max-height 0.7s;
}

#knowledge-content.open {
    max-height: 2000px;

    -webkit-transition: max-height 0.7s;
    -moz-transition: max-height 0.7s;
     transition: max-height 0.7s;
}

#knowledge-content2.open {
    max-height: 2000px;

    -webkit-transition: max-height 0.7s;
    -moz-transition: max-height 0.7s;
     transition: max-height 0.7s;
}

#knowledge-content3.open {
    max-height: 2000px;

    -webkit-transition: max-height 0.7s;
    -moz-transition: max-height 0.7s;
     transition: max-height 0.7s;
}

.page-content:after{
    display: block;
    content: "";
    clear: both;

}


#knowledge-content h2, p{
    color: #0f4ced;
}
#knowledge-content2 h2, p{
    color: #0f4ced;
}
#knowledge-content3 h2, p{
    color: #0f4ced;
}

#knowledge-content p{
    font-size: 16pt;
}

#knowledge-content2 p{
    font-size: 16pt;
}
#knowledge-content3 p{
    font-size: 16pt;
}

#button-area1{
    width: 20%;
    float: left;
    margin-left: 14%;

}

#button-area2{
    width: 20%;
    float: left;
    margin-left: 10%;

}

#button-area3{
    width: 14%; 
    float: left;
    margin-left: 11%;
    margin-right: 0%;

}


footer{
    background-color: black;
    color: white;
    padding: 5px 0px;
    margin-top: 100px;
    text-align:center;
}

#end-content-grid:after{
    display:"block";
    content:"";
    clear: both;

}

//* MEDIA QUERIES */

@media screen and (max-width: 480px) {

header{
background-color: black;
}

}

2 个答案:

答案 0 :(得分:2)

您用于媒体查询的CSS语法应如下:

/* 
  ##Device = Most of the Smartphones Mobiles (Portrait)
  ##Screen = B/w 320px to 479px
*/

@media (max-width: 480px) {

  //CSS

}

从以下位置无耻地复制和修改了CSS代码段:

https://gist.github.com/gokulkrishh/242e68d1ee94ad05f488

为链接添加书签,以备将来参考。 另外,我建议您始终对CSS中的颜色使用rgba()hexadecimal值。

这是一个小提琴:

#header{
  background-color: black;
  color:white;
}

@media (max-width: 480px) {
  #header{
  background-color: blue;
}
}
<div id="header">Hi there!!!!!</div>

<!-- Try Modifying the size of the HTML Output Section -->

答案 1 :(得分:0)

OR @media“仅”屏幕和(最大宽度:480px)

#header{
  background-color: black;
  color:white;
}

@media only screen and (max-width: 480px) {
  #header{
  background-color: blue;
}
}
<div id="header">Hi there!!!!!</div>

<!-- Try Modifying the size of the HTML Output Section -->