-2

This is what I'm trying to do: I have this website with lots of pages. All of them have something similar to this:

<head>
 <link rel="stylesheet" type="text/css" href="img_css/style.css"  media="all" />
</head>

But I want my css rules to be inline. Like this:

<style>
 div{color: blue;} 
 p{padding: 5px;}
 /* ect... */
</style>

Of course this is not recommended due to maintenance difficulties. But what if I put all css rules in a single php file and do an include on every page? Like this:

<head>    
  <style type="text/css">
   <?php include_once "img_css/new_style.php"; ?>
  </style>    
</head> 

I need to know if there's anything wrong with this method. Can I really do this? Do you guys have any advice or warning about this kind of thing? Do you approve?
Thanks in advance for any reply!

Peristilo peris
  • 105
  • 1
  • 7

1 Answers1

1

Yes you could do that but the real question is should you do it. In most cases you shouldn't. The major problem with embedding your CSS in the page, as was mentioned in the comments, is that it can't be cached for quick usage in future page loads. If your HTML is not served with compression, it could add significantly to the page size; if it is served with compression it will add to the overhead of compressing the page.

There are a few factors that combined might make embedding it a good choice:

  • It is a relatively small amount of code
  • It contains styles that aren't used anywhere else on the site
  • Related to the size of the code, embedding it will be quicker than making another request to the server for a cacheable external file.
Useless Code
  • 12,123
  • 5
  • 35
  • 40
  • Thank you! My css is not that big (16kb). And I can improve the code so that it will be specific to the page and not used anywhere else. I only need to apply this method to 2 pages only. So maintenance won't be an issue. What do you think? – Peristilo peris Aug 22 '16 at 23:16