代码之家  ›  专栏  ›  技术社区  ›  Sam Johannes Rudolph

为什么文本颜色属性称为“颜色”?

  •  3
  • Sam Johannes Rudolph  · 技术社区  · 14 年前

    在CSS中,为什么用 color 而不是 text-color font-color ?

    还有,两者之间的区别 font- text- ?

    2 回复  |  直到 12 年前
        1
  •  9
  •   Community CDub    7 年前

    这个 color 属性不属于属性的字体或文本类别。它实际上在它自己的规范中,两个 CSS1 CSS2.1 . 甚至有一整个 CSS3 module 专注于色彩。

    每种规格均指 颜色 作为定义 前景 颜色,然后将其描述为引用元素文本内容的颜色。但是自从 foreground-color 如果不必要的话,属性名中就没有“前台”这个词了。 background-color 是不言而喻的 颜色 .

    也就是说 颜色 如果还指定了边框但未指定 border-color ,边框将采用与文本相同的颜色。这是完全正常的,因为它是规定的 in the CSS box model specification :

    如果未使用border属性指定元素的边框颜色,则用户代理必须使用元素的“color”属性的值作为边框颜色的计算值。

    <div style="color: red; border: 1px dotted">
        This block has red text and a red 1-pixel dotted border.
        Notice only the width and style are specified in the style attribute.
    </div>
    

    Martin Algesten in his answer has a nice summary 字体和文本属性之间的差异。

        2
  •  2
  •   BoltClock    12 年前

    字体 字体 ,即。 Arial 是字体, Arial Bold 是字体。你没有设置字体颜色(我从来没有听说过有特定颜色的字体)。你在设置文本的颜色。

    我想这个想法是你可以有一个受 color ,不仅仅是文本。与 text-decoration: underline 很明显只会影响文本,但如果我们设想可以在内联中插入一个随机形状,它也会受到 颜色 .