External CSS File That Contains PHP Code

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

External CSS File That Contains PHP Code

Post 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.
Last edited by Benjamin on Sun Oct 17, 2010 7:47 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: External CSS File That Contains PHP Code

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: External CSS File That Contains PHP Code

Post by josh »

Just name it as styles.css.php
Post Reply