html - 使用 CSS3 媒体查询时减少 HTTP 请求

标签 html css media httprequest

我正在尝试使用此处讨论的 CSS3 媒体查询技术...

http://www.stuffandnonsense.co.uk/blog/about/hardboiled_css3_media_queries

如果我从下方获取代码并使用某种将自身标识为具有小屏幕的设备(例如 Device Central 或 Protofluid)的模拟器在新页面上运行它,我可以在 Charles 中看到仍然发出请求对于每个 CSS 文件,即使选择了正确的样式表,这是正常行为吗?

<link rel="stylesheet" href="smartphone.css" media="only screen and (min-device-width : 320px) and (max-device-width : 480px)">
<link rel="stylesheet" href="smartphone-landscape.css" media="only screen and (min-width : 321px)">
<link rel="stylesheet" href="smartphone-portrait.css" media="only screen and (max-width : 320px)">
<link rel="stylesheet" href="ipad.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px)">
<link rel="stylesheet" href="ipad-landscape.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)">
<link rel="stylesheet" href="ipad-portrait.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)">
<link rel="stylesheet" href="widescreen.css" media="only screen and (min-width : 1824px)">
<link rel="stylesheet" href="iphone4.css" media="only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5)">

最佳答案

是的,链接元素中的任何样式表都将被下载并动态评估媒体查询。例如,您的样式表 ipad-landscape.cssipad-portrait.css 需要在有人转动他们的设备时应用,您不想等到他们这样做以开始下载样式表和任何引用的元素,它应该立即准备就绪。唯一的异常(exception)是(或可能是)备用样式表。

这个的处理模型是discussed in the HTML5 spec :

The appropriate time to obtain the resource is when the external resource link is created or when its element is inserted into a document, whichever happens last. If the resource is an alternative stylesheet then the user agent may defer obtaining the resource until it is part of the preferred style sheet set.

关于html - 使用 CSS3 媒体查询时减少 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485529/

相关文章:

jsf-2 - PrimeFaces p:使用youtube的媒体标签

javascript - 禁用 jQuery 函数

html - 图像在固定标题上可见

javascript - 覆盖 CSS 过滤器反转在 Mozilla Firefox 中不起作用

JavaScript - 如何在完成功能后淡入不可见的按钮元素?

css - 将图像裁剪为大屏幕上的条纹

css - 不同页面上内容 div 的不同高度

html - 小型设备上的 Bootstrap 网格对齐

javascript - 为什么我的 CSS 无法使用 jquery .hover() 正确呈现?

android - 播放 Activity 中的铃声突然停止