- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 平移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪切
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块级尺寸
- CSS - 最小块级尺寸
- CSS - 混合模式
- CSS - 最大内联尺寸
- CSS - 最小内联尺寸
- CSS - 偏移量
- CSS - 重音颜色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要性
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰中断
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS 伪类 - :nth-child()
CSS :nth-child() 伪类根据元素在其父元素的子元素中的位置来选择元素。:nth-child() 伪类接受一个参数,该参数可以是数字、关键字或数学表达式。
语法
:nth-child(<nth> [of <complex-selector-list>]?) {
/* ... */
}
使用 :nth-child() CSS 伪类时,子元素计数包括所有兄弟姐妹子元素(任何元素类型),但只有与选择器其他组件匹配的元素才会被选中。
可能的值
-
odd - 此值选择所有奇数编号的子元素,例如 1、3、5 等。
-
even - 此值选择所有偶数编号的子元素,例如 2、4、6 等。
-
函数表示法 (<an+b>) - 此值从其父容器中选择每个第 an+b 个子元素,其中“a”是正整数,“n”是从 0 开始的计数器变量。“b”是另一个正整数。
下表描述了 CSS 选择器列表及其描述
| 选择器 | 描述 |
|---|---|
| tr:nth-child(odd) 或 tr:nth-last-child(2n+1) | HTML 表格中的所有奇数行。 |
| tr:nth-child(even) 或 tr:nth-last-child(2n) | HTML 表格中的所有偶数行。 |
| :nth-child(7) | 选择第七个元素。 |
| :nth-child(5n) | 选择是 5 的倍数的元素,例如第 5 个、第 10 个、第 15 个等。 |
| :nth-child(n+6) | 选择从第六个元素开始的所有元素。 |
| :nth-child(3n+4) | 选择是 3 的倍数加 4 的元素,例如第 4 个、第 7 个、第 10 个、第 13 个等。 |
| :nth-child(-n+3) | 选择前三个元素。 |
| p:nth-child(n) | 选择所有在一组兄弟姐妹中的 <p> 元素。 |
| p:nth-child(1) 或 p:nth-child(0n+1) | 选择所有其父元素的第一个子元素的 <p> 元素。 |
| p:nth-child(n+8):nth-child(-n+15) | 选择一组兄弟姐妹元素的第八到第十五个 <p> 元素。 |
CSS :nth-child 示例
以下是如何仅选择和设置段落元素样式的示例:
<html>
<head>
<style>
p, em {
display: inline-block;
border: 3px solid black;
margin: 5px;
padding: 5px;
}
.box1 p:nth-child(2n+1),
.box2 p:nth-child(2n+1),
.box3 p:nth-of-type(2n + 1) {
background-color: pink;
color: blue;
}
</style>
</head>
<body>
<h4>Styling all odd-numbered (1,3,5,7) paragraphs with a pink background and blue font color.</h4>
<div class="box1">
<p>Orange</p>
<p>Apple</p>
<p>Mango</p>
<p>Grapes</p>
<p>Banana</p>
<p>Watermelon</p>
<p>Cheery</p>
<p>Pear</p>
</div>
<h4>Styling all odd-numbered paragraphs (1, 3, 7, etc.) with a pink background and blue font color, except for sentence 5, which is not a paragraph element.</h4>
<div class="box2">
<p>Orange</p>
<p>Apple</p>
<p>Mango</p>
<p>Grapes</p>
<em>Banana</em>
<p>Watermelon</p>
<p>Cheery</p>
<p>Pear</p>
</div>
<h4>Styling paragraphs (1, 4, 6, 8 ) with a pink background and blue except sentence 3 because it's not a paragraph element.</h4>
<div class="box3">
<p>Orange</p>
<p>Apple</p>
<em>Mango</em>
<p>Grapes</p>
<p>Banana</p>
<p>Watermelon</p>
<p>Cheery</p>
<p>Pear</p>
</div>
</body>
</html>
CSS :nth-child() - 列表示例
以下是如何设置 ol 列表中 li 元素样式的示例:
<html>
<head>
<style>
li:nth-child(1) {
font-weight: bold;
color: red;
}
li:nth-child(3n+4){
background-color: pink;
}
li:nth-child(n+6){
font-weight: bold;
}
</style>
</head>
<body>
<p>Styling first list as bold and red.</p>
<p>Styling every third list item starting with the fourth list item with a pink background.</p>
<p>Styling all list items from the sixth list item onwards as bold.</p>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
</ol>
</body>
</html>
CSS :nth-child() - <selector>
带有 of <selector> 元素的 :nth-child() 伪类的语法如下
li:nth-child(even of .fruits) {
/* Your styles here */
}
将选择器 li.fruits 移到函数之外将选择所有具有 fruits 类的 li 元素,而不管它们在子元素列表中的位置。
li.fruits: nth-child(-n + 3);
以下是如何使用 :nth-child(even of .fruits) 伪类的示例。这将选择 .fruits 元素的所有偶数子元素:
<html>
<head>
<style>
ul {
list-style-type: none;
}
li {
display: inline-block;
border: 3px solid black;
margin: 5px;
padding: 5px;
}
.fruits {
background-color: pink;
}
li:nth-child(even of .fruits) {
font-weight: bold;
color: blue;
}
</style>
</head>
<body>
<h3>Selects all even-numbered child elements of the class .fruits.</h3>
<ul>
<li>Orange</li>
<li class="fruits">Apple</li>
<li class="fruits">Mango</li>
<li class="fruits">Grapes</li>
<li class="fruits">Banana</li>
<li>Watermelon</li>
<li class="fruits">Cheery</li>
<li class="fruits">Pear</li>
</ul>
</body>
</html>
CSS :nth-child() - <selector> 与 <selector> nth child
以下是如何使用 :nth-child() 伪类根据元素在其兄弟姐妹中的位置(相对于另一个元素)来选择元素的示例:
<html>
<head>
<style>
ul {
list-style-type: none;
}
li {
display: inline-block;
border: 3px solid black;
margin: 5px;
padding: 5px;
}
.fruits {
background-color: pink;
}
ul.list1 > li:nth-child(-n + 2 of .fruits) {
color: blue;
}
ul.list2 > li.fruits:nth-child(-n + 4) {
color: blue;
}
</style>
</head>
<body>
<p>Styling the first two child elements (with .fruits class) in the list.</p>
<ul class="list1">
<li>Orange</li>
<li class="fruits">Apple</li>
<li>Mango</li>
<li class="fruits">Grapes</li>
<li class="fruits">Banana</li>
<li>Watermelon</li>
<li>Cheery</li>
<li class="fruits">Pear</li>
</ul>
<p>Select the first four child elements that are both .fruits elements and in the .list2 list.</p>
<ul class="list2">
<li class="fruits">Orange</li>
<li>Apple</li>
<li class="fruits">Mango</li>
<li>Grapes</li>
<li>Banana</li>
<li class="fruits">Watermelon</li>
<li>Cheery</li>
<li class="fruits">Pear</li>
</ul>
</body>
</html>
CSS :nth-child() - 用于修复表格条带的选择器
以下是如何使用 :nth-child() 伪类设置表格中偶数行的样式的示例,即使某些行被隐藏:
<html>
<head>
<style>
table {
margin: 20px;
}
.even-rows > tbody > tr:nth-child(even) {
background-color: pink;
}
.even-rows-hidden > tbody > tr:nth-child(even of :not([hidden])) {
background-color: pink;
}
</style>
</head>
<body>
<p>Selector selects all even rows in the tbody of a table.</p>
<table class="even-rows">
<thead>
<tr>
<th>First Names</th>
<th>Last Names</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jhon</td>
<td>Sean</td>
</tr>
<tr>
<td>Rocky</td>
<td>Luis</td>
</tr>
<tr hidden>
<td>Oliver</td>
<td>David</td>
</tr>
<tr>
<td>Marry</td>
<td>Alamnd</td>
</tr>
<tr>
<td>Dora</td>
<td>Ann</td>
</tr>
<tr>
<td>Tonny</td>
<td>Rai</td>
</tr>
</tbody>
</table>
<p>Selector selects all even rows in the tbody of a table, but skips any rows that are hidden.</p>
<table class="even-rows-hidden">
<thead>
<tr>
<th>First Names</th>
<th>Last Names</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jhon</td>
<td>Sean</td>
</tr>
<tr>
<td>Rocky</td>
<td>Luis</td>
</tr>
<tr hidden>
<td>Oliver</td>
<td>David</td>
</tr>
<tr>
<td>Marry</td>
<td>Alamnd</td>
</tr>
<tr>
<td>Dora</td>
<td>Ann</td>
</tr>
<tr>
<td>Tonny</td>
<td>Rai</td>
</tr>
</tbody>
</table>
</body>
</html>
广告