我正在尝试在我的 .css 中使用 php 变量。 我已将 style.css 更改为 style.php 并在其中添加了下一个代码:
<?php header("Content-type: text/css");
$color[0]='#ff0000';
$color[1]='#00ff00';
$color[2]='#0000ff';
$i=rand(0,2);
?>
然后我尝试在 css 属性中使用这种颜色。像这样:
background-color: "<?=$color[$i]?>";
当我试着看看会发生什么时……什么也没有发生。如果我在 firefox 检查器上看到我的代码似乎没有从 php 更改为 html。
有什么想法吗?
最佳答案
在主体上使用类:
<?php
$class[0]='red';
$class[1]='lime';
$class[2]='blue';
$i=rand(0,2);
?>
<body class="<?php echo $class[$i]; ?>">
//The result
<body class="red"></body>
<body class="lime"></body>
<body class="blue"></body>
在 CSS 文件上使用继承:
.red .my-another-class{
background-color: red;
}
.lime .my-another-class{
background-color: lime;
}
.blue .my-another-class{
background-color: blue;
}
关于css中的php变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503733/