使用 php 更改 css 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10950734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change css value with php
提问by Ronny Kibet
how can I change css of a div displaying some text on my home page from the admin are. I want when I enter a color code in my plugin admin page, the code is updated in the css file. This is a common thing, but can't get a grasp of it.
如何从管理员更改在我的主页上显示一些文本的 div 的 css。我希望当我在插件管理页面中输入颜色代码时,代码会在 css 文件中更新。这是很常见的事情,但无法掌握。
so here is the css of my div.
所以这是我的 div 的 css。
#div{
background: #0000;
}
and here is the php code for my admin section.
这是我的管理部分的 php 代码。
<?php
Background Color: <input type="text" size="20" name="background"
value="<?php get_option('backgroundchanges');?>"/>
so basically I want the value that I enter in the input to be reflected in the css file.
所以基本上我希望我在输入中输入的值反映在 css 文件中。
thanks for any help.
谢谢你的帮助。
regards, :)
问候, :)
-ROn.
-罗恩。
回答by Steve
You should actually not use a .css file and instead use a .php file acting as a .css file. Then you can just set the new color to a variable in the .php css file.
您实际上不应使用 .css 文件,而应使用充当 .css 文件的 .php 文件。然后您可以将新颜色设置为 .php css 文件中的变量。
回答by Purpletoucan
Putting out a CSS file through PHP will add overhead to the server as you will have dynamic HTML and CSS to create. Given the 'cascading' nature of CSS, any inline style served in the HTML pages will override the CSS, so it's quite likely that you can simply add some inline styles into the pages already being served by PHP. Another option is to update the CSS using Javascript, here is one way to do this, but I don't believe this works in all browsers (I use this in Webkit on iOS):-
通过 PHP 发布 CSS 文件会增加服务器的开销,因为您将需要创建动态 HTML 和 CSS。鉴于 CSS 的“级联”性质,HTML 页面中提供的任何内联样式都将覆盖 CSS,因此您很可能只需将一些内联样式添加到已由 PHP 提供的页面中即可。另一种选择是使用 Javascript 更新 CSS,这是一种方法,但我不相信这适用于所有浏览器(我在 iOS 上的 Webkit 中使用它):-
var cssStyle = getCSSRule('#styleName');
cssStyle.style.background = redfinedColour;
...using getCSSRule from here:-
...从这里使用 getCSSRule :-
http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
回答by Lawrence Cherone
How I do it is have a content loader controller or script that will load images/css/js and pass them to the browser, you can also control the cache and create javascript dynamically, like the image lists in tinyMCE ect:
我的做法是有一个内容加载器控制器或脚本来加载图像/css/js并将它们传递给浏览器,您还可以控制缓存并动态创建javascript,例如tinyMCE中的图像列表等:
Heres an stripped down example but you get the idea.
这是一个精简的例子,但你明白了。
<?php
$filename = value passed from url
if(file_exists(SITE_ROOT.'/templates/'.$theme.'/css/'.basename($filename))==true){
$fullpath=SITE_ROOT.'/templates/'.$theme.'/css/'.basename($filename);
header("Content-type: text/css");
$search=array('{SITE_URL}','{BACKGROUND}');
$replace=array('http://example.com',get_option('backgroundchanges'));
echo str_replace($search,$replace,file_get_contents($fullpath));
die;
}else{
die('CSS file does not exist!');
}
?>
Then have you css files with place holders replaced by your parameters:
然后将占位符替换为参数的 css 文件:
#div{
background: #{BACKGROUND} url('{SITE_URL}/stripe.png') repeat;
}

