Css 强选择器如何覆盖id选择器?Isn';id选择器是否被认为更具体?

Css 强选择器如何覆盖id选择器?Isn';id选择器是否被认为更具体?,css,css-selectors,css-specificity,Css,Css Selectors,Css Specificity,在下面的代码段中,strong选择器为什么会覆盖id选择器?id选择器是否被认为更具体,因此具有优先级 <!doctype html> <html> <head> <title>Sample document</title> <style> strong{color:red;} #abc {color:blue;} </style> </head> &l

在下面的代码段中,strong选择器为什么会覆盖id选择器?id选择器是否被认为更具体,因此具有优先级

<!doctype html>
<html>
  <head>
  <title>Sample document</title>
  <style>
      strong{color:red;}
      #abc {color:blue;}
  </style>
  </head>
  <body>
    <p id="abc">
      <strong>C</strong>ascading
      <strong>S</strong>tyle
      <strong>S</strong>heets
    </p>
  </body>
</html>

样本文件
强{颜色:红色;}
#abc{颜色:蓝色;}

Cascading Style S注意事项


您的具体操作是正确的,但选择器仅对元素的直接内容起作用。因此
strong
元素的文本颜色不同,因为它嵌套得更深,并且
p
选择器无法更改这些元素的颜色。因此,如果您确实想更改
#abc
的强元素,您可以这样做

strong { color: red; }

#abc strong {  color: blue; } 
如果您希望
strong
标记文本与
p
文本相同,那么您可以这样做

#abc, #abc strong { color: red; }
strong{color:red;}
#abc强{颜色:蓝色;}
#def,#def strong{颜色:红色;}

Cascading Style S注意事项

ABC DEF


没有任何内容被覆盖,因为您的目标是两个不同的元素。这与特异性无关。谢谢,但是标签中的字母不也在p#abc内吗?是的,但是strong有
颜色:红色
,它没有
颜色:继承
strong
取代
\abc
,因为
\abc
样式只为
的内容继承。是的,
#abc
在命名方面更具体,但在呈现
时,它只是继承的(比直接样式规则更不具体)。如果它是一个
,那么绝对正确,文本是蓝色的。因为它是嵌套的?是的。如果为
p
文本指定颜色,那么该标记中的每个元素都会得到该颜色,这是没有意义的。