In the context of improving overall site performance (downloading and rendering speed) there appears to be a contradiction between the following two best practices:
Only bring down the CSS that you need for the page being viewed. (Because too many CSS rules cause slow rendering)
Always minify CSS and combine it into one file. (Because more requests means slower page load)
Now say I decide to follow rule 1.
The following issues come up:
What if 2 pages share 1 set of CSS rules?
In this case, I need to put those rules in a separate file and reference that file from both pages.
However, if I begin to have a lot of these "shared rules", I might end up referencing a lot of separate files from each page, and thus, breaking rule 2.
For example, page A might depend on CSS 1 and 2, while pages B and C both depend on CSS 2 and page D depends on CSS 1.
In this scenario, it's impossible to have exactly one CSS per page or even multiple CSS's per page, since some pages will need to share some CSS files with other pages.
But can't we solve this problem by combining all the CSS for each page into a separate per-page CSS file?
We could, but this would create other problems.
If two pages shared one fragment of CSS, even if we compress the hell out of it, we're still going to download that fragment repeatedly, every time we request a page who's CSS contains that fragment.
Because we've compressed the CSS by page, we've allowed redundancies to occur where a CSS fragment is shared by two or more pages.
Browser caching does us no good here because to the browser, each CSS file has a different filename, and is thus a separate file, even if some of them contain content that is the same.
So which rule should we break?
The one I'd cross off is:
1. You should only bring down the CSS that you need for the page being viewed.
I think it's much simpler and more practical to minify/combine the hell out of all the CSS for my site, and bring it down in one go.
As for the performance problems this might create, I think they're lessened by the following facts:
Modern browsers are getting faster at processing CSS rules, so pretty soon it won't matter if you have a lot of rules in memory that aren't being used.
Having all your CSS cached will improve speed a lot more than any improvement you'd gain from leaving out unnecessary rules, which are going to get loaded anyway, when the user browses to the pages that require those rules.
Am I right here?