:first-child

  • Revision slug: CSS/:first-child
  • Revision title: :first-child
  • Revision id: 41233
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment 34 words added

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. :(

Specifications

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 {{ CompatGeckoDesktop("1.9") }} 7 [1] 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

[1] IE7 doesn't update the styles when elements are added dynamically. In Internet Explorer 8, if a 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>
<h3 name="Summary">Summary</h3>
<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>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">element:first-child { <em>style properties</em> }
</pre>
<h3 name="Examples">Examples</h3>
<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. :(&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. :(</span></p>
<h3 name="Notes">Specifications</h3>
<ul> <li><a class="external" href="http://www.w3.org/TR/css3-selectors/#first-child-pseudo">Selectors Level 3</a></li>
</ul>
<h3 class="editable"><span>Browser compatibility</span></h3>
<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 [1]</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>
<p>[1] IE7 doesn't update the styles when elements are added dynamically. In Internet Explorer 8, if a 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