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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>:first-child</title>
<style type='text/css'>
<!--
body { background: #fff; color: 000; font-family: Arial, Helvetica, sans-serif; }
pre { background: #fff; padding: 0.5em; }
li { background: #aaa; padding: 1em; width: 80%; margin: 0 0 3em; }
.test { display: block; padding: 0.75em; }
.base, .defaultgreen { background-color: #090; }
.defaultred { background-color: #900; }
.defaultred :first-child {
background-color: #090;
}
.defaultgreen :first-child {
background-color: #900;
}
blockquote {
margin: 0;
}
-->
</style>
</head>
<body>
<p>This page is part of the <a href="http://www.css3.info">CSS3.info</a> <a href="http://www.css3.info/selectors-test/">CSS selectors test</a>. See more info on <a href="http://www.css3.info/preview/attribute-selectors.html">CSS3 selectors</a>.</p>
<div class='base'></div>
<ol>
<li>
<div class='defaultred'>
<div class='test required'></div>
</div>
<pre>div :first-child {
}
<div>
<div></div>
</div></pre>
<p>
The CSS selector should match the inner div element, because it is the only child of the outer div element
</p>
</li>
<li>
<div class='defaultred'>
<div class='test'></div>
<blockquote></blockquote>
</div>
<pre>div :first-child {
}
<div>
<div></div>
<blockquote></blockquote>
</div></pre>
<p>
The CSS selector should match the inner div element, because it is the first child of the outer div element
</p>
</li>
<li>
<div class='defaultred'>
<!-- Just a comment -->
<div class='test'></div>
</div>
<pre>div :first-child {
}
<div>
<!-- Just a comment -->
<div></div>
</div></pre>
<p>
The CSS selector should match the inner div element, because it is the first child of the outer div element
Comments are not elements, so they should not be considered when determining the first child.
</p>
</li>
<li>
<div class='defaultred'>
.
<div class='test'></div>
</div>
<pre>div :first-child {
}
<div>
How about regular text...
<div></div>
</div></pre>
<p>
The CSS selector should match the inner div element, because it is the first child of the outer div element.
Regular text is not an element, so it should not be considered when determining the first child.
</p>
</li>
<li>
<div class='defaultgreen'>
<blockquote></blockquote>
<div class='test default required'></div>
</div>
<pre>div :first-child {
}
<div>
<blockquote></blockquote>
<div></div>
</div></pre>
<p>
The CSS selector should not match the inner div element, because it is the second child of the outer div element
</p>
</li>
<li>
<div class='defaultred'>
<div id='insertBefore1'></div>
</div>
<script type="text/javascript">
<!--
var ib = document.getElementById('insertBefore1');
var el = document.createElement("div");
el.className = 'test';
ib.parentNode.insertBefore(el, ib);
//-->
</script>
<pre>div :first-child {
}
<div>
<div id='insertBefore'></div>
</div>
var ib = document.getElementById('insertBefore');
ib.parentElement.insertBefore(document.createElement("div"), ib);</pre>
<p>
The CSS selector should match the div element that is inserted by the Javascript code.
</p>
</li>
<li>
<div class='defaultgreen'>
<div id='insertBefore2' class='test default'></div>
</div>
<script type="text/javascript">
<!--
var ib = document.getElementById('insertBefore2');
ib.parentNode.insertBefore(document.createElement("div"), ib);
//-->
</script>
<pre>div :first-child {
}
<div>
<div id='insertBefore'></div>
</div>
var ib = document.getElementById('insertBefore');
ib.parentElement.insertBefore(document.createElement("div"), ib);</pre>
<p>
The original div element should not be a match for the :first-child selector.
</p>
</li>
</ol>
</body>
</html>
|