css nth-child

CSS has a number of pseudo-classes to help you style your app with ease. Having lots of options can be great, but it can also be confusing to know which pseudo-class to use and what it really does. Here we’ll look at CSS’s nth-child and nth-of-type pseudo-classes to get a better understanding of when to use each and what the actual difference is.

CSS有许多伪类,可帮助您轻松设置应用样式。 有很多选项可能很棒,但是知道要使用哪个伪类以及它的实际作用也可能会造成混淆。 在这里,我们将查看CSS的nth-child和nth-of-type伪类,以更好地理解何时使用每种伪类以及它们之间的实际区别。

The nth-child and nth-of-type CSS pseudo-classes are interesting compared to other types of pseudo-classes because they select elements based on their position in the DOM. Whereas some pseudo-classes select a specific state of an element (e.g. the hover, active, target pseudo-classes), nth-child and nth-of-type are more concerned with the structure of your markup.

与其他类型的伪类相比, nth-childnth-of-type CSS伪类很有趣,因为它们基于元素在DOM中的位置来选择元素。 尽管某些伪类选择元素的特定状态(例如, 悬停,活动 , 目标伪类),但是nth-child nth-of-typenth-of-type更关心标记的结构。

设置我们HTML (Setting Up Our HTML)

To understand the difference between nth-child and nth-of-type, let’s first build our HTML to know what we’re going to be styling.

要了解nth-childnth-of-type之间的区别,让我们首先构建HTML来了解样式。

Let’s say we have a section on our webpage that has a mix of header (<h1>, <h3>) and paragraph (<p>) elements.

假设我们的网页上有一部分包含标头( <h1><h3> )和段落( <p> )元素。

<article><h1>Here's a Header</h1><p>I'm a paragraph with all kinds of information.</p><p>Let's add another paragraph for fun.</p><p>yadda yadda yadda</p><p>blah blah blah</p><p>yadda yadda yadda</p><h3>Here's a Subheader</h3><p>blah blah blah</p><p>And maybe just one more.</p>
</article>

This markup will look something like this:

这个标记看起来像这样:

In total, we have an <article> element as the parent, and nine child elements: one <h1>, one <h3>, and seven <p> tags.

总的来说,我们有一个<article>元素作为父元素,还有9个子元素:一个<h1> ,一个<h3>和七个<p>标记。

nth-child和nth-of-type语法 (nth-child and nth-of-type Syntax)

There are a few options for what values you can pass the nth-child and nth-of-type pseudo-classes. Note: nth-child is used here but it can be replaced with nth-of-type too.

对于可以传递第nth-childnth-of-type伪类的值,有一些选择。 注意:此处使用了nth-child ,但也可以将其替换为nth-of-type

  • :nth-child(2n+3): This option requires some math. The numbers are up to you; it’s the n that will vary. This will take your selected elements, set n to 0 to start, and increment from there. So, similarly to a for loop in JavaScript, it will iterate through your selected elements by updating the n value: 2(0)+3 = 3, 2(1)+3 = 5, 2(2)+3 = 7, and so on. The result of this will be selecting the third, fifth, seventh, etc. elements.

    :nth-​​child(2n + 3) :此选项需要一些数学运算。 数字由您决定; 是n会有所不同。 这将采用您选择的元素,将n设置为0开始,并从此处开始递增。 因此,类似于JavaScript中的for循环,它将通过更新n值来遍历您选择的元素: 2(0)+3 = 3 2(1)+3 = 5 2(2)+3 = 7 ,等等。 这样的结果将是选择第三,第五,第七等元素。

  • :nth-child(odd/even): The strings odd or even can be passed to select the odd and even elements available.

    :nth-​​child(odd / even) :可以传递oddeven字符串以选择可用的奇数和偶数元素。

  • :nth-child(3n): You can also pass a number with the n variable, which will select that interval of the selected element’s occurrence. If 3 is passed, it will select the third, sixth, ninth, etc. elements.

    :nth-​​child(3n) :您还可以传递带有n变量的数字,该数字将选择所选元素出现的时间间隔。 如果3通过,它将选择的第三,第六,第九,等元素。

  • :nth-child(3): If you just pass a number (without the n), it will select that element from the DOM specifically. Passing 3 will select the third matching element only.

    :nth-​​child(3) :如果只传递一个数字(不带n ),它将从DOM中专门选择该元素。 传递3只会选择第三个匹配元素。



使用CSS的第n个子级伪类 (Using CSS’s nth-child Pseudo-Class)

The nth-child pseudo-class has two important components to consider:

nth-child伪类具有两个要考虑的重要组成部分:

  • the element(s) selected that will have the pseudo-class applied to it/them所选元素将应用伪类的伪元素
  • the value passed to the pseudo-class.传递给伪类的值。

If we go to our CSS stylesheet for the HTML example above, we can select our paragraph elements and make the font color maroon like so:

如果转到上述HTML示例CSS样式表,则可以选择我们的段落元素,并使字体颜色maroon如下:

article p {color: maroon;
}

Let’s say we want every other paragraph element to be a yellow-ish color, though. (An “interesting” style choice…

css nth-child_比较CSS伪类:nth-​​child与nth-of-type相关推荐

  1. 关于css中after和before伪类的用法

    一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类--:hover,:link,:active,:target,:not(),:focus. 常见伪元素--::first-let ...

  2. CSS基础知识(表单标签、事件、CSS介绍、选择器、伪类选择器案例、样式、盒子模型、定位)...

    一. 表单标签 <!DOCTYPE html> <html lang="en"> <head><meta charset="UT ...

  3. 常用的一些 CSS 技巧二 — 选择器(伪类与伪元素)

    你可以看看其他常用的 CSS 技巧: 常用的一些 CSS 技巧一 常用的一些 CSS 技巧三 CSS 重置盒模型 *, *::before, *::after {box-sizing: border- ...

  4. CSS复合选择器之链接伪类选择器及总结

    在某些大型购物网站.新闻网站中可以看到许许多多的链接,其中很多链接的样式都为黑色,当鼠标放到某一链接上的时候,链接的颜色也会变化,点击过后颜色也会变化.这就用到了CSS复合选择器中的链接伪类选择器. ...

  5. CSS学习(05结构伪类选择器 浮动及清除)

    文章目录 一 结构伪类选择器 1.1 作用与优势: 1.2 选择器 1.3 n的注意点 二 伪元素 2.1 区别: 2.2 种类: 2.3 注意点: 三 标准流 四 浮动 4.1 浮动的产生 4.2 ...

  6. html css hover事件,用:hover伪类代替js的hover事件_html/css_WEB-ITnose

    制作二级菜单要实现鼠标移动上去显示子菜单,鼠标移出子菜单隐藏,或者其他类似需求的地方,首先我会想到用jquery的hover事件来实现,如: $(".nav").hover(fun ...

  7. CSS属性选择器和部分伪类

    p[name]{background:red;} 只使用属性名 p[name=ghr]{background:red;} 指定属性名,并指定了该属性的属性值 p[name~=old]{backgrou ...

  8. CSS学习05之结构伪类选择器

    回顾 基本选择器又分为三种方式: 后代选择器 子选择器 相邻选择器 通用选择器 结构伪类选择器 结构伪类选择器有很多种伪类,这里我们列举常用的几种方式: first-child 伪类 hover悬停伪 ...

  9. 【温故知新】CSS学习笔记(链接伪类选择器)

    链接伪类选择器 伪类选择器用于向某些选择器添加特殊的效果,比如给链接添加特殊效果. 之前我们学过类选择器,以点"."开头,为了区分开来,伪类选择器则是以冒号":" ...

  10. CSS基础(part20)--CSS3结构伪类选择器

    学习笔记,仅供参考,有错必究 参考自:pink老师教案 结构伪类选择器 属性列表: nth-child(n) 参数详解: 注意:本质上就是选中第几个子元素 n可以是数字.关键字.公式 n如果是数字,就 ...

最新文章

  1. 服务器项目带文件名,tomcat服务器上webapps里的文件名和项目名称不一样,修改方法...
  2. PHP调试的时候遇到Warning: session_start() [function.session-
  3. .NET软件开发, 你应该知道 (整)
  4. 4.21 LNMP环境介绍 4.22/23/24 Mariadb安装 4.25 服务管理
  5. ASP.NET 2.0 中的资源与本地化
  6. eof怎么结束输入_SimRobot算法社第二次活动圆满结束啦!
  7. C/C++中char *与wchar_t*的几种转换方法
  8. Flask项目--发送短信验证码
  9. falcon适配ldap密码同步
  10. spring boot 2.x 系列 —— spring boot 整合 kafka
  11. jquery页面跳转带cookie_python socket编程:实现redirect函数、cookie和session
  12. 简单新闻管理系统(jsp)
  13. 【Python函数优化】
  14. windows截图快捷方式
  15. 3W 字 Docker 容器技术大科普
  16. Span 介绍及使用(二)
  17. xxl-job整合到微服务
  18. 【转】如何用Word编辑参考文献
  19. 美国计算机专业博士后,美国俄克拉荷马大学电子与计算机工程系招聘博士后
  20. Xshell连接虚拟机失败,解决办法!

热门文章

  1. 手机“族群论”:十大族群背后的移动新世界
  2. 带上紧箍咒,特斯拉辅助驾驶系统即将更新
  3. 服务器bcd配置损坏怎么修复,引导记录损坏修复方法详解
  4. YOLOV3实现车牌检测
  5. WIN10 3个手指拖动_用好笔记本的触摸板(win10小技巧)
  6. 玩转「Wi-Fi」系列之测试工具(三)
  7. 运行sfc/scannow的时候弹出插入cd对话框
  8. win10计算机配置在哪里打开,详细教您win10设备管理器在哪打开
  9. 搞ERP的和搞低代码的别鸡同鸭讲,还是走着瞧吧
  10. 京东智能供应链平台应急场景实践