Jump to content

Is it good practice to make PHP and Javascript based CSS files?


Recommended Posts

So yea, we mostly include our css via <link rel='stylesheet' type='text/css' href='style.css'/>, but I just found that I can make CSS work from an included PHP file in <style></style> tag printed out dynamically based on any settings I make. But the downside is that this ends up scattered around the HTML output. Does this reduce the performance in any way?

 

I wish we could put PHP in css files.. :/

 

Please note that I'm new to programming.

But the downside is that this ends up scattered around the HTML output. Does this reduce the performance in any way?

 

No, but it's not a good way of organising CSS.

 

I wish we could put PHP in css files.. :/

 

You can, but it's not usually needed.

Its a very bad idea to just place your CSS directly in a style element on your page. It does make each of your pages load faster, but when you got multiple pages using the same CSS, it quickly starts to load slower than it would with External CSS files. That's because these External files are cached by the browser.

 

If you just got a single page, then its fine to just throw in your CSS in a style element on the page, and save the time involved in making another HTTP Request.

 

You can however minimize your CSS files dynamically on the server-side, removing all white space and linebreaks. This can sometimes provide a huge performance increase, and still maintain readability in the raw files.

 

Obviously it takes a little server CPU to process the files, so you shouldn't do this if your server doesn't have the resources. But for most sites, this won't be an issue, not even on shared hosts.

 

You basically just need to deliver the correct Content-type, in this case text/css for CSS files. But you would also want to control the caching of the files manually, either based on the last-edited tag of the files themselves, or from a timestamp saved in your database. You could even save a minimized version of your CSS and deliver this directly, which would save you CPU on the minimizing process.

 

Without the caching, there's little point using PHP to minimize the files, since the browser would just re-download the files on each request, rather than using the version from the cache.

 

The same can be done to markup such as HTML, but this would likely require that you output the entire page at once, similar to whats done in python. For example, if you got the entire page in a variable, then its easy to remove whitespace and linebreaks where you don't want them, and increase the speed for your site even more. Can be done in PHP as well, but you would most likely want a better approach, than just escaping in and out of your HTML/Markup.

I wish we could put PHP in css files.. :/

I had the same issue with me once as i was supposed to use dynamic css value but i was unable to put php code in the .css file as it was not working. Instead i put the code in .css and renamed it with style.php and linked it as css. It was working

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.