I have CSS file and I want to refer some image paths in that files in PHP varaible format. Then I refer that css file inside a html file. Following are 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 me how to do this ?
If you're able to rename your CSS file "layout.php", there's no need for all of these workarounds:
Your layout.php file would look like:
<?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 would look like:
<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?
Perhaps the return value of base_url()
does not end in the path separator.
With that in mind, try this:
@import url("<?php echo base_url().'/public/';?>css/layout.css");
(Notice the slash before "public")
@import
is corrector
You also view the CSS via your browser to identify whether the contents are being correctly built. If you see <?php
inside the response, you'll need to make Apache treat the CSS file as if it was PHP.
You can add something similar to the following into your .htaccess file:
<FilesMatch "\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
You should ensure that the "mod_headers" Apache module is enabled to allow the use of the Header
directive.
Although, personally I would rename such dynamic stylesheets to have a .php.css extension. This will have no effect, but then Apache can be configured to only pass the dynamic stylesheets to the PHP preprocessor.
<FilesMatch "\.php\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With