改变特异性

时间:2012-08-05 16:23:35

标签: css css-specificity controlling css-cascade

<html>
  <head>
    <style type="text/css">
    #sidebar p {
        font-family: Verdana;
        font-size: .9em; }

        #sidebar .intro {
            font-family: Georgia;
            font-size: 1.25em;
            color:red;
            }

    </style>

  </head>
  <body>
    <div id=”sidebar”>
       <p class=”intro”>
           As you can see, the more CSS styles you create, the greater the potential for formatting
          snafus. For example, you may create a class style specifying a particular
         font and font size, but when you apply the style to a paragraph, nothing happens!
          This kind of problem is usually related to the cascade. Even though you may think
         that directly applying a class to a tag should apply the class’s formatting properties,
          it may not if there’s a style with greater specificity.

        You have a couple of options for dealing with this kind of problem. First, you can
        use !important (as described in the box above) to make sure a property always
        applies. The !important approach is a bit heavy handed, though, since it’s hard to
        predict that you’ll never, ever, want to overrule an !important property someday.
        Read on for two other cascade-tweaking solutions.
    </p>    
</div>

  </body>
</html>
#sidebar p 样式的特异性为101 (ID为100,标记为1) 选择器),而 .intro风格的特异性为 10 (类选择器为10分)。由于101大于10,所以#sidebar p优先。将.intro更改为#sidebar .intro会将其特异性更改为110。

即使我改变了这个,我的结果还没有到来

任何人都可以解释我。

输出:应为红色,font-size-1.25em和font-family -Georgia

1 个答案:

答案 0 :(得分:1)

ID sidebar和类名intro周围的引号无效。 ”sidebar”应为"sidebar"

浏览器会将ID解释为”sidebar”而不是sidebar,因此,您的所有规则都不匹配。