简体   繁体   English

如何减少CSS HTTP请求?

[英]How to reduce css http requests?

How to reduce css http requests? 如何减少CSS HTTP请求?

1 large css file 1个大型CSS文件

or one css file which importing all other css 或一个导入所有其他CSS的CSS文件

is this same? 一样吗

What is the benefit of using this 使用这个有什么好处

@import url('/css/typography.css');
@import url('/css/layout.css');
@import url('/css/color.css');

1 large css file or one css file which importing all other css is this same? 1个大css文件或一个导入所有其他css的css文件是一样的吗?

No as the Client still needs to load every CSS file (with one HTTP-request each) 否,因为客户端仍然需要加载每个CSS文件(每个都带有一个HTTP请求)

Some Links to learn more about reducing HTTP requests: 一些链接以了解有关减少HTTP请求的更多信息:

If you are using Firefox I highly recommend Firebug , which offers a view with detailed information about HTTP-Requests. 如果您使用的是Firefox,我强烈建议您使用Firebug ,它提供了一个包含有关HTTP请求的详细信息的视图。
EDIT: 编辑:
As flybywire points out in the comments: YSlow is a Firefox extension that integrates with Firebug to analyze web page performance 正如flybywire在评论中指出的那样: YSlow是Firefox扩展,与Firebug集成以分析网页性能

3 easy things I would try first: 首先我会尝试3件简单的事情:

  • Combine global CSS (the things you use on every page) 合并全局CSS(您在每个页面上使用的东西)
  • Compress CSS (with YUI compressor or some online tool) 压缩CSS(使用YUI压缩器或某些在线工具)
  • Inline the page-specific stuff (things that are not shared among pages) 内联页面特定的内容(页面之间不共享的内容)

Most of those things also apply to Javascript files. 这些东西大多数也适用于Javascript文件。

A style import will still trigger an HTTP request. 样式导入仍将触发HTTP请求。 I recommend serving only one CSS file. 我建议仅提供一个CSS文件。 You could combine multiple CSS files using imports from the server side to send one file without managing all your CSS rules from a single file. 您可以使用服务器端的导入来组合多个CSS文件,以发送一个文件,而无需从一个文件管理所有CSS规则。

In case you are using asp.net mvc - check out this approach . 如果您使用的是asp.net mvc,请查看此方法 Still works like a charm. 仍然像魅力。 Basically - it uses httphandler to combine, compress and cache asset files. 基本上-它使用httphandler合并,压缩和缓存资产文件。

But that's asp.net specific. 但这是特定于asp.net的。 Can't tell if that helps. 无法判断是否有帮助。


Offtopic: 题外话:

The same technique you can apply for javascript files (it feels great to have nice structured js files and still - to avoid many http requests). 您可以将相同的技术应用于javascript文件(拥有漂亮的结构化js文件感觉很好,而且仍然如此-避免了许多http请求)。

For images - check out spriting . 对于图像-检查spriting This bookmarklet can be quite helpful too. 此小书签也可能很有帮助。

Do you want to reduce number of http requests or size of them? 您是否要减少http请求的数量或大小?

I would recommend to put all the most used selectors into one large CSS file (along with all your libraries / plugins used) and minify it with online optimizers/minimizers 我建议将所有最常用的选择器放到一个大CSS文件中(连同所有使用的库/插件一起使用),并使用在线优化器/最小化器将其最小化

If you are going to be serving your css files from a web farm or it is a high volume site, I would suggest having a look at : http://code.google.com/p/talifun-web/wiki/CrusherModule It uses a file watcher to look for changes on css/js files. 如果您打算从网络服务器场中访问css文件,或者它是一个高流量站点,我建议您看一下: http : //code.google.com/p/talifun-web/wiki/CrusherModule使用文件观察器查找css / js文件的更改。 Css/js files belong to file sets specified in the web.config. Css / js文件属于web.config中指定的文件集。 When a change is detected on one of the component js/css files it creates a new munged css or jss for the file set. 当在一个组件js / css文件中检测到更改时,它将为该文件集创建一个新的css或jss。

It also has a simple control to output css/js file set links. 它还具有用于输出css / js文件集链接的简单控件。 The control will append a querystring with a hash of the file, so you are guaranteed the correct file contents. 该控件将在查询字符串后附加文件的哈希,因此可以保证文件内容正确。

This means that you can serve the munged file with IIS directly. 这意味着您可以直接使用IIS服务该文件。 Then you can take advantage of kernel mode caching. 然后,您可以利用内核模式缓存。 Also means you don't need to worry about buggy implementations of http header support: 这也意味着您无需担心HTTP标头支持的错误实现:

  • ETag 标签
  • Expires 过期
  • Last-Modified 上一次更改
  • If-Match 如果匹配
  • If-None-Match 如果不匹配
  • If-Modified-Since 如果自修改
  • If-Unmodified-Since 如果未修改,自
  • Unless-Modified-Since 除非修改自

It is better to compress all your js/css files into one giant file for the entire website, then it is to dynamically serve just the required js/css files for the page. 最好将所有js / css文件压缩为整个网站的一个巨型文件,然后动态地仅提供页面所需的js / css文件。 Browsers can cache the one giant file and then never have to worry about downloading css/js from your site again. 浏览器可以缓存一个巨型文件,然后不必担心再次从您的站点下载css / js。

You can use an HTTP request combiner to specify a list of common css files which should be delivered as a single minified css file to the browser. 您可以使用HTTP请求组合器来指定常见css文件的列表,这些列表应作为单个缩小的css文件传送到浏览器。 This will reduce the size as well as bring the number of requests down significantly. 这将减小大小,并显着减少请求数量。

A good example of this working in a .Net context: http://www.codeproject.com/KB/aspnet/HttpCombine.aspx 在.Net上下文中工作的一个很好的例子: http : //www.codeproject.com/KB/aspnet/HttpCombine.aspx

I wrote some blog posts about this. 我为此写了一些博客文章。 See Supercharging CSS in PHP . 请参阅在PHP中对CSS进行增压 Even though it is written for PHP the principles are universal. 即使它是为PHP编写的,其原理也是通用的。

Basically your goal is to reduce external HTTP requests. 基本上,您的目标是减少外部HTTP请求。 You can do this with a combination of combining your CSS files and/or versioning your CSS files and using a far future Expires header. 您可以通过组合CSS文件和/或对CSS文件进行版本控制以及使用将来的Expires标头来实现此目的。

Lastly, you should always GZip CSS (and Javascript) if the client accepts it. 最后,如果客户端接受,则应始终使用GZip CSS(和Javascript)。

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

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