PHP文本变量更改颜色

时间:2018-06-28 09:44:43

标签: html css


如何解决$ var($ a $ b和$ c)中的变量为红色而其他文本(这是测试,为)为普通黑色。
谢谢您的回答!

$a = 125;
$b = "My text";
$c = "is";

$var = "This is test $b is $c $a.";
echo '<div class="test">', $var,'</div>';

<style>
.test {
  color: #000000;
  z-index: 8;
  display: block;
  font-weight: normal;
  position:relative;
  left: -1255px;
  top: -345px;
  font-family: arial;
  font-size: 30px;
  max-width: 1450px;
}
</style>

2 个答案:

答案 0 :(得分:2)

您可以将这些变量包装在元素中并设置其样式。如果您只想要颜色,则该元素可能只是<span>,或者如果我希望强调这些值在语义上有意义,则(我更喜欢)像<em>这样的更具描述性的元素。无论选择哪个,一旦输出元素,就可以照常对其进行样式设置:

$a = 125;
$b = "My text";
$c = "is";

$var = "This is test <em>$b</em> is <em>$c</em> <em>$a</em>.";
echo '<div class="test">'.$var.'</div>';

请注意,样式表中添加了.test em规则,以解决<em> div内的所有(且仅).test元素。

<style>
.test {
  color: #000000;
  z-index: 8;
  display: block;
  font-weight: normal;
  position:relative;
  left: -1255px;
  top: -345px;
  font-family: arial;
  font-size: 30px;
  max-width: 1450px;
}
.test em {
  color: red;
}
</style>

有关此元素的详细信息以及(仅)视觉和语义标记之间的一般区别,请参见Mozilla上的<em>: The Emphasis element

答案 1 :(得分:-1)

$a = 125;
$b = "My text";
$c = "is";

$var = "This is test $b is $c $a.";
echo '<div class="test">', $var,'</div>';

echo '<style>
.test {
  color: #000000;
  z-index: 8;
  display: block;
  font-weight: normal;
  position:relative;
  left: -1255px;
  top: -345px;
  font-family: arial;
  font-size: 30px;
  max-width: 1450px;
}
</style>';
相关问题