如何更改文本的背景颜色?

时间:2016-12-06 17:04:07

标签: html css

我正在尝试更改文字的背景颜色。这样做的最佳方法是什么?

我已将文本放入div类,然后将其引用到我的CSS并添加了background-color:xxxx;。



.text {
  background-colour: #33383b;
  box-sizing: border-box;
  padding: 30px 30px;
  text-colour: red;
}

<div class="text">
  <p>
    <h4>Lorem Ipsum</h4>
    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>
&#13;
&#13;
&#13;

不幸的是,这不起作用,任何我出错的提示?感谢

4 个答案:

答案 0 :(得分:2)

您正在使用英国拼写&#34; color&#34;,使用&#34; color&#34;代替。

最简单的方法可能是尝试将所需的文本包裹在span或其他元素中,如下所示,然后将background-color添加到其中:

&#13;
&#13;
span {
  background: #000;
  color: #fff;
}
&#13;
<span>Highlight text</span> Not Highlighted Text
&#13;
&#13;
&#13;

另外,请使用color: #000;,而不是text-color: #000;。该财产不会退出。

答案 1 :(得分:1)

一些更正使您的代码更好

  • <p>标记替换为<span>标记(因为<h4>标记不能位于<p>标记内。
  • 没有任何css属性 background-color ,请使用 background-color
  • 没有任何css属性 text-color ,请将其替换为颜色

.text {

    background-color:  #33383b;
    box-sizing: border-box;
    padding: 30px 30px;
    color: red;

    }
<div class="text">
            <span>
            <h4>A.Willi A.G für Menschen zur Stelle</h4>
            Die A.Willi A.G ist als Personalverleiher in der gesamten Schweiz und im Ausland erfolgreich tatig mit über 
            30-jahrige Erfahrung in dieser Sparte.
            </span> 
            </div>

答案 2 :(得分:0)

您应该更改代码

的列表
  • 标题(h1 - h6)绝不应位于段落
  • ü无效HTML;这是一个需要用&符号进行转义的特殊角色,如&uuml;,意思是带有变音符号的你
  • text-colour应拼写为text-
  • background-colourtext-colour应拼写为u
  • 然后是30px 30px的填充,您可以缩短为30px,但这不是太大的交易。

我不喜欢抨击其他人的答案,但你已经接受了梦想猎人的答案,这在很多方面都是完全错误的。

  • 你在那里的文字显然是一个段落,所以把它改成一个不会在语义上正确的范围
  • 在内联块(例如跨度)内部有一个块元素(例如h4)是错误的

希望这有帮助

答案 3 :(得分:-1)

您为任何css类设置background-colourtext-colour以更改背景颜色。

相关问题