Ivan Ivković Posted January 24, 2012 Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255647-is-it-good-practice-to-make-php-and-javascript-based-css-files/ Share on other sites More sharing options...
trq Posted January 24, 2012 Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255647-is-it-good-practice-to-make-php-and-javascript-based-css-files/#findComment-1310567 Share on other sites More sharing options...
SuperBlue Posted January 27, 2012 Share Posted January 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255647-is-it-good-practice-to-make-php-and-javascript-based-css-files/#findComment-1311795 Share on other sites More sharing options...
fredrikrob Posted February 2, 2012 Share Posted February 2, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255647-is-it-good-practice-to-make-php-and-javascript-based-css-files/#findComment-1313709 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.