css伪类选择器系列二
# 概述
本文主要讲述CSS
的部分伪类选择器第二篇,包括::nth-child
、:nth-last-child
、:nth-of-type
和:nth-last-of-type
。
# :nth-child()
:nth-child
伪类是根据父元素的子元素列表中的索引来选择元素。
# 语法
:nth-child
是以一个参数nth
来描述匹配兄弟元素列表中元素索引的模式。元素索引从1开始。
:nth-child(nth) {
/** */
}
1
2
3
2
3
其中nth
可以是关键字也可以是函数符号
关键字 :
odd
表示奇数,如 1、3、5...;even
表示偶数,如 2、4、6。函数符号
<An+B>
:A
表示步长;B
表示偏移量;n
表示从 0 开始的整数,如5n+1
就是包含 1、6、11...nth of <selector>
:表示选择与<selector>
选择器匹配的第nth
个元素,这时的nth
可以是<An+B>
的模式或者非负整数
非负整数:
<nth>
也可以就是一个数值,如 1,3,6,8...
# 示例
- 效果
- 代码如下:
<style>
.first {
.children.pre span:nth-child(-n + 3) {
background: rgb(30, 120, 223);
}
.children.odd span:nth-child(odd) {
background: red;
}
.children.odd span:nth-child(2n + 1) {
border: 2px solid #000;
}
.children.even span:nth-child(even) {
background: orange;
}
.children.even span:nth-child(2n) {
border: 4px dotted green;
}
.children span:nth-child(-n + 3 of .im) {
background-color: gold;
}
}
</style>
<div class="order">
<h2><code>:nth-child</code></h2>
<div class="content first">
<div class="item">
<div class="children">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>-n+3:前3个背景为蓝色</h5>
<div class="children pre">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>odd:背景为红色;2n+1:边框加粗且颜色为黑色</h5>
<div class="children odd">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>odd:背景为橘色;2n+1:边框改为虚线加粗且颜色为绿色</h5>
<div class="children even">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>of <selector /> :子元素前3个`im`类的背景色改为金色</h5>
<div class="children of">
<span>1</span><span class="im">2</span><span>3</span><span>4</span
><span class="im">5</span> <span class="im">6</span><span>7</span
><span>8</span><span class="im">9</span><span>10</span>
</div>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# :nth-last-child()
:nth-last-child()
和:nth-child
的语法类似,不过:nth-last-child()
是从最后倒着计数,和后者相反。
# 示例
- 效果
- 代码如下:
<style>
.last {
.children.pre span:nth-last-child(-n + 3) {
background: rgb(30, 120, 223);
}
.children.odd span:nth-last-child(odd) {
background: red;
}
.children.odd span:nth-last-child(2n + 1) {
border: 2px solid #000;
}
.children.even span:nth-last-child(even) {
background: orange;
}
.children.even span:nth-last-child(2n) {
border: 4px dotted green;
}
.children span:nth-last-child(-n + 3 of .im) {
background-color: gold;
}
}
</style>
<h2><code>:nth-last-child</code></h2>
<div class="content last">
<div class="item">
<div class="children">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>-n+3:后3个背景为蓝色</h5>
<div class="children pre">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>odd:背景为红色;2n+1:边框加粗且颜色为黑色</h5>
<div class="children odd">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>odd:背景为橘色;2n+1:边框改为虚线且颜色为绿色</h5>
<div class="children even">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>of <selector /> :子元素后3个`im`类的背景色改为金色</h5>
<div class="children of">
<span>1</span><span class="im">2</span><span>3</span><span>4</span
><span class="im">5</span> <span class="im">6</span><span>7</span
><span>8</span><span class="im">9</span><span>10</span>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# :nth-of-type()
:nth-of-type()
伪类是基于相同类型(标签名称)的兄弟元素中的位置来匹配元素。
# 语法
:nth-of-type(nth) {
/** */
}
1
2
3
2
3
nth
表示匹配元素的模式,同上差不多。可以是关键词odd
或even
,也可以是<An+B>
,还可以是非负整数。
# 示例
- 效果
- 代码如下:
<style>
.first {
.children.odd span.light:nth-of-type(odd) {
color: red;
}
.children.even span.light:nth-of-type(even) {
color: rgb(99, 32, 32);
font-weight: bolder;
}
.children.blue span:nth-of-type(2n + 1) {
color: rgb(14, 53, 224);
font-weight: bolder;
}
}
</style>
<h2><code>:nth-of-type</code></h2>
<div class="content first">
<div class="item">
<div class="children">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>odd:颜色为红色</h5>
<div class="children odd">
<span>1</span>
<span class="light">2</span>
<span>3</span>
<span class="dark">4</span>
<span>5</span>
<span class="light">6</span>
<span class="light">7</span>
<span>8</span>
<span class="light">9</span>
<span>10</span>
</div>
</div>
<div class="item">
<h5>even:颜色为棕色</h5>
<div class="children even">
<span>1</span>
<span class="light">2</span>
<span>3</span>
<span class="dark">4</span>
<span>5</span>
<span class="light">6</span>
<span class="light">7</span>
<span>8</span>
<span class="light">9</span>
<span>10</span>
</div>
</div>
<div class="item">
<h5>2n+1:颜色为蓝色</h5>
<div class="children blue">
<span>1</span>
<span class="light">2</span>
<span>3</span>
<span class="dark">4</span>
<span>5</span>
<span class="light">6</span>
<span class="light">7</span>
<span>8</span>
<span class="light">9</span>
<span>10</span>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# :nth-last-of-type()
:nth-last-of-type()
和:nth-of-type
的语法类似,不过:nth-last-of-type()
是从最后倒着计数,和后者相反。
# 示例
- 效果
- 代码如下:
<style>
.last {
.children.odd span.light:nth-last-of-type(odd) {
color: red;
}
.children.even span.light:nth-last-of-type(even) {
color: rgb(99, 32, 32);
font-weight: bolder;
}
.children.blue span:nth-last-of-type(2n + 1) {
color: rgb(14, 53, 224);
font-weight: bolder;
}
}
</style>
<h2><code>:nth-of-last-type</code></h2>
<div class="content last">
<div class="item">
<div class="children">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
<span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
</div>
</div>
<div class="item">
<h5>odd:颜色为红色</h5>
<div class="children odd">
<span>1</span>
<span class="light">2</span>
<span>3</span>
<span class="dark">4</span>
<span>5</span>
<span class="light">6</span>
<span class="light">7</span>
<span>8</span>
<span class="light">9</span>
<span>10</span>
</div>
</div>
<div class="item">
<h5>even:颜色为棕色</h5>
<div class="children even">
<span>1</span>
<span class="light">2</span>
<span>3</span>
<span class="dark">4</span>
<span>5</span>
<span class="light">6</span>
<span class="light">7</span>
<span>8</span>
<span class="light">9</span>
<span>10</span>
</div>
</div>
<div class="item">
<h5>2n+1:颜色为蓝色</h5>
<div class="children blue">
<span>1</span>
<span class="light">2</span>
<span>3</span>
<span class="dark">4</span>
<span>5</span>
<span class="light">6</span>
<span class="light">7</span>
<span>8</span>
<span class="light">9</span>
<span>10</span>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
编辑 (opens new window)
上次更新: 2024/09/25, 06:33:24