aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_colon_first-child/index.html
blob: 2ff9f8e726dcfcc6a3b078a59cb6b53478f2aea6 (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
---
title: ':first-child'
slug: 'Web/CSS/:first-child'
translation_of: 'Web/CSS/:first-child'
---
<div>{{CSSRef}}</div>

<p><strong><code>:first-child</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> <a href="/en-US/docs/Web/CSS/Pseudo-classes">pseudo-class</a> 表示在一组兄弟元素中的第一个元素。</p>

<pre class="brush: css no-line-numbers">/* Selects any &lt;p&gt; that is the first element
   among its siblings */
p:first-child {
  color: lime;
}</pre>

<div class="note">
<p><strong>Note</strong>: 最初定义时,所选元素必须有一个parent。而从选择器 Level 4 开始,parent不再是必须的。</p>
</div>

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

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

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

<h3 id="Basic_example">Basic example</h3>

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

<pre class="brush: html">&lt;div&gt;
  &lt;p&gt;This text is selected!&lt;/p&gt;
  &lt;p&gt;This text isn't selected.&lt;/p&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;h2&gt;This text isn't selected: it's not a `p`.&lt;/h2&gt;
  &lt;p&gt;This text isn't selected.&lt;/p&gt;
&lt;/div&gt;
</pre>

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

<pre class="brush: css">p:first-child {
  color: lime;
  background-color: black;
  padding: 5px;
}
</pre>

<h4 id="Result">Result</h4>

<p><span>{{EmbedLiveSample('Basic_example', 500, 200)}}</span></p>

<h3 id="Styling_a_list">Styling a list</h3>

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

<pre class="brush: html">&lt;ul&gt;
  &lt;li&gt;Item 1&lt;/li&gt;
  &lt;li&gt;Item 2&lt;/li&gt;
  &lt;li&gt;Item 3
    &lt;ul&gt;
      &lt;li&gt;Item 3.1&lt;/li&gt;
      &lt;li&gt;Item 3.2&lt;/li&gt;
      &lt;li&gt;Item 3.3&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</pre>

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

<pre class="brush: css">ul li {
  color: blue;
}

ul li:first-child {
  color: red;
  font-weight: bold;
}</pre>

<h4 id="Result_2">Result</h4>

<p><span>{{EmbedLiveSample('Styling_a_list')}}</span></p>

<h2 id="Specifications">Specifications</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', '#first-child-pseudo', ':first-child')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>Matching elements are not required to have a parent.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Selectors', '#first-child-pseudo', ':first-child')}}</td>
   <td>{{Spec2('CSS3 Selectors')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'selector.html#first-child', ':first-child')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

<div>


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

<h2 id="相关链接">相关链接</h2>

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