简体   繁体   English

CSS精灵和HTTP请求

[英]CSS sprites and HTTP requests

My first question was : if I declare multiple rules like 我的第一个问题是:如果我声明多个规则,例如

.link1 {background:url('sprite.png') 0 0;}
.link2 {background:url('sprite.png') -20px -20px;}

Do these count as separate HTTP requests? 这些算作单独的HTTP请求吗? I found multiple answers on this and I know now that the image would be fetched from the browsers cache. 我对此找到了多个答案,现在我知道该图像将从浏览器缓存中获取。

Now for the new question: How would I check this in Chrome/Firefox? 现在针对新问题:如何在Chrome / Firefox中进行检查? Where in the console can I check how many HTTP requests are made from within a stylesheet. 我可以在控制台中的哪个位置检查样式表中发出了多少个HTTP请求。

Off the top of my head YSlow can help with this YSlow可以帮助解决这个问题

Yahoo's Exceptional Performance team has identified 34 rules that affect web page performance. 雅虎的卓越性能团队已经确定了34条影响网页性能的规则。 YSlow's web page analysis is based on the 23 of these 34 rules that are testable. YSlow的网页分析基于这34条可测试的规则中的23条。

  1. Minimize HTTP Requests 最小化HTTP请求

That said, sprites specifically reduce the number of HTTP requests being made 也就是说,精灵可以专门减少发出的HTTP请求的数量

Bring up the Chrome or Firefox Developer Tools and check the Network tab to see a list of all network operations and requests. 调出Chrome或Firefox开发人员工具,然后选中“网络”标签以查看所有网络操作和请求的列表。

Sprites should only require a single GET request regardless of how many times they are referenced in the CSS. Sprite只需一个GET请求,无论CSS中引用了多少次。 It is cached after the initial request. 初始请求后将对其进行缓存。

You can also use a tool like YSlow or an online page speed test / profiler to examine network requests. 您还可以使用YSlow之类的工具或在线页面速度测试/分析器来检查网络请求。

Do these count as separate HTTP requests? 这些算作单独的HTTP请求吗?

Nope. 不。 It gets cached the first time and then reused from cache. 它是第一次缓存的,然后从缓存中重用。 In some cases using SSL may force the browser to reload them on return visits. 在某些情况下,使用SSL可能会迫使浏览器在回访时重新加载它们。

The Minimize HTTP Requests rule isn't a bad one, but the style rules can become a nightmare to manage if your 'sprite sheet' gets quite large! 最小化HTTP请求规则不是一个坏规则,但是如果您的“ sprite sheet”变得很大,则样式规则可能成为管理的噩梦!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM