Page 1 of 1

External CSS File That Contains PHP Code

Posted: Sun Oct 17, 2010 7:09 pm
by JackD
We have a some web pages at the main level and some in a sub-directory. To allow us to use the same images and files, we have a standard.php file that all web pages invoke. As long as our CSS code is internal to this file, everything works correctly. As soon as we move the CSS code to an external file, the browsers appear to quit processing the external CSS file as soon as they encounter '<?php'. Is it possible to have php code in an external CSS file? Here is the code.

In standard.php, this is the complete code up to and including the link to the external CSS file:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<head>
<?php
global $imagedir,$maindir,$cachedir;
if(file_exists('images'))
 { $imagedir='images';
   $maindir=''; 
   $cachedir='cache';
 }
 else 
 { $imagedir='../images';
   $maindir='../';
   $cachdir='../cache';
 }
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books For Less of Georgia</title>
<?php
echo '<link rel="SHORTCUT ICON" href="' . $imagedir . '/favicon.ico"/>';
echo '<script src="'.$maindir.'jquery2.js" type="text/javascript"></script>';
?>
<?php
echo '<link rel="stylesheet" type="text/css" href="'.$maindir.'standard.css" />';
?>
Here is the code in the external CSS file:

Code: Select all

a.white {color:white;}
a.blue {color:blue;}
a:link { color: #0000CC; text-decoration: none; }
a:visited { color: #FF0000; text-decoration: none; }
a:active { color: #0000CC; text-decoration: none; }
a:hover { color: #FF0000; text-decoration: underline; }

.twoColLiqLtHdr #sidebar1 {
    float: left;
    width: 200px; /* top and bottom padding create visual space within this div  */
<?php
    echo 'background-image: url(' . $imagedir . '/red1%20background.jpg);';
?>    
    padding-top: 0px;
    padding-right: 6px;
    padding-bottom: 0px;
    padding-left: 6px;
}

.twoColLiqLtHdr #header {
    text-align: center;
    background-color: #C00;
    border: thick none #FF0;
    padding-top: 0;
    padding-right: 0px;
    padding-bottom: 0;
    padding-left: 0px;
<?php
    echo 'background-image: url(' . $imagedir . '/BfL-Banner.jpg);';
?>    
}
.twoColLiqLtHdr #footer { 
    padding: 0 0px; 
<?php
    echo 'background-image: url('.$imagedir.'/red1%20background.jpg);';
?>
} 
.twoColLiqLtHdr #footer p {
    margin: 0; 
<?php
    echo 'background-image: url('.$imagedir.'/red1%20background.jpg);';
?>
    width: 813px;
    padding-top: 0px;
    padding-right: 0;
    padding-bottom: 0px;
    padding-left: 0px;
    height: 70px;
    float: none;
}
Is there a problem using php in external CSS files or should we be looking for a bug in our CSS code? Again, when an internal style, it works. It was only when we moved it to an external file the failure occurs.

Re: External CSS File That Contains PHP Code

Posted: Sun Oct 17, 2010 7:57 pm
by Weirdan
By default php does not process files with .css extensions. You need to configure your webserver to process them with php. How to do that depends on the particular webserver used.

Re: External CSS File That Contains PHP Code

Posted: Mon Oct 18, 2010 9:47 am
by josh
Just name it as styles.css.php