简体   繁体   中英

Should I combine .css files with php to reduce http requests?

I was thinking today, 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 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 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 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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