Adjacent sibling selectors

  • Revision slug: Web/CSS/Adjacent_sibling_selectors
  • Revision title: Adjacent sibling selectors
  • Revision id: 466619
  • Created:
  • Creator: khs
  • Is current revision? No
  • Comment To make it very clear that + only selects the immediately adjacent element (contrast with ~ )

Revision Content

{{ CSSRef() }}

Summary

This is referred to as an adjacent selector. It will select only the element that is immediately preceded by the former element.

Syntax

former_element + target_element { style properties }

Example

li:first-of-type + li {
  color: red;
}
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>

{{ EmbedLiveSample('Example_1', '', '', '') }}

Another use case might be styling of "caption span"s of the following {{ HTMLElement("img") }} elements:

img + span.caption {
  font-style: italic;
}

will match the following {{ HTMLElement("span") }} elements:

<img src="photo1.jpg"><span class="caption">The first photo</span>
<img src="photo2.jpg"><span class="caption">The second photo</span>

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() }}
Note:
  • Internet Explorer 7 doesn't update the style correctly when an element is dynamically placed before an element that matched the selector.
  • 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.

See also

  • {{ Cssxref("General_sibling_selectors", "General sibling selectors") }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>This is referred to as an adjacent selector. It will select only the element that is immediately preceded by the former element.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre>
former_element + target_element { <em>style properties</em> }
</pre>
<h2 id="Example" name="Example">Example</h2>
<div id="Example_1" name="Example_1">
  <pre class="brush: css">
li:first-of-type + li {
  color: red;
}
</pre>
  <pre class="brush: html">
&lt;ul&gt;
  &lt;li&gt;One&lt;/li&gt;
  &lt;li&gt;Two&lt;/li&gt;
  &lt;li&gt;Three&lt;/li&gt;
&lt;/ul&gt;</pre>
</div>
<p>{{ EmbedLiveSample('Example_1', '', '', '') }}</p>
<p>Another use case might be styling of "caption span"s of the following {{ HTMLElement("img") }} elements:</p>
<pre class="brush: css">
img + span.caption {
  font-style: italic;
}
</pre>
<p>will match the following {{ HTMLElement("span") }} elements:</p>
<pre class="brush: html">
&lt;img src="photo1.jpg"&gt;&lt;span class="caption"&gt;The first photo&lt;/span&gt;
&lt;img src="photo2.jpg"&gt;&lt;span class="caption"&gt;The second photo&lt;/span&gt;
</pre>
<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/#adjacent-sibling-combinators" title="http://www.w3.org/TR/css3-selectors/#adjacent-sibling-combinators">CSS Selectors Level 3</a></td>
      <td>{{ Spec2('CSS3 Selectors') }}</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><a class="external" href="http://www.w3.org/TR/CSS21/selector.html#adjacent-selectors" title="http://www.w3.org/TR/CSS21/selector.html#adjacent-selectors">CSS 2.1</a></td>
      <td>{{ Spec2('CSS2.1') }}</td>
      <td>&nbsp;</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>
<div class="note">
  <strong>Note:</strong>
  <ul>
    <li>Internet Explorer 7 doesn't update the style correctly when an element is dynamically placed before an element that matched the selector.</li>
    <li>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.</li>
  </ul>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{ Cssxref("General_sibling_selectors", "General sibling selectors") }}</li>
</ul>
Revert to this revision