Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PHP Tutorial] Using PHP within CSS style sheets
12-28-2010, 01:01 AM
Post: #1
[PHP Tutorial] Using PHP within CSS style sheets
PHP can be extremely helpful for having dynamic style sheets without having multiple style sheet files. To accomplish this, we need to do two simple tasks.
First, reference your style sheet very much like you normally would:
Code:
<link rel="stylesheet" href="myStyle.php" media="screen">

Now, create a new php file named myStyle.php and put following in it:

Code:
<?php
header("Content-type: text/css"); //This is a necessity for the file to be considered a style sheet! Note that it much be the first line of output, before any other displayed text/code!

$bgcolor = "#fff";
$fcolor = "#ccc";
$lcolor = "#c00";
?>

body
{
    margin: 4px;
    color: <?php echo $bgcolor; ?>;
    background: <?php echo $fcolor; ?>;
}
a:link
{
    color: <?php echo $lcolor; ?>;
}
a:visited
{
    color: #cee;
}

That's it! Notice that you can use either php or directly enter the color and such. Any php code can be used the same way you would on any other php page.

Free image hosting at http://myph.us.
Find all posts by this user
Quote this message in a reply
12-28-2010, 05:15 AM
Post: #2
RE: [PHP Tutorial] Using PHP within CSS style sheets
I knew that PHP can generate HTML and stuff, but i haven't seen it is used to generate CSS !!!
Usually i thought that you need to make a few CSS files with different styles included and switch between them over PHP
and with that effect you can change appearance of your site.
ajcis55, really good, this can be useful. For example if you want to make a fully customizable appearance of the site, so
users can change everything - colors etc. Connection with the database could be needed also.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
06-02-2011, 11:38 AM
Post: #3
RE: [PHP Tutorial] Using PHP within CSS style sheets
(12-28-2010 01:01 AM)ajcis55 Wrote:  PHP can be extremely helpful for having dynamic style sheets without having multiple style sheet files. To accomplish this, we need to do two simple tasks.
First, reference your style sheet very much like you normally would:
Code:
<link rel="stylesheet" href="myStyle.php" media="screen">

Now, create a new php file named myStyle.php and put following in it:

Code:
<?php
header("Content-type: text/css"); //This is a necessity for the file to be considered a style sheet! Note that it much be the first line of output, before any other displayed text/code!

$bgcolor = "#fff";
$fcolor = "#ccc";
$lcolor = "#c00";
?>

body
{
    margin: 4px;
    color: <?php echo $bgcolor; ?>;
    background: <?php echo $fcolor; ?>;
}
a:link
{
    color: <?php echo $lcolor; ?>;
}
a:visited
{
    color: #cee;
}

That's it! Notice that you can use either php or directly enter the color and such. Any php code can be used the same way you would on any other php page.
Find all posts by this user
Quote this message in a reply
05-31-2012, 07:49 PM
Post: #4
RE: [PHP Tutorial] Using PHP within CSS style sheets
This is acctually one of the ways e.g. vBulletin work, or places where users may change the design of e.g. their profile.
Integrate this with mysql and it'll acctually be very useful.
Find all posts by this user
Quote this message in a reply
06-02-2012, 03:23 PM
Post: #5
RE: [PHP Tutorial] Using PHP within CSS style sheets
Yeah, I knew this trick with dynamically definition of a header as a css, and this combination Smile Extremely useful if you want to keep your site colorfull and dynamic! Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: