Search code examples
phpcssoptimizationinlinehead

PHP CSS - faster/good practice to load external file or echo into head?


I'd like to know if it's better to

A) include external CSS files using <link> or

B) echo/flush/print the CSS directly into the <head>.

Personally, I like option B, since it allows

  • removing comments / minifying
  • using css placed above the root directory
  • compressing multiple css files = less HTTP requests

Before I put this into practice, is there a big reason I should(n't)?


Solution

  • Option A will give you an extra HTTP request, but the browser keeps the CSS file in a cache so it's not a big deal.

    On the other hand, even if option B will save you an HTTP request, your HTML page will be considerably bigger and the browser will not be able to cache your HTML page if there's frequently changes on it. So you server will have to handle more data transfer.

    For pages where the content will be very rarely changed, use option B. Otherwise, use option A.