css - 媒体查询中的 "screen"和 "only screen"有什么区别?

标签 css media-queries

媒体查询中screenonly screen有什么区别?

<link media="screen and (max-device-width: 480px)" rel="stylesheet" href="m.css" />
<link media="only screen and (max-device-width: 480px)" rel="stylesheet" href="m.css" />

为什么我们需要使用only screenscreen 本身是否提供了足够的信息以 呈现给屏幕?

我见过许多使用以下三种不同方式中的任何一种的响应式网站:

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

最佳答案

让我们一个一个地分解你的例子。

@media (max-width:632px)

这个是说对于 max-width 为 632px 的窗口,您要应用这些样式。在大多数情况下,在那个尺寸下,你会谈论任何小于桌面屏幕的东西。

@media screen and (max-width:632px)

这是针对具有 screenmax-width 为 632px 的窗口应用样式的设备。除了您指定的是 screen 而不是 other available media types 之外,这几乎与上面的相同。最常见的另一种是 print

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

这是直接来自 W3C 的引述来解释这一点。

The keyword ‘only’ can also be used to hide style sheets from older user agents. User agents must process media queries starting with ‘only’ as if the ‘only’ keyword was not present.

由于不存在“仅”这样的媒体类型,因此旧版浏览器应忽略样式表。

这是 link to that quote该页面上的示例 9 中显示了这一点。

希望这对媒体查询有所启发。

编辑:

一定要查看@hybrids excellent answer关于如何真正处理 only 关键字。

关于css - 媒体查询中的 "screen"和 "only screen"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549529/

相关文章:

html - 向下滚动时,如何使其与我的视频重叠?

css - 是否可以在 Windows 上从 WSH 运行 CSLint?

css - @media screen 没有在正确的屏幕尺寸上应用正确的样式

ipad - 仅针对 iPad 和 iPad 的 CSS 媒体查询?

google-chrome - 仅在 Chrome 上定位两种屏幕尺寸

css - 媒体查询 : How to target desktop, 平板电脑和手机?

javascript - 你如何为我的角色添加坐标,这是一个将继续移动的 img

internet-explorer - 类似于 IE 过渡的东西

javascript - 我怎样才能拆开这个 SVG 字体以在 Chrome 中以编程方式使用它?

css - 如何在桌面上检查媒体查询