Want to share your CSS expertise with others? Apply to the MDN Fellowship by April 1! http://mzl.la/MDNFellowship

mozilla

Revision 393881 of Child selectors

  • Revision slug: Web/CSS/Child_selectors
  • Revision title: Child selectors
  • Revision id: 393881
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment CSS/Child_selectors Web/CSS/Child_selectors

Revision Content

{{ CSSRef() }}

Summary

The > combinator separates two selectors and matches only those elements matched by the second selector that are direct children of elements matched by the first. By contrast, when two selectors are combined with the descendant selector, the combined selector expression matches those elements matched by the second selector for which there exists an ancestor element matched by the first selector, regardless of the number of "hops" up the DOM.

Syntax

selector1 > selector2 { style properties }

Examples

span { background-color: white; }
div > span {
  background-color: DodgerBlue;
}

when applied to markup as follows:

<div>
  <span>Span 1. In the div.
    <span>Span 2. In the span that's in the div.</span>
  </span>
</div>
<span>Span 3. Not in a div at all</span>

should result in:

Span 1. In the div. Span 2. In the span that's in the div.
Span 3. Not in a div at all.

Specifications

Specification Status Comment
CSS Selectors Level 3 {{ Spec2('CSS3 Selectors') }}  
CSS 2.1 {{ Spec2('CSS2.1') }}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} 7.0 {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>&gt;</code> combinator separates two selectors and matches only those elements matched by the second selector that are <strong>direct</strong> children of elements matched by the first. By contrast, when two selectors are combined with the <a href="/en/CSS/Descendant_selectors" title="https://developer.mozilla.org/en/CSS/Descendant_selectors">descendant selector</a>, the combined selector expression matches those elements matched by the second selector for which there exists an ancestor element matched by the first selector, regardless of the number of "hops" up the DOM.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">selector1 &gt; selector2 { <em>style properties</em> }
</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: css">span { background-color: white; }
div &gt; span {
  background-color: DodgerBlue;
}
</pre>
<p>when applied to markup as follows:</p>
<pre class="brush: html">&lt;div&gt;
  &lt;span&gt;Span 1. In the div.
    &lt;span&gt;Span 2. In the span that's in the div.&lt;/span&gt;
  &lt;/span&gt;
&lt;/div&gt;
&lt;span&gt;Span 3. Not in a div at all&lt;/span&gt;
</pre>
<p>should result in:</p>
<p style="margin-left: 40px;"><span style="background-color: DodgerBlue;">Span 1. In the div.</span> <span>Span 2. In the span that's in the div.</span><br>
  <span>Span 3. Not in a div at all.</span></p>
<h2 id="Specifications" 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/#child-combinators" title="http://www.w3.org/TR/css3-selectors/#child-combinators">CSS Selectors Level 3</a></td>
      <td>{{ Spec2('CSS3 Selectors') }}</td>
      <td> </td>
    </tr>
    <tr>
      <td><a class="external" href="http://www.w3.org/TR/CSS21/selector.html#child-selectors" title="http://www.w3.org/TR/CSS21/selector.html#child-selectors">CSS 2.1</a></td>
      <td>{{ Spec2('CSS2.1') }}</td>
      <td> </td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">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>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>7.0</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</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>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
Revert to this revision