Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 了解CSS选择器优先级/特异性_Html_Css_Css Selectors - Fatal编程技术网

Html 了解CSS选择器优先级/特异性

Html 了解CSS选择器优先级/特异性,html,css,css-selectors,Html,Css,Css Selectors,我想了解CSS选择器如何处理属性冲突。如何选择一个属性而不是另一个属性 <!-- start class vs id --> <p class="class1" id="id1">.class vs #id: The winner is #id</p> <!-- upper vs bottom --> <p id="id2">the very bottom is the winner</p> <

我想了解CSS选择器如何处理属性冲突。如何选择一个属性而不是另一个属性

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>
div{
背景色:红色;
}
我的班级{
背景色:黑色;
}
div#我的id{
背景颜色:蓝色;
}
身体科{
背景颜色:绿色;
}
正文>div{
背景颜色:橙色;
}
body>div#我的id{
背景颜色:粉红色;
}

您好
您感兴趣的是

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>

Firebug是一个很好的工具来帮助检查这一点。但是其他浏览器也有内置的工具来检查应用的CSS规则。

您感兴趣的是

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>

Firebug是一个很好的工具来帮助检查这一点。但其他浏览器也有内置的工具,用于检查应用的CSS规则。

我将插入一个指向CSS 2.1规范本身的链接,以及浏览器应该如何计算特定性:

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>
:

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>
选择器的特异性计算如下:

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>
  • 如果声明来自,则计数1是一个“样式”属性,而不是一个带选择器的规则,否则为0(=a)(在HTML中,元素“样式”属性的值是样式表规则。这些规则没有选择器,因此a=1、b=0、c=0和d=0。)
  • 计算选择器中ID属性的数量(=b)
  • 计算选择器中其他属性和伪类的数量(=c)
  • 计算选择器中元素名称和伪元素的数量(=d)
  • 特异性仅基于选择器的形式。特别是,形式为“[id=p33]”的选择器被计为属性选择器(a=0,b=0,c=1,d=0),即使id属性在源文档的DTD中定义为“id”
将四个数字a-b-c-d(在基数较大的数字系统中)串联在一起,给出了特殊性

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>
如果特殊性相同,则发挥作用:

  <!-- start class vs id -->
  <p class="class1" id="id1">.class vs #id: The winner is #id</p>

  <!-- upper vs bottom -->
  <p id="id2">the very bottom is the winner</p>

  <!--most specific is the winner -->
  <p id="id3">the most specific</p>

  <!--pseudo and target selector -->
  <h3>pseudo vs type selector</h3>

  <!-- !important is more important! -->
  <h1 id="very-specific">HI! I am very important!</h1>
  </body>
  • 最后,按指定的顺序排序:如果两个声明具有相同的权重、来源和特定性,则后一个声明将获胜。导入的样式表中的声明被视为在样式表本身中的任何声明之前

  • 请注意,这是指何时定义样式,而不是何时使用样式。如果类
    .a
    .b
    具有相同的特异性,则以样式表中最后定义的为准<根据
    .a
    .b
    的定义顺序,code>

    的样式将是相同的,我将插入一个指向CSS 2.1规范本身的链接,以及浏览器应该如何计算特定性:

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    :

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    选择器的特异性计算如下:

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    • 如果声明来自,则计数1是一个“样式”属性,而不是一个带选择器的规则,否则为0(=a)(在HTML中,元素“样式”属性的值是样式表规则。这些规则没有选择器,因此a=1、b=0、c=0和d=0。)
    • 计算选择器中ID属性的数量(=b)
    • 计算选择器中其他属性和伪类的数量(=c)
    • 计算选择器中元素名称和伪元素的数量(=d)
    • 特异性仅基于选择器的形式。特别是,形式为“[id=p33]”的选择器被计为属性选择器(a=0,b=0,c=1,d=0),即使id属性在源文档的DTD中定义为“id”
    将四个数字a-b-c-d(在基数较大的数字系统中)串联在一起,给出了特殊性

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    如果特殊性相同,则发挥作用:

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
  • 最后,按指定的顺序排序:如果两个声明具有相同的权重、来源和特定性,则后一个声明将获胜。导入的样式表中的声明被视为在样式表本身中的任何声明之前
  • 请注意,这是指何时定义样式,而不是何时使用样式。如果类
    .a
    .b
    具有相同的特异性,则以样式表中最后定义的为准<根据
    .a
    .b
    的定义顺序,code>

    的样式将相同

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
  • 元素
  • 类选择器
  • ID选择器
  • 内联样式
  • !!重要的
  • 按顺序,1为最低特异性,5为最高特异性。 将显示详细信息以演示它

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
  • 元素
  • 类选择器
  • ID选择器
  • 内联样式
  • !!重要的
  • 按顺序,1为最低特异性,5为最高特异性。
    将显示详细信息以进行演示。

    您可以在此处参考完整答案

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    从最具体的开始: id选择器>类选择器>类型选择器(普通h1、p标记等) !重要的总是赢,但这被认为是一个坏的做法。请参阅上面的链接

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    最好的方法是进行试验:

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    

    一个测试用例。

    你可以在这里参考完整答案

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    从最具体的开始: id选择器>类选择器>类型选择器(普通h1、p标记等) !重要的总是赢,但这被认为是一个坏的做法。请参阅上面的链接

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    
    最好的方法是进行试验:

      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>
    

    一个测试用例。

    Firebug是否有特定的(没有双关语)工具来分析特异性?比通过罢工查看被推翻的规则更重要吗?@Pekka,不,没有。尽管如此,这仍然是一个很好的工具。@Pekka在HTML选项卡的右侧,Calculated(?)选项卡(介于样式和外观之间)有另一个贯穿覆盖指令视图的工具。当您只对一个属性感兴趣时,它比您可能指的样式更有用。Firebug是否有特定的(没有双关语)工具来分析特定性?超过了覆盖规则的穿透视图?@Pe
      <!-- start class vs id -->
      <p class="class1" id="id1">.class vs #id: The winner is #id</p>
    
      <!-- upper vs bottom -->
      <p id="id2">the very bottom is the winner</p>
    
      <!--most specific is the winner -->
      <p id="id3">the most specific</p>
    
      <!--pseudo and target selector -->
      <h3>pseudo vs type selector</h3>
    
      <!-- !important is more important! -->
      <h1 id="very-specific">HI! I am very important!</h1>
      </body>