PHP in CSS

  • 2,898
    Posts
    21
    Years
    • Seen Jul 27, 2024
    This is probably a n00bish question but anyways; is their anyway to imbed php code in a .css file without having to rename the extension and leaving as a .css extension.
     
    Just a question, why do you need to do that? You can't without changing the extention, but there may be a workaround depending on what you wanna do.
     
    I do not believe there's anyway to put PHP into a CSS file.

    You can always just put the code into your <head> of the actual page though~
     
    Hmm...I'm not at all sure about this but...
    in your .htaccess file (create if you have none) add this line
    Code:
    AddType application/x-httpd-php .css
    AFAIK it will still be treated as a css file..

    Or alternatively, change the link to style.php and do it in there.
     
    Why exactly would you want PHP code in a .css file?
     
    If you include it into a php file, and don't use it just... alone, just use the normal php code.
     
    I assume you'd want to do something like if (condition) { this css } else { other css }?

    Just use a php file for it. Put the CSS in <style type="text/css"> tags, and include() the php file into your page.
     
    well you could google it because i found a website that does all that but i frogot it XD
     
    Dragonfree said:
    Why exactly would you want PHP code in a .css file?
    There are a lot of uses for it~ For example, you can change elements depending on what browser a visitor is using.

    Or let them change the CSS using certain coding.
     
    Paul said:
    There are a lot of uses for it~ For example, you can change elements depending on what browser a visitor is using.

    Or let them change the CSS using certain coding.
    Eww, browser detect. :x

    If you want to change stylesheets or something like that, you can just include different stylesheets depending on a variable...
     
    If you're letting the user change CSS then it's more likely to be Javascript than PHP, since JS is a CSSL and has features that allow you to change CSS on the fly, while PHP is a SSSL and you'd need to rebuild the entire CSS each time.
     
    Or he's lazy and wanted to do like

    PHP:
    <?
    // Declare variables
    $text = "515C89";
    $link = "263059";
    $background = "B7BDD6";
    $border = "D3D7E6";
    ?>
    <style type="text/css">
    <!--
    <? print "
    a { color: #$link; text-decoration: none; }
    a:link { color: #$link; text-decoration: none; }
    a:visited { color: #$link; text-decoration: none; }
    a:active { color: #$link; text-decoration: none; }
    a:hover { color: #$link; text-decoration: none; }
    table { color: #$text; font-family: Tahoma; font-size: 10pt; background-color: #$background; }
    body { color: #$text; font-family: Tahoma; font-size: 10pt; background-color: #$background; }
    textarea { border: 1pt solid #$link; color: #$text; font-family: Tahoma; font-size: 9pt; background-color: #$background; }
    .big { color: #$text; font-family: Tahoma; font-size: 14pt; }
    .strike { color: #$text; font-family: Tahoma; font-size: 10pt; text-decoration: line-through; }
    .center { text-align: center; }
    .left { text-align: left; }
    "; ?>
    -->
    </style>
    (copied from JKN)
     
    Which is what I said, just <style type="text/css"> and include().
     
    Back
    Top