mozilla

Revision 35200 of :only-child

  • Revision slug: CSS/:only-child
  • Revision title: :only-child
  • Revision id: 35200
  • Created:
  • Creator: FredB
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

{{ CSSRef() }}

Summary

The :only-child pseudo-class represents any element which is the only child of its parent. Same as :first-child:last-child or :nth-child(1):nth-last-child(1), but with a lower specificity.

Syntax

parent child:only-child {
    property:value;
} 

Examples

div span:only-child {
    color:red;
}
<div>
    <span>Some red text</span>
</div>

<div>
    <span>Some regular text</span>
    <span>Some regular text</span>
</div> 

Result

Some red text
Some regular text Some regular text

Specifications

Specification Status Comment
CSS Selectors Level 3 {{ Spec2('CSS3 Selectors') }}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 2 {{ CompatGeckoDesktop("1") }} 9 9.5 3.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 {{ CompatGeckoMobile("1") }} 9 10.0 3.1

{{ languages( { "fr": "fr/CSS/:only-child" }) }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2>Summary</h2>
<p>The <code>:only-child</code> <a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class</a> represents any element which is the only child of its parent. Same as <code>:first-child:last-child</code> or <code>:nth-child(1):nth-last-child(1)</code>, but with a lower specificity.</p><h2>Syntax</h2>
<pre>parent child:only-child {
    property:value;
} 
</pre>
<h2>Examples</h2>
<pre class="brush: css">div span:only-child {
    color:red;
}
</pre>
<pre class="brush: html">&lt;div&gt;
    &lt;span&gt;Some red text&lt;/span&gt;
&lt;/div&gt;

&lt;div&gt;
    &lt;span&gt;Some regular text&lt;/span&gt;
    &lt;span&gt;Some regular text&lt;/span&gt;
&lt;/div&gt; 
</pre>
<h3>Result</h3>
<div> <pre class="eval"><span style="color:red;">Some red text</span>
<span>Some regular text</span> <span>Some regular text</span></pre>
</div><h2 name="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><a class="external" href="http://www.w3.org/TR/css3-selectors/#only-child-pseudo" title="http://www.w3.org/TR/css3-selectors/#only-child-pseudo">CSS Selectors Level 3</a></td> <td>{{ Spec2('CSS3 Selectors') }}</td> <td> </td> </tr> </tbody>
</table>
<h2>Browser compatibility</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>2</td> <td>{{ CompatGeckoDesktop("1") }}</td> <td>9</td> <td>9.5</td> <td>3.1</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>2.1</td> <td>{{ CompatGeckoMobile("1") }}</td> <td>9</td> <td>10.0</td> <td>3.1</td> </tr> </tbody> </table>
</div>
<p>{{ languages( { "fr": "fr/CSS/:only-child" }) }}</p>
Revert to this revision