javascript - 更有效地应用媒体查询

标签 javascript css html

我写了一个示例代码来尝试在 css 中进行媒体查询...
这是代码:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    background-color: green;
}

@media only screen and (min-device-width: 374px) and (max-device-width: 376px) {
    body {
        background-color: blue;
    }
    #img {
        background-color: blue; 
    }
button {
        margin: auto;
    }
}
</style>
</head>
<body>

<p>Media queries are simple filters that can be applied to CSS styles. They make it easy to change styles based on the characteristics of the device rendering the content, including the display type, width, height, orientation and even resolution.</p>
<button>Click me!</button>
<image src='Promo_2-2.jpg' id='img' alt='image'></image>
</body>
</html>

但媒体查询不起作用。我哪里错了?

最佳答案

如果您是在计算机上查看查询,那么您的查询没有任何问题。记住这个:min-device-width计算设备的宽度,而 min-width计算窗口大小。您的媒体查询应如下所示:

@media only screen and (min-width: 374px) and (max-width: 376px) {
    body {
        background-color: blue;
    }
    #img {
        background-color: blue; 
    }
button {
        margin: auto;
    }
}

关于 only关键字,Mozilla 开发者网络是这样说的:

only 关键字阻止不支持具有媒体功能的媒体查询的旧版浏览器应用给定样式:

<link rel="stylesheet" media="only screen and (color)" href="example.css" />

Learn more about Media Queries here.

关于javascript - 更有效地应用媒体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26715991/

相关文章:

javascript - 使用 velocity.js 时是否可以对动画进行排队?

javascript - 确保垂直内容水平溢出

javascript - 鼠标悬停在图像上闪烁

javascript - 正则表达式 - 替换 HTML 字符串中不包含在 HTML 标记中的文本

javascript - ajax值未返回

html - 是否可以更改/覆盖 DRUPAL 模块的自动生成的 html 代码?

html - 响应式网页设计 : "How to resize a background image according to browser window size using CSS"?

javascript - 使用javascript将多维数组字符串化

javascript - ExtJS 4 - 如何使用 Ajax 下载文件?

html - Bootstrap 组件上的填充