aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_colon_only-child/index.html
blob: 1c8ba8f8dcad505bc45cf3aa57ddc3801bcd5a5a (plain)
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
---
title: ':only-child'
slug: 'Web/CSS/:only-child'
tags:
  - CSS
  - Layout
  - Pseudo-class
  - Reference
  - Web
translation_of: 'Web/CSS/:only-child'
---
<div>{{CSSRef}}</div>

<div></div>

<p>CSS<a href="/en-US/docs/CSS/Pseudo-classes" title="Pseudo-classes">伪类</a><code>:only-child</code> 匹配没有任何兄弟元素的元素.等效的选择器还可以写成 <code>:first-child:last-child</code>或者<code>:nth-child(1):nth-last-child(1)</code>,当然,前者的权重会低一点.</p>

<pre class="brush: css no-line-numbers">/* Selects each &lt;p&gt;, but only if it is the */
/* only child of its parent */
p:only-child {
  background-color: lime;
}</pre>

<div class="note">
<p><strong>Note</strong>: 在起初定义, 被选择的元素必须拥有一个父级元素. 从 Selectors Level 4 开始, 这不再是必须的了.</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">{{csssyntax}}
</pre>

<h2 id="示例">示例</h2>

<h3 id="基础示例">基础示例</h3>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">&lt;main&gt;
  &lt;div&gt;
    &lt;i&gt;I am a lonely only child.&lt;/i&gt;
  &lt;/div&gt;

  &lt;div&gt;
    &lt;i&gt;I have siblings.&lt;/i&gt;&lt;br&gt;
    &lt;b&gt;So do I!&lt;/b&gt;&lt;br&gt;
    &lt;span&gt;I also have siblings, &lt;span&gt;but this is an only child.&lt;/span&gt;&lt;/span&gt;
  &lt;/div&gt;
&lt;/main&gt;</pre>

<h4 id="CSS">CSS</h4>

<pre class="brush: css">main :only-child {
  color: red;
}
</pre>

<h4 id="结果">结果</h4>

<p>{{EmbedLiveSample('基础示例','100%',180)}}</p>

<h3 id="一个列表示例">一个列表示例</h3>

<h4 id="HTML_2">HTML</h4>

<pre class="brush: html">&lt;ol&gt;
  &lt;li&gt;First
    &lt;ul&gt;
      &lt;li&gt;This list has just one element.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Second
    &lt;ul&gt;
      &lt;li&gt;This list has three elements.&lt;/li&gt;
      &lt;li&gt;This list has three elements.&lt;/li&gt;
      &lt;li&gt;This list has three elements.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;
</pre>

<h4 id="CSS_2">CSS</h4>

<pre class="brush: css">li li {
  list-style-type: disc;
}
li:only-child {
  color: red;
  list-style-type: square;
}</pre>

<h4 id="结果_2">结果</h4>

<p>{{EmbedLiveSample('一个列表示例', '100%', 210)}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS4 Selectors', '#only-child-pseudo', ':only-child')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>Matching elements are not required to have a parent.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Selectors', '#only-child-pseudo', ':only-child')}}</td>
   <td>{{Spec2('CSS3 Selectors')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>
<div>


<p>{{Compat("css.selectors.only-child")}}</p>
</div>
</div>

<h2 id="另见">另见</h2>

<ul>
 <li>{{Cssxref(":only-of-type")}}</li>
 <li>{{Cssxref(":first-child")}}</li>
 <li>{{Cssxref(":last-child")}}</li>
 <li>{{Cssxref(":nth-child")}}</li>
</ul>