BLOG ESPERANCA

css 파일에서 php 변수를 사용하기 본문

Program/PHP

css 파일에서 php 변수를 사용하기

yobuce 2010. 11. 26. 09:54

http://css-tricks.com/css-variables-with-php/

Style.php

Instead of using the .css file extension, use .php

<link rel='stylesheet' type='text/css' href='css/style.php' />

 

 

Content-type

At the top of your new style.php file set the Content-type back to CSS: 

<?php
    header("Content-type: text/css; charset: UTF-8");
?>

 

Setup variables

Now you can set up variables for whatever you like: 

 

<?php
    header("Content-type: text/css; charset: UTF-8");

   $brandColor = "#990000";
   $linkColor = "#555555";
   $CDNURL = "http://cdn.blahblah.net";
?>

 

Use variables

Below all that PHP stuff, you can just commence regular CSS writing, only you can intermix some PHP to spit out those variables

#header {
   background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
  color: <?php echo $linkColor; ?>;
}

...

ul#main-nav li a {
  color: <?php echo $linkColor; ?>;
}