来自php变量的十六进制颜色不会设置为背景

时间:2014-01-07 19:42:00

标签: php css

因此用户可以选择拾取进入数据库的十六进制颜色。 PHP脚本获得该值,并开始出现问题。背景只是空白。这是代码:

  $hex=78FFDB; 
    if ($hex != NULL or $hex != ""){
    $body="<body style='background:#$hex;'></body>";
    $stat="#$hex runs";
    }
echo $body;

4 个答案:

答案 0 :(得分:1)

我认为问题是你的十六进制值不在字符串中,所以PHP试图将这些字母解释为数字,并且它在那里失败。试试这个

<?php
$hex="78FFDB"; 
    if ($hex != NULL or $hex != ""){
    $body="<body style='background:#$hex;'></body>";
    $stat="#$hex runs";
    }
echo $body;
?>

答案 1 :(得分:0)

背景中没有内容,因此没有任何内容可以显示背景。 尝试添加&nbsp; before </body>,看看它是否有效。另外,请确保$ hex =是引号中的字符串。

答案 2 :(得分:0)

首先,您的$hex需要报价。

<?php
$hex="78FFDB"; 
if ($hex != NULL or $hex != ""){
    $body="<body style='background:#$hex;'></body>";
    $stat="#$hex runs";
}
echo $body;
?>

然后您的<body>可能不可见,因为没有内容所以您应该首先尝试添加一些内容

$body="<body style='background:#$hex;'>some content</body>";

并查看内容是否有背景颜色。

如果您需要全屏幕背景,请使用此css:

显示内容
html, body {
    height: 100%;
}

答案 3 :(得分:0)

我举一个示例将以下代码保存为您的目录中的index.php。如果您使用wamp服务器,请将该index.php文件放在以下目录 C:\ wamp \ www \ hex \ index中。 php 并从服务器运行它。 这里hex是你的文件夹名称。希望它能在你的系统上运行。

<?php  $hex="78FFDB";
    if ($hex != NULL or $hex != ""){
    $body="<body style='background:#$hex;'></body>";
    $stat="#$hex runs";
    }
echo $body;
?>

<!DOCTYPE html>
<html>
<head>
<title>
    Selectors
</title>
<style>

div {
   width:1000px;
   border:2px solid #000;
}
 .divone{
      background-image:linear-gradient(to right,red 0%,gray 100%);
 }
 .divone ~ div{
      background-image:radial-gradient(circle at 50% 50%,red 0%,gray 100%);
 }
</style>
</head>
<body>
    <div class="fancy bold">
        This is the type selectors used in css.
    </div>

   <div class="divone">
                                                                                                                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    </div>
          <div><h1>Hello World!</h1></div>
       <div>
                                                                                                                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    </div>
</body>
</html>

enter image description here