在 PHP 中设置背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3412194/
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
Set background color in PHP?
提问by ion
What is the easiest way to set background color in PHP ?
在 PHP 中设置背景颜色的最简单方法是什么?
回答by user3055304
just insert the following line and use any color you like
只需插入以下行并使用您喜欢的任何颜色
echo "<body style='background-color:pink'>";
回答by Quentin
<?php
header('Content-Type: text/css');
?>
some selector {
background-color: <?php echo $my_colour_that_has_been_checked_to_be_a_safe_value; ?>;
}
回答by Jay
You must use CSS. Can't be done with PHP.
您必须使用 CSS。不能用 PHP 完成。
回答by Jeremy
CSS supports text input for colors (i.e. "black" = #000000 "white" = #ffffff) So I think the helpful solution we are looking for here is how can one have PHP take the output from an HTML form text input box and have it tell CSS to use this line of text for background color.
CSS 支持颜色的文本输入(即“黑色”=#000000“白色”=#ffffff)所以我认为我们在这里寻找的有用解决方案是如何让 PHP 从 HTML 表单文本输入框中获取输出并具有它告诉 CSS 使用这行文本作为背景颜色。
So that when a a user types "blue" into the text field titled "what is your favorite color", they are returned a page with a blue background, or whatever color they happen to type in so long as it is recognized by CSS.
因此,当用户在标题为“你最喜欢的颜色是什么”的文本字段中键入“蓝色”时,他们会返回一个带有蓝色背景的页面,或者只要 CSS 识别出他们碰巧键入的任何颜色。
I believe Dan is on the right track, but may need to elaborate for use PHP newbies, when I try this I am returned a green screen no matter what is typed in (I even set this up as an elseif to display a white background if no data is entered in the text field, still green?
我相信 Dan 是在正确的轨道上,但可能需要详细说明使用 PHP 新手,当我尝试这个时,无论输入什么,我都会返回一个绿屏(我什至将它设置为 elseif 以显示白色背景,如果文本字段中没有输入数据,还是绿色?
回答by code-zoop
You can use php in the style sheet. Just remember to set header("Content-type: text/css") in the style.php (or whatever then name is) file
您可以在样式表中使用 php。请记住在 style.php(或其他名称)文件中设置 header("Content-type: text/css")
回答by hollsk
This really depends on what you need to do. If you want to set a background colour on a page then you need to use CSS as per Jay's and David Dorward's answers.
这实际上取决于您需要做什么。如果要在页面上设置背景颜色,则需要按照 Jay 和 David Dorward 的回答使用 CSS。
If you are building an image with PHP then you can use the GD library to allocate colours yourself. I don't recommend this without thoroughly reading up on how to create images with GD. http://www.php.net/manual/en/function.imagecolorallocate.php
如果您使用 PHP 构建图像,那么您可以使用 GD 库自行分配颜色。如果没有彻底阅读如何使用 GD 创建图像,我不建议这样做。http://www.php.net/manual/en/function.imagecolorallocate.php
回答by Dan
Try this:
尝试这个:
<style type="text/css">
<?php include("bg-color.php") ?>
</style>
And bg-color.php can be something like:
而 bg-color.php 可以是这样的:
<?php
//Don't forget to sanitize the input
$colour = $_GET["colour"];
?>
body {
background-color: #<?php echo $colour ?>;
}
回答by Mike Johnson
You better use CSS for that, after all, this is what CSS is for. If you don't want to do that, go with Dorwand's answer.
你最好为此使用 CSS,毕竟这就是 CSS 的用途。如果您不想这样做,请使用 Dorwand 的答案。
回答by Centurion
I would recommend to use css, but php to use to set some class or id for the element, in order to make it generated dynamically.
我建议使用 css,但使用 php 为元素设置一些类或 id,以使其动态生成。