2

在这段代码中,我的段落看起来像这样

<p class="special">
    This is just a test. <em>what color here</em> inheritance work
</p>

我想知道为什么字符串“这里的颜色”不是从父p元素中获取颜色。我的意思是特殊类的特异性值为 10,而诸如 em 之类的类型的特异性值为 1,所以这里 10 大于 1。

所以我的意思是颜色应该取自选择器.special

这是标记和CSS

<!DOCTYPE html>
<html>
    <head>
       <meta name="keyword" content="html5. tutorial" charset=utf-8" />
       <title></title>
       <style type="text/css" media="screen">
          em
          {
             font-weight:bold;
             color:red;
           }

          .special
          {
              color:blue;
           }
       </style>
    </head>
    <body>
       <p class="special">
          This is just a test. Is this color <em>red</em> inheriatance work
       </p>
   </body>
</html>

//托尼

4

3 回答 3

6

是 中的<em>一个单独元素.special,因此它有自己的特异性分解。如果代码是<em class="special">,则类特异性将适用于<em>.

于 2013-09-04T16:17:15.000 回答
3

这与特异性无关。当两个或多个样式规则应用于同一个元素时,特异性适用。

在这里,您只有一个段落(蓝色),其中恰好包含一个em元素(红色)。

于 2013-09-04T16:17:25.110 回答
0

.special does not select the <em> (the selector does not match). The em selector does match and its properties are applied, hence the red color.

The reason why the <em>'s color would appear blue if the em selector were not there or did not match is because of Inheritance and the fact that color is an inherited property.

For example:

于 2013-09-04T16:28:36.993 回答