How to use PHP inside css file

I have a CSS file and I want to refer to some image paths in these files in varaible PHP format. Then I refer to this css file inside the html file. Below is my file

CSS file

<? header ("Content-type: text/css");?> body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000; background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0} 

HTML file

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="css/layout.css" media="screen"> </head> 

Other things. Can you explain to me how to do this?

+7
html css php
source share
4 answers

If you can rename your CSS file "layout.php", there is no need for all these workarounds:

Your layout.php file will look like this:

 <?php header("Content-type: text/css; charset: UTF-8"); ?> body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000; background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0} 

Your HTML files will look like this:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="css/layout.php" media="screen"> </head> 

This question is very similar: Include: css with php file extension?

+10
source share

I believe the problem is that the .css file will not be interpreted as PHP, and therefore your code in the file will not be executed. If you included it as a PHP file, your code must be executed and your values ​​must be filled.

[Edit] This seems to be a way to do this, as noted by the answer, which was related to the comment on the original post here .

+1
source share

Perhaps the return value of base_url() does not end with a path separator.

With this in mind, try the following:

 @import url("<?php echo base_url().'/public/';?>css/layout.css"); 

(Note the slash before "public")

  • Check the source of the page through your browser "source of view" or similar, and check the path in @import

or

  • Use a query logger similar to the Chrome network devtools "network network" tab to find out which URL is trying to load your imported CSS file from your browser.

You also look at CSS through your browser to determine if the content is built correctly. If you see <?php inside the answer, you need to force Apache to treat the CSS file as if it were PHP.

You can add something like the following to your .htaccess file:

 <FilesMatch "\.css$"> SetHandler application/x-httpd-php Header set Content-type "text/css" </FilesMatch> 

You must ensure that the Apache module "mod_headers" is enabled to enable the use of the Header directive.

Although, personally, I would rename such dynamic stylesheets to have the extension .php.css. This will have no effect, but then Apache can only be configured to pass dynamic stylesheets to the PHP preprocessor.

 <FilesMatch "\.php\.css$"> SetHandler application/x-httpd-php Header set Content-type "text/css" </FilesMatch> 
0
source share

This is easy if you have some knowledge of rewriting URLs.

Write the .htaccess file in the root directory, it will look something like this:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^customized\.css$ css\_generator\.php [L] </IfModule> 

Now just create the css_generator.php file with the contents:

 <?php header('Content-type: text/css; charset: UTF-8'); ?> body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000; background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0} 

Your html should look like this:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="customized.css" media="screen"> </head> 

Understanding what just happened.

  • When the page loads, when your browser loads customized.css, it will be redirected to css_generator.php
  • css_generator.php content will also be available as yoursite.com/customized.css

Hope this helps

0
source share

All Articles