gpt4 book ai didi

html - Adam Freeman 书中的 CSS 特异性查询

转载 作者:太空宇宙 更新时间:2023-11-04 02:56:29 25 4
gpt4 key购买 nike

我正在阅读 Adam Freeman 的“HTML5 权威指南”,并且有一个关于 CSS 特异性的问题。他给出了以下示例:

<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>

<style type="text/css">
a {
color: black;
}

a.myclass {
color: white;
background: grey;
}
</style>
</head>

<body>
<a href="http://apress.com">Visit the Apress website</a>
<p>I like <span>apples</span> and oranges.</p>
<a class="myclass" href="http://w3c.org">Visit the W3C website</a>
</body>

</html>

并指出:

In this case, the selector a.myclass includes a class attribute, which means that the specificity of the style is 0-1-0 (0 id values + 1 other attributes + 0 element names). The other style has a specificity of 0-0-0 (that is, it contains no id values, other attributes or element names).

我原以为“a.myclass”选择器的特异性得分为 0-1-1,因为它包括类和元素。同样,我希望“a”选择器的得分为 0-0-1。将这两个选择器输入到这个 CSS Specificity Calculator确实产生了我期望的结果。 (除了这个计算器在分数中包含内联样式)

谁能解释 Adam 的评论,或者我应该将其报告为勘误表?

最佳答案

你是对的。 a.myclass 选择器的特异性得分为 0-1-1a 是类型选择器,必须以 1 计分。

请检查 w3.org 特异性示例:

*               /* a=0 b=0 c=0 -> specificity =   0 */
LI /* a=0 b=0 c=1 -> specificity = 1 */
UL LI /* a=0 b=0 c=2 -> specificity = 2 */
UL OL+LI /* a=0 b=0 c=3 -> specificity = 3 */
H1 + *[REL=up] /* a=0 b=1 c=1 -> specificity = 11 */
UL OL LI.red /* a=0 b=1 c=3 -> specificity = 13 */
LI.red.level /* a=0 b=2 c=1 -> specificity = 21 */
#x34y /* a=1 b=0 c=0 -> specificity = 100 */
#s12:not(FOO) /* a=1 b=0 c=1 -> specificity = 101 */

引用:w3.org - Calculating a selector's specificity

关于html - Adam Freeman 书中的 CSS 特异性查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com