mozilla

Revision 494151 of :first-child

  • Revision slug: Web/CSS/:first-child
  • Revision title: :first-child
  • Revision id: 494151
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Updated tags

Revision Content

{{ CSSRef() }}

Summary

The :first-child CSS pseudo-class represents any element that is the first child element of its parent.

Syntax

element:first-child { style properties }

Examples

span:first-child {
    background-color: lime;
}

...where...

<div>
  <span>This span is limed!</span>
  <span>This span is not. :(</span>
</div>

... should look like ...
This span is limed! This span is not. :(

 

Example 2 - Using UL

HTML Content

<ul>
  <li>List 1</li>
  <li>List 2</li>
  <li>List 3</li>
</ul>

CSS Content

ul li{
  color:red;
}
ul li:first-child{
  color:green;
}
Live Sample : http://jsfiddle.net/Tripad/kvDa4/

 

Specifications

Specification Status Comment
{{ SpecName('CSS4 Selectors', '#first-child-pseudo', ':first-child') }} {{ Spec2('CSS4 Selectors') }} No change.
{{ SpecName('CSS3 Selectors', '#first-child-pseudo', ':first-child') }} {{ Spec2('CSS3 Selectors') }} Initial definition.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 {{ CompatGeckoDesktop("1.9") }} 7 9.5 4
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 {{ CompatGeckoMobile("1.9.1") }} 7 10.0 3.1

Internet Explorer notes

Internet Explorer 7 doesn't update the styles when elements are added dynamically. In Internet Explorer 8, if an element is inserted dynamically by clicking on a link the first-child style isn't applied until the link loses focus.

{{ languages( { "fr": "fr/CSS/:first-child", "pl": "pl/CSS/:first-child" } ) }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>:first-child</code> <a href="/en/CSS" title="CSS">CSS</a> <a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class </a>represents any element that is the first child element of its parent.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
element:first-child { <em>style properties</em> }
</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: css">
span:first-child {
    background-color: lime;
}
</pre>
<p>...where...</p>
<pre class="brush: html">
&lt;div&gt;
  &lt;span&gt;This span is limed!&lt;/span&gt;
  &lt;span&gt;This span is not.&nbsp;:(&lt;/span&gt;
&lt;/div&gt;
</pre>
<p>... should look like ...<br />
 <span style="background-color: lime;">This span is limed!</span> <span>This span is not.&nbsp;:(</span></p>
<p>&nbsp;</p>
<h2 id="Example_2_-_Using_UL" name="Example_2_-_Using_UL">Example 2 - Using UL</h2>
<h3 id="HTML_Content">HTML Content</h3>
<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>
<h3 id="CSS_Content">CSS Content</h3>
<pre class="brush: css">
ul li{
  color:red;
}
ul li:first-child{
  color:green;
}</pre>
<h5 id="Live_Sample_.3A_http.3A.2F.2Fjsfiddle.net.2FTripad.2FkvDa4.2F">Live Sample : <a href="http://jsfiddle.net/Tripad/kvDa4/embedded/result/">http://jsfiddle.net/Tripad/kvDa4/</a></h5>
<p>&nbsp;</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>No change.</td>
  </tr>
  <tr>
   <td>{{ SpecName('CSS3 Selectors', '#first-child-pseudo', ':first-child') }}</td>
   <td>{{ Spec2('CSS3 Selectors') }}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>
<h2 class="editable" id="Browser_compatibility"><span>Browser compatibility</span></h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>4.0</td>
    <td>{{ CompatGeckoDesktop("1.9") }}</td>
    <td>7</td>
    <td>9.5</td>
    <td>4</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>1.0</td>
    <td>{{ CompatGeckoMobile("1.9.1") }}</td>
    <td>7</td>
    <td>10.0</td>
    <td>3.1</td>
   </tr>
  </tbody>
 </table>
</div>
<h3 id="Internet_Explorer_notes">Internet Explorer notes</h3>
<p>Internet Explorer 7 doesn't update the styles when elements are added dynamically. In Internet Explorer 8, if an element is inserted dynamically by clicking on a link the first-child style isn't applied until the link loses focus.</p>
<p>{{ languages( { "fr": "fr/CSS/:first-child", "pl": "pl/CSS/:first-child" } ) }}</p>
Revert to this revision