简体   繁体   English

为什么我的媒体查询 styles 没有变化?

[英]How come my media query styles aren't changing?

I'm trying to test my media query code by adding a background color to the header in my tablet view.我正在尝试通过在我的平板电脑视图中向 header 添加背景颜色来测试我的媒体查询代码。 When I try and change it back to the desktop view it still stays red even though I didn't include red as a background colour in my desktop view media query.. Can anyone offer any advice?当我尝试将其更改回桌面视图时,即使我没有在桌面视图媒体查询中将红色作为背景色包括在内,它仍然保持红色。任何人都可以提供任何建议吗?


/* For tablet: */

@media only screen and (min-width: 601px) {
  
    /* Header section  */
 
   header {
        margin: 40px auto;
       background-color: red;
      
    }
    
 
    
} /* Closing tablet query - DO NOT DELETE */



/* For desktop*/

@media only screen and (min-width: 992px) {

  .col-l-1 {width: 8.33%;}
  .col-l-2 {width: 16.66%;}
  .col-l-3 {width: 25%;}
  .col-l-4 {width: 33.33%;}
  .col-l-5 {width: 41.66%;}
  .col-l-6 {width: 50%;}
  .col-l-7 {width: 58.33%;}
  .col-l-8 {width: 66.66%;}
  .col-l-9 {width: 75%;}
  .col-l-10 {width: 83.33%;}
  .col-l-11 {width: 91.66%;}
  .col-l-12 {width: 100%;}
    
    
/* Header section  */

    header {
        width: 1100px;
        margin: 40px auto;
    }
    
    .logo {
        margin-left: 290px;
        margin-top: 80px;
    }
    
    nav {
         margin-top: 60px;
         margin-left: 240px;
            
    }  

} /*closing for desktop*/

So I think what's happening is both styles are being applied, and since the 992px doesn't specify a background-color it defaults to using red.所以我认为正在发生的事情是 styles 都被应用,并且由于 992px 没有指定背景颜色,它默认使用红色。 Using max-width can eliminate that from happening.使用max-width可以消除这种情况。 See below for a simple example:请参阅下面的简单示例:

 @media only screen and (max-width: 601px) { header { width: 1100px; margin: 40px auto; background-color: red; } } @media only screen and (max-width: 992px) { header { width: 1100px; margin: 40px auto; } }
 <header>Header</header> <nav>Nav</nav>

The problem you are having is due to the media query, which specifies min-width: 601px .您遇到的问题是由于媒体查询,它指定min-width: 601px For all screens 601 pixels or larger, the properties assigned to each selector will be true, so all header elements will have a background-color: red;对于所有 601 像素或更大的屏幕,分配给每个选择器的属性将为 true,因此所有header元素都将具有background-color: red; , unless otherwise specified, in all screens with a width 601 pixels or larger. ,除非另有说明,在所有宽度为 601 像素或更大的屏幕中。

To avoid seeing the red background color in your desktop sizes, you should either:为避免在您的桌面尺寸中看到红色背景颜色,您应该:

1 - Overwrite the property on the desktop media query: 1 - 覆盖桌面媒体查询上的属性:


@media only screen and (min-width: 992px) {
    header {
        width: 1100px;
        margin: 40px auto;
        background-color: transparent; /* or whatever you'd like */
    }
}

2 - Or limit your tablet media query to tablet sizes by adding a max-width condition: 2 - 或通过添加最大宽度条件将您的平板电脑媒体查询限制为平板电脑尺寸:

@media only screen and (min-width: 601px) and (max-width: 992px) {
  ...
}

CSS works with the cascade style flow. CSS 使用级联样式流。 The order of the rules will override the next rule if they are pointing the same css rule.如果它们指向相同的 css 规则,则规则的顺序将覆盖下一条规则。 If they doesn't the last rule will be maintained (it doesn't matter if the rules are in a media query).如果他们没有,最后一条规则将被维护(规则是否在媒体查询中并不重要)。 Resume: the media query doesn't override all the rules, only override the rules that you defined and it'll maintain the other rules as the last value in relation of the order in the css file.恢复:媒体查询不会覆盖所有规则,只会覆盖您定义的规则,并且会将其他规则维护为与 css 文件中的顺序相关的最后一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM