blob: 7ab1d988ac1a3d7abe13cbce5ed317e6b6e0f7a8 (
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
|
---
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>
{{csssyntax}}
<h2 id="範例">範例</h2>
<h3 id="範例_1">範例 1</h3>
<h4 id="HTML_內文">HTML 內文</h4>
<pre class="brush: html"><div>
<span>This span is limed!</span>
<span>This span is not. :(</span>
</div>
</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"><ul>
<li>List 1</li>
<li>List 2</li>
<li>List 3</li>
</ul></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>
{{Compat("css.selectors.first-child")}}
<h2 id="參見">參見</h2>
<ul>
<li>{{cssxref(":last-child")}}</li>
<li>{{cssxref(":nth-child")}}</li>
<li>{{cssxref(":last-of-type")}}</li>
</ul>
|