Should I combine .css files with php to reduce http requests?
I was thinking 开发者_如何学Ctoday, as I linked a webpage to 3 external .css files:
"This is too many HTTP requests!" I got to wondering if I should be using
php to combine these files into one? Or should I just import all three into the
<head>
with <style>
tags? I found one relevant link on the interwebs:
Suture CSS or JavaScript Files to Reduce HTTP Requests but I don't think it gives an adequate discussion.
note: I am tagging this for javascript as well because I believe it also applies to .js files so please make note if your answer applies to only css or javascript
I think it's not necessary. The browser will download both files just once. If you add them to the style tag on the page, it will have to download the same content everytime this page is loaded.
Unless your page/site has lots of access per second, you won't need it.
That's what I think...
I got to wondering if I should be using php to combine these files into one?
Often yes, not only because of the number of HTTP requests (which will usually occur only once anyway) but also because Internet Explorer can't deal with more than 31 external style sheets on the same page.
Preferably do it in a cached way so you don't have to run PHP on every CSS request. This question has a number of pointers to excellent resources on the issue.
Or should I just import all three into the with tags?
No: It makes the styles un-cacheable by the browser, and increases the size of every request.
I use a combination of methods so that I can serve only one css and as few js files as possible.
By using media queries it's possible to combine print and screen styles into one page, and by using body class selectors you can add ie styles inline without using hacks.
For example, in main.css
@media screen {
p { [styles] }
...
}
@media print {
p { [styles] }
...
}
This article gives a rundown of body class selectors:
http://misteroneill.com/improved-internet-explorer-targeting-through-body-classes/
精彩评论