登录 | 注册 | FAQ
Anonymous

Can I use PHP in CSS style file?

+ 发表回复

7 篇帖子 分页: 1 / 1


Can I use PHP in CSS style file?

zjing129 » 周四 5月 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.
头像

zjing129

  • 帖子: 4
  • 注册: 周四 5月 10, 2012 1:00 am

Re: Can I use PHP in CSS style file?

XainPro » 周四 5月 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 ...
代码: 全选
<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>
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm

Can I use PHP in CSS style file?

jin1234i » 周三 5月 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
头像

jin1234i

  • 帖子: 1
  • 注册: 周四 5月 17, 2012 7:20 am

Re: Can I use PHP in CSS style file?

XainPro » 周三 5月 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 :)
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm

Re: Can I use PHP in CSS style file?

webbolato » 周四 2月 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/
头像

webbolato

  • 帖子: 1
  • 注册: 周四 2月 07, 2013 12:55 pm

Re: Can I use PHP in CSS style file?

comport9 » 周四 2月 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.
头像

comport9

  • 帖子: 28
  • 注册: 周日 11月 04, 2012 5:35 pm

Re: Can I use PHP in CSS style file?

RyanKnol » 周四 7月 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/
头像

RyanKnol

  • 帖子: 1
  • 注册: 周四 7月 30, 2015 10:33 pm


+ 发表回复

分页: 1 / 1