简体   繁体   English

为什么我的媒体查询不起作用?

[英]Why does my media query not work?

My CSS looks like something below: 我的CSS看起来如下:

@media  (max-device-width:480px) {
    {
        body{background: red;}
   }
}

when I resize the browser to iphone width, I dont see body get red background. 当我将浏览器调整为iphone宽度时,我看不到身体变红背景。 Do you guys know what is happening. 你们知道发生了什么吗? I also tried @media (max-width:480px) 我也试过@media (max-width:480px)

FYI I have meta view post tag also set up as: 仅供参考我有meta view post标签也设置为:

<meta content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no" name="viewport">

You have to set it to this 你必须把它设置为这个

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

    body{background: red;}

}

EDIT Forgot to explain my changes. 编辑忘了解释我的更改。

I removed your extra brackets that were inside the media query. 我删除了媒体查询中的额外括号。 But most importantly changed it so that it is only targeting the screen(as stated is optional). 但最重要的是改变它,以便它只针对屏幕(如上所述是可选的)。 And changed max-device-width to max-width as you have already stated in your meta tag that the width should be the value of the device width. 并将max-device-width更改为max-width因为您已在meta标记中声明宽度应为设备宽度的值。

max-width is right ( max-device-width would be the screen size, not the window size) but you have extra braces. max-width是正确的( max-device-width是屏幕大小,而不是窗口大小)但是你有额外的括号。 Take them out: 带他们出去:

@media  (max-width:480px) {
    body {
        background: red;
    }
}

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

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