css后代选择器:nth

The following is an extract from our book, CSS Master, written by Tiffany B. Brown. Copies are sold in stores worldwide, or you can buy it in ebook form here.

以下是Tiffany B. Brown所著《 CSS Master》一书的摘录。 副本在世界各地的商店中都有出售,或者您可以在此处以电子书形式购买 。

Think of specificity as a score or rank that determines which style declarations are ultimately applied to an element. The universal selector (*) has low specificity. ID selectors are highly specific. Descendant selectors such as p img and child selectors such as .panel > h2 are more specific than type selectors such as p, img, or h1.

特异性视为得分或等级,该得分或等级确定最终将哪些样式声明应用于元素。 通用选择器( * )具有较低的特异性。 ID选择器非常具体。 后代选择器(例如p img和子选择器(例如.panel > h2比类型选择器(例如pimgh1更具体。

Calculating exact specificity values seems tricky at first. As explained in Selectors Level 3, you need to:

首先,计算确切的特异性值似乎很棘手。 如选择器级别3中所述,您需要:

  • count the number of ID selectors in the selector (= A)

    计算选择器中ID选择器的数量(= A)

  • count the number of class selectors, attribute selectors, and pseudo-classes in the selector (= B)

    计算选择器中的类选择器,属性选择器和伪类的数量(= B)

  • count the number of type selectors and pseudo-elements in the selector (= C)

    计算选择器中类型选择器和伪元素的数量(= C)

  • ignore the universal selector

    忽略通用选择器

These A, B, and C values are then combined to form a final specificity value. An ID selector such as #foo has a specificity of 1,0,0. Attribute selectors, such as [type=email] and class selectors such as .chart have a specificity of 0,1,0. Adding a pseudo-class such as :first-child (for example, .chart:first-child) gives us a specificity of 0,2,0. But using a simple type or element selector such as h1 or p only gives us a specificity of 0,0,1.

然后将这些A,B和C值合并以形成最终的特异性值。 ID选择器(例如#foo )的特异性为1,0,0。 属性选择器(例如[type=email]和类选择器(例如.chart的特异性为0,1,0。 添加一个伪类,如:first-child (例如.chart:first-child ),可以得到0,2,0的特异性。 但是使用简单的类型或元素选择器(例如h1p只会给我们带来0,0,1的特异性。

注意:计算特异性 (Note: Calculating Specificity)

Keegan Street’s Specificity Calculator and Joshua Peek’s CSS Explain are helpful for learning about and calculating selector specificity.

Keegan Street的特异性计算器和Joshua Peek的CSS Explain有助于学习和计算选择器特异性。

Complex and combinator selectors, of course, give us higher specificity values. Let’s look at an example. Consider the following CSS:

当然,复杂选择器和组合选择器为我们提供了更高的特异性值。 让我们来看一个例子。 考虑以下CSS:

ul#story-list > .book-review {
color: #0c0;
}
#story-list > .book-review {
color: #f60;
}

These two rule sets are similar, but they are not the same. The first selector, ul#story-list > .bookreview, contains a type selector (ul), an ID selector, (#story-list), and a class selector (.bookreview). It has a specificity value of 1,1,1. The second selector, #story-list > .book-review only contains an ID and a class selector. Its specificity value is 1,1,0. Even though our #story-list > .book-review rule succeeds ul#story-list > .bookreview, the higher specificity of the former means that those elements with a .book-review class will be green rather than orange.

这两个规则集相似,但是不相同。 第一个选择器ul#story-list > .bookreview包含类型选择器( ul ),ID选择器( #story-list )和类选择器( .bookreview )。 其特异性值为1,1,1。 第二个选择器#story-list > .book-review仅包含一个ID和一个类选择器。 其特异性值为1,1,0。 即使我们的#story-list > .book-review规则继承ul#story-list > .bookreview ,但前者的较高特异性意味着具有.book-review类的那些元素将是绿色而不是橙色。

Pseudo-classes such as :link or :invalid have the same level of specificity as class selectors. Both a:link and a.external have a specificity value of 0,1,1. Similarly, pseudo-elements such as ::before and ::after are as specific as type or element selectors. In cases where two selectors are equally specific, the cascade kicks in. Here’s an example:

诸如:link:invalid类的伪类具有与类选择器相同的特异性。 a:linka.external的特异性值均为0,1,1。 同样,伪元素(例如::before::after与类型或元素选择器一样具体。 如果两个选择器是同样特定的,则级联开始。这是一个示例:

a:link {
color: #369;
}
a.external {
color: #f60;
}

If we applied this CSS, every link would be slate blue except for those with class="external" applied. Those links would be orange instead.

如果我们应用此CSS,则除应用class="external"链接外,每个链接都将变成蓝色。 这些链接将改为橙色。

Keeping specificity low helps prevent selector creep, or the tendency for selector specificity and length to increase over time. This often happens as you add new developers to a team, or new forms of content to a website. Selector creep also contributes to long-term maintenance headaches. You either end up using more specific selectors to override other rule sets, or needing to refactor your code. Longer selectors also increase the weight of your CSS files.

保持较低的特异性有助于防止选择器蠕变 ,或防止选择器特异性和长度随时间增加的趋势。 这通常是在您将新开发人员添加到团队中或将新内容形式添加到网站时发生的。 选择器的蠕变也会导致长期的维护头痛。 您要么最终使用更特定的选择器来覆盖其他规则集,要么需要重构代码。 较长的选择器还会增加CSS文件的权重。

We discuss strategies for keeping specificity low in Chapter 2.

我们将在第2章中讨论降低特异性的策略。

结论 (Conclusion)

After reading this chapter, you should have a good understanding of CSS selectors. Specifically, you should now know how to:

阅读本章后,您应该对CSS选择器有很好的了解。 具体来说,您现在应该知道如何:

  • use selectors to apply CSS to particular elements, pseudo-elements, and pseudo-classes

    使用选择器将CSS应用于特定元素,伪元素和伪类

  • understand the difference between pseudo-elements and pseudo-classes

    了解伪元素和伪类之间的区别

  • employ newer pseudo-classes introduced by the Selectors Level 3 and 4 specifications

    使用选择器级别3和4规范引入的更新的伪类

  • calculate specificity

    计算特异性

In the next chapter, we’ll address some golden rules for writing maintainable, scalable CSS.

在下一章中,我们将讨论编写可维护的,可伸缩CSS的一些黄金法则。

翻译自: https://www.sitepoint.com/css-selectors-specificity/

css后代选择器:nth

css后代选择器:nth_CSS选择器:特异性相关推荐

  1. 基本CSS选择器,复合选择器,后代选择器

    基本CSS选择器有标记选择器.类别选择器.ID选择器3种 1.标记选择器     每一种HTML标记的名称都可以作为相应的标记选择器的名称,如h1,p,等等 2.类别选择器     类别选择器的名称可 ...

  2. 【温故知新】CSS学习笔记(后代和子代选择器)

    后代和子代选择器 CSS的选择器除了之前介绍的基本选择器之外,还有符合选择器,可以处理一些基本选择器无法处理的难题. 1.后代选择器 比如下面的例子,我们需要将所有的"Hello" ...

  3. css 交集选择器 并集选择器 后代选择器

    css 交集选择器 并集选择器 后代选择器 **交集选择器:**就是相互交集在一起的选择器同时对一个标签进行样式 的书写. 代码如下: <!DOCTYPE html> <html l ...

  4. html子代选择器,Css 后代选择器与子代选择器的区别

    后代选择器用空格,比如A B{border:1px solid red;} 子代选择器用>, 比如A>B{border:1px solid red;} 但是,如果你仔细想想,这俩个概念是不 ...

  5. CSS之后代、子代选择器的区别

    后代.子代选择器的区别 子代选择器 后代选择器 子代选择器 后代选择器 子代选择器只选中父级元素的亲一代,父子级之间用大于号">"连接 后代选择器选中的是指定元素的所有后代, ...

  6. css后代选择器:nth_消失的行为:CSS:空选择器

    css后代选择器:nth CSS writers are the blind swordsmen of web development: we pen declarations and throw t ...

  7. 好程序员web前端CSS选择符(选择器):表示要定义样式的对象

    好程序员web前端CSS选择符(选择器):表示要定义样式的对象 1) 元素选择符/类型选择符(element选择器 ) 如:div{width:100px; height:100px; backgro ...

  8. css样式表和选择器

    CSS样式----图文详解:css样式表和选择器 主要内容 CSS概述 CSS和HTML结合的三种方式: 行内样式表 . 内嵌样式表 . 外部样式表 CSS四种基本选择器: 标签选择器 . 类选择器 ...

  9. ***CSS魔法堂:选择器及其优先级

    一.前言     首先看看一道阿里这期网申的题目吧! 1.找出下面优先级相同的选择器 A. img.thumb:after B.[data-job="frontend"]::fir ...

最新文章

  1. 构建自主操作系统,阿里为何失败了?
  2. Spring data redis应用示例
  3. axios基础和封装
  4. 从零开始入门 K8s | etcd 性能优化实践
  5. 0207.Domino R8.0.x群集配置手册
  6. linux 命令行使用技巧linux 下的文件管理
  7. 牛客题霸 [矩阵查找] C++题解/答案
  8. map 循环_被问到Spring循环依赖怎么解决?秀给面试官看!内附图解
  9. 学习人工智能不走捷径,走大道的方式
  10. sql server经典sql
  11. 关于“想哭”病毒,我也来两句。--转载
  12. PAT 乙级 1031. 查验身份证(15) Java版
  13. 玩转VIM编辑器-自动补全
  14. 常见移动机器人多角度对比分析(图片版)
  15. XP和win7的软件崩溃提示
  16. Redis缓存持久化
  17. 《Java核心技术卷一》读书笔记(二)
  18. 「mac基础知识」mac上将Keynote设置为使用iCloud的方法
  19. Android Studio如何Debug对应so文件C/C++代码
  20. java8 foreach 异常_在java 8流foreach中抛出异常

热门文章

  1. katalon:赋值方式
  2. SQL 执行计划(一)
  3. python list去重 set和list互转
  4. idea开发工具右侧没有maven工具栏
  5. ad敷铜后还有部分飞线_LOL新赛季“冥火”再次出现:AD退游,职业选手找不到血瓶...
  6. 50道PMP考试样题
  7. 博客迁移之从CSDN导出markdown格式文章
  8. Java基础之删除list中的某个元素
  9. 和刘备相关的人(五)
  10. CISP——身份鉴别