aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/css/_colon_first-child/index.html
blob: 195449ca3d85f843ac4dcb88030a511979ab4843 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
title: ':first-child'
slug: 'Web/CSS/:first-child'
tags:
  - CSS
  - CSS Pseudo-class
  - Layout
  - Reference
  - Web
translation_of: 'Web/CSS/:first-child'
---
<div>{{CSSRef}}</div>

<p><code>:first-child</code><a href="/en-US/docs/Web/CSS">CSS</a>當中的一種<a href="/en-US/docs/Web/CSS/Pseudo-classes">偽類別(pseudo-class)</a>,代表任何身為長子的元素(親元素的第一個子元素)。</p>

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

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

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

<h3 id="範例_1">範例 1</h3>

<h4 id="HTML_內文">HTML 內文</h4>

<pre class="brush: html">&lt;div&gt;
  &lt;span&gt;This span is limed!&lt;/span&gt;
  &lt;span&gt;This span is not. :(&lt;/span&gt;
&lt;/div&gt;
</pre>

<h4 id="CSS_內文">CSS 內文</h4>

<pre class="brush: css">span:first-child {
    background-color: lime;
}
</pre>

<p>呈現效果如下:</p>

<p><span>{{EmbedLiveSample('範例_1',300,50)}}</span></p>

<h3 id="範例_2_-_使用_UL">範例 2 - 使用 UL</h3>

<h4 id="HTML_內文_2">HTML 內文</h4>

<pre class="brush: html">&lt;ul&gt;
  &lt;li&gt;List 1&lt;/li&gt;
  &lt;li&gt;List 2&lt;/li&gt;
  &lt;li&gt;List 3&lt;/li&gt;
&lt;/ul&gt;</pre>

<h4 id="CSS_內文_2">CSS 內文</h4>

<pre class="brush: css">li{
  color:red;
}
li:first-child{
  color:green;
}</pre>

<p>呈現效果如下:</p>

<p><span>{{EmbedLiveSample('範例_2_-_使用_UL',300,100)}}</span></p>

<h2 id="規格">規格</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">規格</th>
   <th scope="col">狀態</th>
   <th scope="col">註解</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS4 Selectors', '#first-child-pseudo', ':first-child')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>無變更</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Selectors', '#first-child-pseudo', ':first-child')}}</td>
   <td>{{Spec2('CSS3 Selectors')}}</td>
   <td>無變更</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'selector.html#first-child', ':first-child')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>初始定義</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>功能</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>基礎支援</td>
   <td>4.0</td>
   <td>{{CompatGeckoDesktop("1.9")}}</td>
   <td>7<sup>[1]</sup></td>
   <td>9.5</td>
   <td>4</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>功能</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>基礎支援</td>
   <td>1.0</td>
   <td>{{CompatGeckoMobile("1.9.1")}}</td>
   <td>7<sup>[1]</sup></td>
   <td>10.0</td>
   <td>3.1</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] 在不重新載入網頁的情況下,Internet Explorer 7 的樣式表並不會套用在動態新增的元素上。在 Internet Explorer 8,若元素是以點擊連結而形成的,則 <code>first-child</code> 在該連結不被選取後才有作用。</p>

<h2 id="參見">參見</h2>

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