til
  • README
  • Software Development Roles
  • solid
  • README
    • service-worker
  • docker
    • arg
    • 更新docker版本
  • editor
    • vscode
    • Creating a VS Code Theme
  • english
    • words
  • front-end
    • ==
    • ECMAScript
    • IIFE
    • Label
    • basic
    • html.js.css渲染顺序
    • npm-vs-yarn
    • obj-delete-key-value
    • react
    • split-join-and-replace
    • video
    • 前端自检清单
    • 递归及去重
    • css
      • css换肤
      • flex
      • list
      • nth-child和nth-of-type区别
      • padding
      • position
      • 层叠上下文
      • 层叠样式(+)
      • 正方形
      • 语义化标签
    • dom
      • DOCTYPE
      • HEAD
      • 修改document
      • 自定义表单验证
    • electron
      • basic
    • es6
      • basic-type
      • basic
      • prototype-example
      • defineProperty
      • understanding-es6
        • 0.introduction
        • Appendix A: Smaller Changes
        • Appendix B: Understanding ES7
        • Block-Binding
        • Proxies&Reflection
        • class
        • 解构赋值
        • function
        • improved-array
        • iterators&generators
        • modules
        • object
        • promise
        • Map&Set
        • symbol
    • images
      • 前端角度看图片
    • interview_case
      • lexical_scope
      • redux和localstroage存储位置
    • javascript
      • fuck-the-js
      • js-engine-work
      • js原生操作dom
      • what-is-function-program
      • 执行上下文
      • articles
        • JavaScript中使用函数组合
        • JavaScript中的依赖注入
        • JavaScript作用域链中的标识符解析和闭包
        • JavaScript是何如工作的--概述
        • JavaScript深拷贝
        • JavaScript的全局变量是如何工作的
        • js继承常见的误解
        • node12&chrome中7个新的提案功能
        • 你真的懂JavaScript吗
      • date
        • index
      • engines
        • basic
        • JavaScript引擎基础:外形和内联缓存
        • v8中推测性优化的介绍
        • 优化原型
        • 更快的异步功能和promise
      • events
        • baisc
        • 事件冒泡和捕获
        • 定义事件
        • 页面生命周期
      • higher-order-function
        • curry
        • monad
      • module
        • basic
        • main&module
      • objects
        • iterator
        • spread
        • examples
          • iterator
      • performance
        • blocking-css
        • cache
      • prototype
        • Property-Descriptors
        • basic
        • prototype-shadow
      • you-dont-known-js
        • async&performance
          • Chapter 1: Asynchrony: Now & Later
          • Chapter 2: Callbacks
          • Chapter 3: Promises
          • Chapter 4: Generators
        • scope & closures
          • apA
          • apB
          • apC
          • apD
          • chapter1-what-is-scope
          • chapter2-lexical-scope
          • chapter3-function-vs-block-scope
          • chapter4-hoisting
          • chapter5-scope-closure
        • this & object prototypes
          • chapter1-this-or-that
          • chapter2-this-make-sense
          • chapter3-objects
          • chapter4-mixing(up)-class-object
          • chapter5-prototype
          • chapter6-behavior-delegation
        • types&grammer
          • Chapter1-Types
          • Chapter2-Values
          • Chapter3-Natives
          • Chapter4-coercion
          • Chapter5-grammer
        • up & going
          • chapter1-into-programming
          • chapter2-into-javascript
          • chapter3-into-YDKJS
    • mobile
      • iPhone分辨率终极指南
    • npm
      • arguments
      • build
    • react-native
      • prop-methods
    • react
      • PropTypes
      • basic
      • codebase-overview
      • component-element-instance
      • context
      • how-to-known-component-is-func-or-class
      • overview
      • react16.9
      • react18计划
      • react的设计原则
      • reconciliation
      • setState
      • useMemo
      • why-do-we-write-super-props
      • 从头实现一个react
      • concurrent
        • 引入并发模式(仅试验)
      • conf
        • conf-2019
      • events
        • 合成事件概述
      • hooks
        • custom-hook
        • effect-hook
        • hooks-api
        • intro
        • overview
        • rules
        • state-hook
        • hooks-vs-class
          • thinking-in-react-hooks
      • overreact
        • Development模式是如何工作的
        • How-Does-setState-Know-What-to-Do
        • Why-Do-React-Elements-Have-a-$$typeof-Property
        • Why-Do-React-Hooks-Rely-on-Call-Order
        • how-to-known-component-is-func-or-class
        • preparing-tach-talk-motivation
        • react作为ui运行
        • things-i-dont-known-as-2018
        • ui-element-problem-and-build-yourself
        • why-do-we-write-super-props
        • 一份完整的useEffect指南
        • 为什么X不是Hook
        • 函数组件与类有什么不同?
        • 演讲准备2-what-why-how
        • 编写弹性组件
        • 让setInterval在React-Hooks中为声明式
      • practice
        • render
      • react-dom
        • basic
      • react-redux
        • apiv7.1-hooks
        • connect
        • shallow-equal
      • redux
        • applyMiddleware
        • applyMiddleware2-细节
        • example
    • regex
      • index
    • stories
      • 数组下标
      • 阻止事件冒泡
    • svelte
      • compile-svelte-in-your-head-1
      • compiler-overview
      • parser
        • 写一个解析器-JavaScript的JSON解析器
    • turbopack
      • basic
    • typescript
      • interface和type的区别
    • webpack
      • hash
      • webpack4-for-react
      • webpack4
      • webpack4to5
      • babel
        • babel-parser和acorn的区别
        • babel.7.11
        • family
        • react16.14使用new-transform
        • update-to-7
    • pdf
      • deep-js
        • basic
      • react
        • reintroducing
  • git
    • capital
    • emoji
  • http
    • http2.0
    • response
  • rails
    • api
    • flash
    • middleware-vs-metal
    • model
    • performance
    • routes
    • environment
      • error
    • patterns
      • service
    • sidekiq
      • params
    • deploy
      • capistrano
        • ssh
  • ruby
    • self
    • net
      • http请求携带cookie
  • server
    • ss
    • ssh
    • user
    • crawler
      • puppeteer
    • nginx
      • domain-without-80
      • nginx节省带宽
  • sql
    • rails
    • search
Powered by GitBook
On this page
  • 先看用法
  • nth-child
  • nth-of-type
  • 来看看区别
  • 相关参考
  1. front-end
  2. css

nth-child和nth-of-type区别

PreviouslistNextpadding

Last updated 5 years ago

在使用nth-child和nth-of-type的时候,你会不会搞混呢?我们来简单的看下他们怎么使用,以及对应的差异。

先看用法

得先知道用法,才能去比较他们的差异。

nth-child

先来看看nth-child的用法。

  • 选择第五个元素

li:nth-child(5) {
    color: green;   
}

要选择第一个元素,可以使用:first-child,其实我打赌你也可以修改上面的元素来实现这一点。

  • 选择除前五个之外的所有

li:nth-child(n+6) {
    color: green;   
}

如果这里有超过10个元素,它将选择5个后面的所有元素。

  • 选择前五个元素

li:nth-child(-n+5) {
    color: green;   
}
  • 每四个一组,选择第一个

li:nth-child(4n-7) {  /* 或者 4n+1 */
    color: green;   
}
  • 选择奇数或者偶数

    • 奇数

      li:nth-child(odd) {
          color: green;   
      }
    • 偶数

      li:nth-child(even) {
          color: green;   
      }
  • 选择最后一个元素

li:last-child {
    color: green;
}

这里显示第十个,因为我们这里只有十个元素。如果有八个元素,那么小绿球就是第八个。...

  • 选择倒数第二个元素

li:nth-last-child(2) {
    color: green;
}

选择第9个因为我们这里有10个元素,但是如果有30个元素则会选择第29个元素。

动手试试?

nth-of-type

假设我们有一个无序列表,并希望“斑马条纹”交替列表项:

<ul>
  <li>First Item</li>
  <li>Second Item</li>
  <li>Third Item</li>
  <li>Fourth Item</li>
  <li>Fifth Item</li>
</ul>

不用为每个列表都添加类(.even & .odd也不用),我们可以使用:nth-of-type:

li {
  background: slategrey;
}
/* 从第二个项开始选择交替项 */
li:nth-of-type(2n) {
  background: lightslategrey;
}

如你所见,:nth-of-type接受一个参数:这个参数可以是单个整数、关键字even或odd,也可以是如上所示的公式。如果指定一个整数,则只选择一个元素——但如果是关键字或公式将遍历 父元素的所有子元素 并选择匹配的元素——类似于在JavaScript中导航数组中的项。关键词even和odd很直截了当,但公式是使用语法a + b构造的,其中:

  • "a"是一个整数

  • "n"是一个字母

  • "+"是一个运算符,可能是+或-

  • "b"是一个整数,如果公式中包含运算符,则需要使用"b"

为了进一步说明,这里有一些不错的:nth-of-type选择器的例子:

感兴趣的点(需要记住哦)

相关的属性

其他的资源

来看看区别

假设有那么一个HTML:

<section>
   <p>Little</p>
   <p>Piggy</p>    <!-- Want this one -->
</section>

我们来做一些完全相同的事情:

p:nth-child(2) { color: red; }
p:nth-of-type(2) { color: red; }

你看到的是相同,但是他们当然有一点不同。

我们的 :nth-child 选择器,在"Plain English"中表示在以下情况下选择一个元素:

  • 这是一个段落元素

  • 这是父级的第二个child

我们的 :nth-of-type 选择器,在"Plain English"中表示:

  • 选择父元素的第二段子元素

:nth-of-type是……怎么说才好呢?更少的条件。

假设我们改成下面这样:

<section>
   <h1>Words</h1>
   <p>Little</p>
   <p>Piggy</p>    <!-- 想要匹配这个 -->
</section>

这就破坏了:

p:nth-child(2) { color: red; } /* 现在就不对了 */

但是下面这个这仍然有效:

p:nth-of-type(2) { color: red; } /* 仍然可以 */

至于上面的"破坏",我的意思是:上面的:nth-child选择器现在选择的是单词"Little"而不是"Piggy",因为该元素满足了条件:1: 它是第二个孩子,2: 它是一个段落元素。至于上面说的"仍然可以",就是说"Piggy"还是被选中的,因为他是该父元素下的第二个段落(p)。

如果想在h1后面添加h2,:nth-child选择器不会选择任何东西,因为现在第二个子节点不再是一个段落,所以选择器什么也找不到。:nth-of-type可以继续工作。

我认为:nth-of-type一般更健壮,也更有用,尽管:nth-child更常见。

浏览器对于:nth-of-type也挺不错,Firefox 3.5+, Opera 9.5+, Chrome 2+, Safari 3.1+, IE 9+。

相关参考

img
img
img

​

img
img

:nth-of-type允许根据公式根据源顺序选择一个或多个元素。在中,它被定义为“结构伪类”,这意味着它用于根据与父元素和兄弟元素的关系对内容进行样式化。

需要注意的是,这个公式是一个方程,遍历每个同级元素,确定要选择哪个。如果包含公式的"n"“部分,则表示一组递增的正整数(就像遍历数组一样)。在上面的例子中,我们用公式2n来选择每第二个元素,这是可行的,因为每次选中一个元素,"n"就会增加1(2×0,2×1,2×2,2×3,等等)。如果一个元素的顺序与方程的结果匹配,那么它将被选中(2,4,6,等等)。要更深入地解释所涉及的数学,请。

:nth-of-type从源顺序的顶部开始遍历元素。它与之间的唯一区别是后者迭代从源顺序底部开始的元素。

:nth-of-type选择器非常类似于:nth-child但有一个 关键区别 :它。在上面的例子中,它们会产生相同的结果,因为我们只遍历li元素,但是如果我们遍历更复杂的兄弟姐妹组,:nth-child会尝试匹配所有的兄弟姐妹,而不仅仅是相同元素类型的兄弟姐妹。这就说明了:nth-of-type的强大功能,它针对的是与类似的兄弟姐妹(而不是所有的兄弟姐妹)的关系顺序中的特定类型的元素。

不要忘记其他的方法::first-of-type,:last-of-type,:nth-last-of-type和:only-of-type。在这里。

来在线尝试
CSS选择器第3级规范
阅读本文
CSS-Tricks Tester
Lea Verou's Tester
:nth-last-of-type
更具体
nth-last-of-type
nth-child
nth-last-child
first-of-type
last-of-type
Mozilla Docs
QuirksMode article
Sitepoint article
了解更多
The Difference Between :nth-child and :nth-of-type
:nth-of-type
Useful :nth-child Recipes
img
img