简体   繁体   English

css中的多个媒体查询无法正常工作

[英]Multiple media queries in css not working

I have multiple queries in my stylesheet where the code @media only screen and (min-width : 768px) and (max-width : 1024px) { } works perfectly fine but under it I have @media screen and (min-device-width : 176px) (max-device-width : 360px) {} does not work. 我的样式表中有多个查询,其中代码@media only screen and (min-width : 768px) and (max-width : 1024px) { }工作得很好,但在它下面我有@media screen and (min-device-width : 176px) (max-device-width : 360px) {}不起作用。

CSS: CSS:

@media only screen and (min-width : 768px) and (max-width : 1024px) { 
body {background: red; } }


@media screen and (min-device-width : 176px) (max-device-width : 360px) {
body {background: green; } }

You're missing the AND between (min-device-width : 176px) (max-device-width : 360px). 你错过了AND之间的(min-device-width : 176px) (max-device-width : 360px).

@media screen and (min-device-width : 176px) and  (max-device-width : 360px) {
    body {background: green; } 
}

The other issues here is you are using min-device-width. 这里的其他问题是你使用min-device-width. (referring to the resolution of the device vs browser width) which is what @NoobEditor is saying below. (参考设备的分辨率与浏览器宽度),这是@NoobEditor在下面所说的内容。

Are you doing that on purpose? 你是故意这样做的吗? If not you should be using min-width && max-width 如果不是,你应该使用min-width && max-width


@media screen and (min-width : 176px) and  (max-width : 360px) {
    body {background: green; } 
}

check this fiddle , change the browser width to see the the media query in action 检查这个小提琴 ,更改浏览器宽度以查看正在运行的媒体查询

@media screen and (max-width : 1500px) {
    body {
        background: #000;
        border-top: 2px solid #DDDDDD;
    }
}

@media screen and (min-width : 768px) and (max-width : 1024px) {
    body {
        background: #fff;
        border-top: 2px solid #DDDDDD;
    }
}

This fiddle works fine, but if you change the order of the media queries it wont work...try it for yourself! 这个小提琴工作得很好,但如果你改变媒体查询的顺序它就不会工作......试试吧!

CSS always selects the last style that was declared if multiple style are found for an attrib. 如果为attrib找到多个样式,CSS总是选择声明的最后一个样式。

for eg : 例如:

@media (max-width: 1024px) {
  body {
    background: black;
  }
}

@media (max-width: 768px) {
  body {
    background: white;
  }
}

for 765px ( since both mq cover this width ) color selected would be white 对于765px因为mq都覆盖此宽度 )所选的颜色将为white

For multiple width for same style, use , : 对于多个width为同一款式,使用,

@media (max-width: 360px), (max-width: 768px) {

    /* style goes here */

}

I found the solution. 我找到了解决方案。 You have to make 2 CSS files, in the first code the first media query and in the second write yout second media query code. 您必须制作2个CSS文件,在第一个代码中第一个媒体查询,在第二个代码中编写第二个媒体查询代码。 I know it is an elegant way to do it but it works. 我知道这是一种优雅的方式,但它有效。

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

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