General sibling selectors

  • Revision slug: Web/CSS/General_sibling_selectors
  • Revision title: General sibling selectors
  • Revision id: 461253
  • Created:
  • Creator: FredB
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}

Summary

The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.

Syntax

element ~ element { style properties }

Example

p ~ span {
  color: red;
}
<span>This is not red.</span>
<p>Here is a paragraph.</p>
<code>Here is some code.</code>
<span>And here is a span.</span>

{{ EmbedLiveSample('Example', '', '', '') }}

Specifications

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

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatUnknown}} {{CompatGeckoDesktop("1")}} 7 9 3
Feature Firefox Mobile (Gecko) Android IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • {{Cssxref("Adjacent_sibling_selectors", "Adjacent sibling selectors")}}

Revision Source

<div>
  {{CSSRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code>~</code> combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
element ~ element { <em>style properties</em> }
</pre>
<h2 id="Examples" name="Example">Example</h2>
<pre class="brush: css">
p ~ span {
  color: red;
}</pre>
<pre class="brush: html">
&lt;span&gt;This is not red.&lt;/span&gt;
&lt;p&gt;Here is a paragraph.&lt;/p&gt;
&lt;code&gt;Here is some code.&lt;/code&gt;
&lt;span&gt;And here is a span.&lt;/span&gt;</pre>
<p>{{ EmbedLiveSample('Example', '', '', '') }}</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/#general-sibling-combinators" title="http://www.w3.org/TR/css3-selectors/#general-sibling-combinators">CSS Selectors Level 3</a></td>
      <td>{{Spec2('CSS3 Selectors')}}</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>{{CompatUnknown}}</td>
        <td>{{CompatGeckoDesktop("1")}}</td>
        <td>7</td>
        <td>9</td>
        <td>3</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>Android</th>
        <th>IE Phone</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>
<ul>
  <li>{{Cssxref("Adjacent_sibling_selectors", "Adjacent sibling selectors")}}</li>
</ul>
Revert to this revision