1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1" />
6 <title>:only-child
</title>
8 <style type='text/css'
>
11 body
{ background: #fff; color: 000; font-family: Arial
, Helvetica
, sans-serif
; }
12 pre
{ background: #fff; padding: 0.5em; }
13 li
{ background: #aaa; padding: 1em; width: 80%; margin: 0 0 3em; }
14 .test { display: block
; padding: 0.75em; }
15 .base, .defaultgreen { background-color: #090; }
16 .defaultred { background-color: #900; }
18 .defaultred :only-child {
19 background-color: #090;
22 .defaultgreen :only-child {
23 background-color: #900;
31 <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>
33 <div class='base'
></div>
37 <div class='defaultred'
>
38 <div class='test required'
></div>
41 <pre>div :only-child {
45 <div
></div
>
49 The CSS selector should match the inner div element, because it is the only child of the outer div element
54 <div class='defaultred'
>
55 <div class='test'
></div>
56 <!-- Just a comment -->
59 <pre>div :only-child {
63 <div
></div
>
64 <!-- Just a comment --
>
68 The CSS selector should match the inner div element, because it is the only child of the outer div element
73 <div class='defaultred'
>
74 <div class='test'
></div>
78 <pre>div :only-child {
82 <div
></div
>
83 How about regular text...
87 The CSS selector should match the inner div element, because it is the only child of the outer div element
92 <div class='defaultgreen'
>
93 <div class='test default required'
></div>
94 <blockquote></blockquote>
97 <pre>div :only-child {
101 <div
></div
>
102 <blockquote
></blockquote
>
106 The CSS selector should not match the inner div element, because it not the only child
112 <div class='defaultgreen'
>
113 <div id='appendChild' class='test default'
></div>
116 <script type=
"text/javascript">
119 var ib
= document
.getElementById('appendChild');
120 ib
.parentNode
.appendChild(document
.createElement("div"));
125 <pre>div :only-child {
129 <div id='appendChild'
></div
>
132 var ib = document.getElementById('appendChild');
133 ib.parentElement.appendChild(document.createElement(
"div"));
</pre>
136 The CSS selector should not match the original div element, because it is
137 not the only child anymore after another child is append by the Javascript code.