• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

PHP in CSS

bgt

R A W R
  • 2,899
    Posts
    20
    Years
    • Age 15
    • Seen Jun 21, 2011
    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.
     
  • 12,504
    Posts
    20
    Years
    • Seen Mar 13, 2022
    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~
     

    PD Wooper

    .: υвεʀ ωοορεʀ :.
  • 213
    Posts
    19
    Years
    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.
     

    aRedMoon

    Wait for me outside the lines
  • 11,127
    Posts
    20
    Years
    If you include it into a php file, and don't use it just... alone, just use the normal php code.
     

    Shining Arcanine

    Senior Super Moderator
  • 721
    Posts
    20
    Years
    PD Wooper is right. That is how you would do it if your web host is using an Apache server and allows use of .htaccess.
     

    Geometric-sama

    The Manly Man of Steel
  • 11,440
    Posts
    20
    Years
    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.
     

    Bowsa

    momderator
  • 21
    Posts
    18
    Years
    • Seen Jul 5, 2007
    well you could google it because i found a website that does all that but i frogot it XD
     
  • 12,504
    Posts
    20
    Years
    • Seen Mar 13, 2022
    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.
     

    Dragonfree

    Teh Spwriter. :3
  • 1,290
    Posts
    20
    Years
    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...
     

    Geometric-sama

    The Manly Man of Steel
  • 11,440
    Posts
    20
    Years
    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.
     

    aRedMoon

    Wait for me outside the lines
  • 11,127
    Posts
    20
    Years
    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)
     
    Back
    Top