Login | Register | FAQ
Anonymous

Can I use PHP in CSS style file?

+ Post a reply

7 posts Page 1 of 1


Can I use PHP in CSS style file?

by zjing129 » Thu May 10, 2012 1:11 am

For example, I would like to set a background image which changes with the day of the week, e.g., "pic1.png" for Monday, "pic2.png" for Tuesday, "pic3.png" for Wednesday, etc. It seems that the following command in CSS file does not work.

body{
background-image: url('pic<?php echo date('w');?>.png');
}


What is wrong? Thanks.
User avatar

zjing129

  • Posts: 4
  • Joined: Thu May 10, 2012 1:00 am

Re: Can I use PHP in CSS style file?

by XainPro » Thu May 10, 2012 6:16 pm

in external css file no you can not but in internal document or embended CSS yes you can...
place it in the head section of your document it should work ...
Code: Select all
<html>
<head>
<title>title</title>
<style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("<?php echo......etc?>");}
</style>
</head>
<body>
The Body
</body>

</html>
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm

Can I use PHP in CSS style file?

by jin1234i » Wed May 23, 2012 8:31 am

This means the style sheet can be renamed from style.css to style.php. An addition to CSS is pending that will permit the use of CSS variables.


Tømrerfirma
User avatar

jin1234i

  • Posts: 1
  • Joined: Thu May 17, 2012 7:20 am

Re: Can I use PHP in CSS style file?

by XainPro » Wed May 30, 2012 11:57 am

no if you use external CSS file then it could not have the PHP code in it but in internal style sheet you can use php code in it.
There could be a hack for this but in normL WAY NO YOU can not use style.php as a CSS file :)
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Can I use PHP in CSS style file?

by webbolato » Thu Feb 07, 2013 12:59 pm

Yes you absolutely can, with a little bit of .htaccess and header hackery :-)

How-to via the following links:
http://css-tricks.com/css-variables-with-php/
http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/
User avatar

webbolato

  • Posts: 1
  • Joined: Thu Feb 07, 2013 12:55 pm

Re: Can I use PHP in CSS style file?

by comport9 » Thu Feb 21, 2013 12:27 am

Take this for what it's worth, but I remember embedding PHP in my CSS file and it worked. But only for a SHORT amount of code. I don't know what was going on, but it seemed like it would read some of the code, but not the rest.

However, as Xian said, it's not necessary, and it's probably best just to put the PHP style part in the PHP/HTML file rather than the CSS.
User avatar

comport9

  • Posts: 28
  • Joined: Sun Nov 04, 2012 5:35 pm

Re: Can I use PHP in CSS style file?

by RyanKnol » Thu Jul 30, 2015 10:35 pm

how is this what google finds for me?

Anyone still looking can use THIS..

http://westcoastmarketing.ca/sass-is-fat-use-php-code-in-css-files/
User avatar

RyanKnol

  • Posts: 1
  • Joined: Thu Jul 30, 2015 10:33 pm


+ Post a reply

Page 1 of 1

cron