css中的nth怎么用

发布时间:2022-04-28 10:09:42 作者:iii
来源:亿速云 阅读:298

CSS中的nth怎么用

在CSS中,nth选择器是一组非常强大的伪类选择器,它们允许我们根据元素在父元素中的位置来选择特定的子元素。这些选择器包括:nth-child():nth-last-child():nth-of-type():nth-last-of-type()。本文将详细介绍这些选择器的用法,并通过示例代码帮助你更好地理解它们的工作原理。

1. :nth-child() 选择器

:nth-child() 选择器用于选择父元素中的第n个子元素。它的语法如下:

:nth-child(an+b)

其中,ab 是整数,n 是一个计数器,从0开始递增。这个公式可以用来选择特定位置的子元素。

1.1 选择奇数或偶数位置的子元素

/* 选择所有奇数位置的子元素 */
li:nth-child(odd) {
  background-color: #f0f0f0;
}

/* 选择所有偶数位置的子元素 */
li:nth-child(even) {
  background-color: #e0e0e0;
}

1.2 选择特定位置的子元素

/* 选择第3个子元素 */
li:nth-child(3) {
  color: red;
}

/* 选择第5个及以后的子元素 */
li:nth-child(n+5) {
  font-weight: bold;
}

/* 选择前3个子元素 */
li:nth-child(-n+3) {
  text-decoration: underline;
}

1.3 使用公式选择子元素

/* 选择每隔2个子元素,从第2个子元素开始 */
li:nth-child(2n+1) {
  background-color: #d0d0d0;
}

/* 选择每隔3个子元素,从第4个子元素开始 */
li:nth-child(3n+4) {
  color: blue;
}

2. :nth-last-child() 选择器

:nth-last-child() 选择器与 :nth-child() 类似,但它从父元素的最后一个子元素开始计数。它的语法如下:

:nth-last-child(an+b)

2.1 选择倒数第n个子元素

/* 选择倒数第2个子元素 */
li:nth-last-child(2) {
  color: green;
}

/* 选择倒数第3个及以后的子元素 */
li:nth-last-child(n+3) {
  font-style: italic;
}

/* 选择倒数前2个子元素 */
li:nth-last-child(-n+2) {
  text-decoration: line-through;
}

2.2 使用公式选择子元素

/* 选择每隔2个子元素,从倒数第2个子元素开始 */
li:nth-last-child(2n+1) {
  background-color: #c0c0c0;
}

/* 选择每隔3个子元素,从倒数第4个子元素开始 */
li:nth-last-child(3n+4) {
  color: purple;
}

3. :nth-of-type() 选择器

:nth-of-type() 选择器用于选择父元素中特定类型的第n个子元素。它的语法如下:

:nth-of-type(an+b)

3.1 选择特定类型的子元素

/* 选择第2个<p>元素 */
p:nth-of-type(2) {
  color: orange;
}

/* 选择每隔2个<div>元素,从第2个<div>元素开始 */
div:nth-of-type(2n+2) {
  background-color: #b0b0b0;
}

3.2 使用公式选择子元素

/* 选择每隔3个<span>元素,从第4个<span>元素开始 */
span:nth-of-type(3n+4) {
  color: pink;
}

4. :nth-last-of-type() 选择器

:nth-last-of-type() 选择器与 :nth-of-type() 类似,但它从父元素的最后一个特定类型的子元素开始计数。它的语法如下:

:nth-last-of-type(an+b)

4.1 选择倒数第n个特定类型的子元素

/* 选择倒数第2个<p>元素 */
p:nth-last-of-type(2) {
  color: brown;
}

/* 选择倒数第3个及以后的<div>元素 */
div:nth-last-of-type(n+3) {
  font-weight: bold;
}

/* 选择倒数前2个<span>元素 */
span:nth-last-of-type(-n+2) {
  text-decoration: underline;
}

4.2 使用公式选择子元素

/* 选择每隔2个<p>元素,从倒数第2个<p>元素开始 */
p:nth-last-of-type(2n+1) {
  background-color: #a0a0a0;
}

/* 选择每隔3个<div>元素,从倒数第4个<div>元素开始 */
div:nth-last-of-type(3n+4) {
  color: teal;
}

5. 实际应用示例

5.1 斑马条纹表格

使用 :nth-child() 选择器可以轻松地为表格添加斑马条纹效果。

<table>
  <tr><th>Header 1</th><th>Header 2</th></tr>
  <tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr>
  <tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr>
  <tr><td>Row 3, Cell 1</td><td>Row 3, Cell 2</td></tr>
  <tr><td>Row 4, Cell 1</td><td>Row 4, Cell 2</td></tr>
</table>
/* 为表格的奇数行添加背景色 */
tr:nth-child(odd) {
  background-color: #f0f0f0;
}

/* 为表格的偶数行添加背景色 */
tr:nth-child(even) {
  background-color: #e0e0e0;
}

5.2 分栏布局

使用 :nth-of-type() 选择器可以为分栏布局中的每一列设置不同的样式。

<div class="columns">
  <div class="column">Column 1</div>
  <div class="column">Column 2</div>
  <div class="column">Column 3</div>
</div>
/* 为第1列设置背景色 */
.column:nth-of-type(1) {
  background-color: #d0d0d0;
}

/* 为第2列设置背景色 */
.column:nth-of-type(2) {
  background-color: #c0c0c0;
}

/* 为第3列设置背景色 */
.column:nth-of-type(3) {
  background-color: #b0b0b0;
}

5.3 响应式导航栏

使用 :nth-last-child() 选择器可以为响应式导航栏中的最后几个菜单项设置不同的样式。

<nav>
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Contact</a>
</nav>
/* 为最后2个菜单项设置不同的样式 */
a:nth-last-child(-n+2) {
  color: red;
  font-weight: bold;
}

6. 总结

CSS中的 nth 选择器为我们提供了强大的工具,可以根据元素在父元素中的位置来选择特定的子元素。通过 :nth-child():nth-last-child():nth-of-type():nth-last-of-type() 这些选择器,我们可以轻松地实现复杂的布局和样式效果。希望本文的示例和解释能够帮助你更好地理解和应用这些选择器。

推荐阅读:
  1. css中选择器nth-child()和nth-of-type()怎么用
  2. CSS中nth-of-type和nth-child有什么区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css

上一篇:css中伪类及伪对象指的是什么

下一篇:php如何去掉字符串的0

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》