CSS样式表中的PHP变量 [英] PHP Variables in the CSS Stylesheet
问题描述
我正在寻找将PHP变量引入样式表(即CSS)。
我已经发现可以通过声明一个PHP页面作为样式表打印:
header('Content-Type:text / css');
在CSS页面顶部。
在这种情况下,PHP变量 $ css
将会' - webkit - '
,' - moz - '
,' - ms - '
或' - o - '
。
想要在CSS3之前回显它。
最初我是通过为每个单独的CSS文件实现这一点,但是这将更有效,并允许我通过真正的造型从数据库,如背景颜色和字体。
可能?如何?
示例作为CSS链接调用的PHP文件。
<?php
global $ css;
header('Content-Type:text / css');
?>
.wheel {
position:absolute; top:50%;左:50%; height:32px; width:32px; margin:-16px; <?php echo $ css;?> transition:opacity 0.3s;
}
.wheel li {
width:3px; height:9px; border-radius:2px; background:#555; <?php echo $ css;?>动画:加载1.2s无限;位置:绝对; <?php echo $ css;?> transform-origin:2px 16px; left:16px;不透明度:0; box-shadow:inset 0 0 2px rgba(255,255,255,0.4);
}
@<?php echo $ css;?>关键帧加载{0%{opacity:0.2;} 50%{opacity:0.9;} 100%{opacity:0.2 ;}}
with HTML:
<?php
header('Content-Type:text / css');
$ css = $ _GET ['css']; //或者你的初始化变量从...
?>
body {
<?= $ css?> border-radius:3px
}
但是,我不认为这对您的用例是必要的。在css文件中静态包含所有各种 - * -
选项并不罕见:
body {
-moz-border-radius:3px;
border-radius:3px;
}
只需添加所有有效选项,浏览器将确定哪些选项最有效它。这也意味着你避免浏览器嗅探的愚钝和容易出错的任务。
I am looking to introduce PHP variables to stylesheets (ie. CSS).
I have worked out that I can print a PHP page as a stylesheet by declaring:
header('Content-Type: text/css');
At the top of the CSS page.
However the variable I am passing is not displaying in the stylesheet.
In this case the PHP variable $css
will be '-webkit-'
, '-moz-'
, '-ms-'
, or '-o-'
.
And in the stylesheet I want to echo it in front of CSS3.
Originally I was achieving this by having a separate CSS file for each however this would be more efficient and allow me to pass genuine styling from the database, such as background-color and font.
Possible? How?
EXAMPLE PHP File called as a CSS link.
<?php
global $css;
header('Content-Type: text/css');
?>
.wheel {
position:absolute; top:50%; left:50%; height:32px; width:32px; margin:-16px; <?php echo $css;?>transition:opacity 0.3s;
}
.wheel li {
width:3px; height:9px; border-radius:2px; background:#555; <?php echo $css;?>animation:loading 1.2s infinite; position:absolute; <?php echo $css;?>transform-origin:2px 16px; left:16px; opacity:0; box-shadow:inset 0 0 2px rgba(255,255,255,0.4);
}
@<?php echo $css;?>keyframes loading { 0% {opacity:0.2;} 50% {opacity:0.9;} 100% {opacity:0.2;} }
You do this the same way you would with HTML:
<?php
header('Content-Type: text/css');
$css = $_GET['css']; // or wherever your're initializing the variable from...
?>
body {
<?= $css ?>border-radius: 3px
}
But, I don't think this is necessary for your use case. It's actually not uncommon to just statically include all the various -*-
options in a css file:
body {
-moz-border-radius: 3px;
border-radius: 3px;
}
Just add all effective options, and the browser will determine which are most effective for it. This also means you get to avoid the dull and error prone task of browser sniffing.
这篇关于CSS样式表中的PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!