-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSS-nth-child-pseudo-class.html
142 lines (141 loc) · 3.68 KB
/
CSS-nth-child-pseudo-class.html
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS nth Child Pseudo Selector </title>
<style>
h1{
text-align: center;
}
ol li:nth-child(1n){
color: green;
}
ol li:nth-child(2n){
color: red;
}
ul li:nth-last-child(3n){
background-color: yellow;
}
p:nth-child(2n+3){
color: turquoise;
/*now selector will identify every second element/tag/item starting with the third and then onward.
*/
}
.aaa p:nth-child(-n+6){
color: tomato;
/* the li:nth-child(-n+6) selector is ONLY identifying the top six items. */
}
div li:nth-child(-2n+5){
background-image: url(img/arrow.png);
background-repeat: no-repeat;
background-position: 0 50%;
padding-left: 25px;
background-size: contain;
list-style: none;
/* nth-child(-2n+5) selector identifies every second list item within the first five list items starting with the first list item */
}
nav li:nth-last-child(3n+2){
background-color: tomato;
}
</style>
</head>
<body>
<h1>:nth-child(n) & :nth-last-child(n)</h1>
<p>
The :nth-child(n) works from the beginning of the document tree while the :nth-last-child(n) works from the end of the
document tree.
</p>
<h3>:nth-child(n)</h3>
<ol>
<li>Green</li>
<li>Red</li>
<li>Green</li>
<li>Red</li>
<li>Green</li>
<li>Red</li>
<li>Green</li>
<li>Red</li>
<li>Green</li>
<li>Red</li>
</ol>
<br>
<h3>:nth-last-child(n)</h3>
<ul>
<li>Green</li>
<li>Yellow</li>
<li>Green</li>
<li>Red</li>
<li>Yellow</li>
<li>Red</li>
<li>Green</li>
<li>Yellow</li>
<li>Green</li>
<li>Red</li>
</ul>
<hr>
<hr>
<h3>nth-child(2n+3)</h3>
<blockquote>now selector will identify every second element/tag/item starting with the third and then onward.</blockquote>
<article>
<p>1.Peace</p>
<p>2.Peace Again</p>
<p>3.Peaceful</p>
<p>4.Peacefully</p>
<p>5.Peaceful Again</p>
<p>6.Peace</p>
<p>7.Peace Again</p>
<p>8.Peaceful</p>
<p>9.Peacefully</p>
<p>10.Peaceful Again</p>
</article>
<hr>
<h3>:nth-child(-n+6)</h3>
<blockquote>the li:nth-child(-n+6) selector is ONLY identifying the top six items</blockquote>
<article class="aaa">
<p>1.Peace</p>
<p>2.Peace Again</p>
<p>3.Peaceful</p>
<p>4.Peacefully</p>
<p>5.Peaceful Again</p>
<p>6.Peace</p>
<p>7.Peace Again</p>
<p>8.Peaceful</p>
<p>9.Peacefully</p>
<p>10.Peaceful Again</p>
</article>
<hr>
<h3>:nth-child(-2n+5)</h3>
<blockquote>nth-child(-2n+5) selector identifies every second list item within the first five list items starting with the first
list item</blockquote>
<div>
<li>Green</li>
<li>Yellow</li>
<li>Green</li>
<li>Red</li>
<li>Yellow</li>
<li>Red</li>
<li>Green</li>
<li>Yellow</li>
<li>Green</li>
<li>Red</li>
</div>
<hr>
<hr>
<h3>:nth-last-child(3n+2)</h3>
<blockquote>:nth-last-child(3n+2) selector, for example, will identify every third list item starting from the second to last item
in a list, moving towards the beginning of the list.</blockquote>
<nav>
<li>Green</li>
<li>Yellow</li>
<li>Green</li>
<li>Red</li>
<li>Yellow</li>
<li>Red</li>
<li>Green</li>
<li>Yellow</li>
<li>Green</li>
<li>Red</li>
</nav>
</body>
</html>