媒体查询不会优先

media query won't take precedence

我有一些简单的 CSS 和 HTML 只是为了看看媒体查询是否有效。但是出了点问题

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
    .box {
        background: red;
        width: 100px;
        height: 200px
    }
    /* Mobile Phones */
    @media screen and (max-device-width:640px)  {
        .box {
            background: blue;
        }
    }
    </style>
</head>
<body>
<div class="box"></div>
</body>
</html>

在移动设备上查看时框背景不会变为蓝色。测试时我使用firefox插件"Go Mobile"模拟手机屏幕环境

解决方案:https://jsfiddle.net/r56m37kb/

max-width 而不是 max-device-width

您使用的媒体查询语法不正确。这是一个工作片段,其中包含有关更改位置的评论(最大宽度与最大设备宽度):

 .box {
        background: red;
        width: 100px;
        height: 200px
    }
    /* Mobile Phones */
    @media screen and (max-width:640px) /* Changed from max-device-width */  {
        .box {
            background: blue;
        }
    }
<div class="box"></div>