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

发布于 2024-10-28 04:49:50 字数 1440 浏览 5 评论 0原文

我正在尝试使用此处讨论的 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)">

I'm trying to use the CSS3 media query techniques discussed here...

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

If I grab the code from below and run it on a new page using some sort of emulator that identifies itself as a device with a small screen (e.g. Device Central or Protofluid) I can see in Charles that a request is still made for each CSS file even though the correct stylsheet is picked up, is this normal behaviour?

<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)">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会发光的星星闪亮亮i 2024-11-04 04:49:51

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

其处理模型讨论过在 HTML5 规范中:

获取的适当时间
资源是当外部资源
链接被创建或者当它的元素被创建时
插入到文档中,无论
最后发生。如果资源是
替代样式表然后用户
代理可能会推迟获取资源
直到它成为首选的一部分
样式表集。

Yep, any stylesheets in a link element will be downloaded and the media queries evaluated dynamically. For instance your stylesheets ipad-landscape.css and ipad-portrait.css would need to be applied when someone turns their device round, you don't want to wait until they do that to start downloading the stylesheet and any referenced items, it should be ready right away. The only exception is (or could be) alternate stylesheets.

The processing model for this is 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文