 
    
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>combinators Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/combinators/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/combinators/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Tue, 07 May 2019 18:21:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>CSS Combinators &#8211; Advanced CSS Selectors &#8211; Part 3</title>
		<link>https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-combinators-advanced-css-selectors-part-3/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Thu, 02 May 2019 14:58:31 +0000</pubDate>
				<category><![CDATA[Front-End Basics]]></category>
		<category><![CDATA[combinators]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[webdev]]></category>
		<guid isPermaLink="false">https://complete-concrete-concise.com/?p=3830</guid>

					<description><![CDATA[<p>Learn how to use CSS combinators to combine CSS selectors for more flexible and precise selection of HTML elements for styling.</p>
<p>The post <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-combinators-advanced-css-selectors-part-3/">CSS Combinators &#8211; Advanced CSS Selectors &#8211; Part 3</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="preamble">
<p>In the <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-specificity-advanced-css-selectors-part-2/">previous tutorial</a>, you have seen how to combine CSS selectors to precisely specify an HTML element for styling and the rules by which the <em>specificity</em> of a CSS selector is calculated.</p>
<p>You will learn about <strong>combinators</strong> for combining CSS selectors to more precisely select HTML elements for styling.</p>
</div>
<h2 id="contents">Contents</h2>
<ol class="incremental" type="1">
<li><a href="#child-combinator">Child Combinator</a></li>
<li><a href="#descendant-combinator">Descendant Combinator</a></li>
<li><a href="#next-sibling-combinator">Next Sibling Combinator</a></li>
<li><a href="#subsequent-sibling-combinator">Subsequent Sibling Combinator</a></li>
<li><a href="#closing-thoughts">Closing Thoughts</a></li>
<li><a href="#references">References</a></li>
</ol>
<h2 id="child-combinator">Child Combinator</h2>
<p>You were introduced to the <strong>Child Combinator</strong> in the first part of this series on <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/advanced-css-selectors-part-1/">Advanced CSS Selectors</a>:</p>
<div id="cb1" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb1-1" class="sourceLine" title="1"></a><span class="fu">.column-left</span> <span class="op">&gt;</span> <span class="fu">.card</span> <span class="op">&gt;</span> <span class="fu">.headline</span> {
<a id="cb1-2" class="sourceLine" title="2"></a>  <span class="kw">background-color</span>: <span class="cn">lightgreen</span><span class="op">;</span>
<a id="cb1-3" class="sourceLine" title="3"></a>}</code></pre>
</div>
<p>The combinator <strong>&gt;</strong> (greater-than symbol<a id="fnref1" class="footnote-ref" href="#fn1"><sup>1</sup></a>) specifies a direct parent-child relationship.</p>
<p>In the example above, it selects an element with the class of <span class="css-class">.headline</span> only if it is an immediate child of an element with the class of <span class="css-class">.card</span> AND the element with the class of <span class="css-class">.card</span> is an immediate child of an element with the class of <span class="css-class">.column-left</span>.</p>
<p>It has a <em>specificity</em> of 0:3:0.</p>
<p>Consider the following HTML code and the selector declared above:</p>
<div id="cb2" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb2-1" class="sourceLine" title="1"></a><span class="co">&lt;!--</span>
<a id="cb2-2" class="sourceLine" title="2"></a><span class="co">  The &lt;p&gt; tag will be selected because the</span>
<a id="cb2-3" class="sourceLine" title="3"></a><span class="co">  parent-child relationships match the selector.</span>
<a id="cb2-4" class="sourceLine" title="4"></a>
<a id="cb2-5" class="sourceLine" title="5"></a><span class="co">  You could also use the selector:</span>
<a id="cb2-6" class="sourceLine" title="6"></a>
<a id="cb2-7" class="sourceLine" title="7"></a><span class="co">  div &gt; div &gt; p</span>
<a id="cb2-8" class="sourceLine" title="8"></a>
<a id="cb2-9" class="sourceLine" title="9"></a><span class="co">  That selector has a specificity of 0:0:3</span>
<a id="cb2-10" class="sourceLine" title="10"></a><span class="co">--&gt;</span>
<a id="cb2-11" class="sourceLine" title="11"></a><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"column-left"</span><span class="kw">&gt;</span>
<a id="cb2-12" class="sourceLine" title="12"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"card"</span><span class="kw">&gt;</span>
<a id="cb2-13" class="sourceLine" title="13"></a>    <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">"headline"</span><span class="kw">&gt;</span>A Headline<span class="kw">&lt;/p&gt;</span>
<a id="cb2-14" class="sourceLine" title="14"></a>  <span class="kw">&lt;/div&gt;</span>
<a id="cb2-15" class="sourceLine" title="15"></a><span class="kw">&lt;/div&gt;</span>
<a id="cb2-16" class="sourceLine" title="16"></a>
<a id="cb2-17" class="sourceLine" title="17"></a><span class="co">&lt;!--</span>
<a id="cb2-18" class="sourceLine" title="18"></a><span class="co">  The &lt;p&gt; tag will NOT be selected because the</span>
<a id="cb2-19" class="sourceLine" title="19"></a><span class="co">  parent-child relationships do not match the</span>
<a id="cb2-20" class="sourceLine" title="20"></a><span class="co">  selector</span>
<a id="cb2-21" class="sourceLine" title="21"></a>
<a id="cb2-22" class="sourceLine" title="22"></a><span class="co">  To select the &lt;p&gt; tag, the selector could be:</span>
<a id="cb2-23" class="sourceLine" title="23"></a>
<a id="cb2-24" class="sourceLine" title="24"></a><span class="co">  .column-left  &gt; div &gt; .headline</span>
<a id="cb2-25" class="sourceLine" title="25"></a>
<a id="cb2-26" class="sourceLine" title="26"></a><span class="co">  That selector has a specificity of 0:2:1</span>
<a id="cb2-27" class="sourceLine" title="27"></a><span class="co">--&gt;</span>
<a id="cb2-28" class="sourceLine" title="28"></a><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"column-left"</span><span class="kw">&gt;</span>
<a id="cb2-29" class="sourceLine" title="29"></a>  <span class="kw">&lt;div&gt;</span>
<a id="cb2-30" class="sourceLine" title="30"></a>    <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">"headline"</span><span class="kw">&gt;</span>A Headline<span class="kw">&lt;/p&gt;</span>
<a id="cb2-31" class="sourceLine" title="31"></a>  <span class="kw">&lt;/div&gt;</span>
<a id="cb2-32" class="sourceLine" title="32"></a><span class="kw">&lt;/div&gt;</span>
<a id="cb2-33" class="sourceLine" title="33"></a>
<a id="cb2-34" class="sourceLine" title="34"></a><span class="co">&lt;!--</span>
<a id="cb2-35" class="sourceLine" title="35"></a><span class="co">  The &lt;p&gt; tag will NOT be selected because the</span>
<a id="cb2-36" class="sourceLine" title="36"></a><span class="co">  parent-child relationships do not match the</span>
<a id="cb2-37" class="sourceLine" title="37"></a><span class="co">  selector.</span>
<a id="cb2-38" class="sourceLine" title="38"></a>
<a id="cb2-39" class="sourceLine" title="39"></a><span class="co">  To select the &lt;p&gt; tag, the selector could be:</span>
<a id="cb2-40" class="sourceLine" title="40"></a>
<a id="cb2-41" class="sourceLine" title="41"></a><span class="co">  .card &gt; .column-left  &gt; .headline</span>
<a id="cb2-42" class="sourceLine" title="42"></a>
<a id="cb2-43" class="sourceLine" title="43"></a><span class="co">  That selector has a specificity of 0:3:0</span>
<a id="cb2-44" class="sourceLine" title="44"></a><span class="co">--&gt;</span>
<a id="cb2-45" class="sourceLine" title="45"></a><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"card"</span><span class="kw">&gt;</span>
<a id="cb2-46" class="sourceLine" title="46"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"column-left"</span><span class="kw">&gt;</span>
<a id="cb2-47" class="sourceLine" title="47"></a>    <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">"headline"</span><span class="kw">&gt;</span>A Headline<span class="kw">&lt;/p&gt;</span>
<a id="cb2-48" class="sourceLine" title="48"></a>  <span class="kw">&lt;/div&gt;</span>
<a id="cb2-49" class="sourceLine" title="49"></a><span class="kw">&lt;/div&gt;</span>
<a id="cb2-50" class="sourceLine" title="50"></a>
<a id="cb2-51" class="sourceLine" title="51"></a><span class="co">&lt;!--</span>
<a id="cb2-52" class="sourceLine" title="52"></a><span class="co">  The &lt;p&gt; tag WILL be selected because the</span>
<a id="cb2-53" class="sourceLine" title="53"></a><span class="co">  parent-child relationships match the selector.</span>
<a id="cb2-54" class="sourceLine" title="54"></a>
<a id="cb2-55" class="sourceLine" title="55"></a><span class="co">  Both &lt;p&gt; tags are children of the parent with</span>
<a id="cb2-56" class="sourceLine" title="56"></a><span class="co">  the class .card. The position of the child</span>
<a id="cb2-57" class="sourceLine" title="57"></a><span class="co">  within the parent doesn't affect its selection.</span>
<a id="cb2-58" class="sourceLine" title="58"></a><span class="co">  All that matters is that it is a direct child.</span>
<a id="cb2-59" class="sourceLine" title="59"></a><span class="co">--&gt;</span>
<a id="cb2-60" class="sourceLine" title="60"></a><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"column-left"</span><span class="kw">&gt;</span>
<a id="cb2-61" class="sourceLine" title="61"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"card"</span><span class="kw">&gt;</span>
<a id="cb2-62" class="sourceLine" title="62"></a>    <span class="kw">&lt;p&gt;</span>Some random text.<span class="kw">&lt;/p&gt;</span>
<a id="cb2-63" class="sourceLine" title="63"></a>    <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">"headline"</span><span class="kw">&gt;</span>A Headline<span class="kw">&lt;/p&gt;</span>
<a id="cb2-64" class="sourceLine" title="64"></a>  <span class="kw">&lt;/div&gt;</span>
<a id="cb2-65" class="sourceLine" title="65"></a><span class="kw">&lt;/div&gt;</span>
<a id="cb2-66" class="sourceLine" title="66"></a>
<a id="cb2-67" class="sourceLine" title="67"></a><span class="co">&lt;!--</span>
<a id="cb2-68" class="sourceLine" title="68"></a><span class="co">  The &lt;p&gt; tag will NOT be selected because the</span>
<a id="cb2-69" class="sourceLine" title="69"></a><span class="co">  parent-child relationships do not match the</span>
<a id="cb2-70" class="sourceLine" title="70"></a><span class="co">  selector.</span>
<a id="cb2-71" class="sourceLine" title="71"></a>
<a id="cb2-72" class="sourceLine" title="72"></a><span class="co">  To select the &lt;p&gt; tag, the selector could be:</span>
<a id="cb2-73" class="sourceLine" title="73"></a>
<a id="cb2-74" class="sourceLine" title="74"></a><span class="co">  .column-left &gt; .card &gt; div &gt; .headline</span>
<a id="cb2-75" class="sourceLine" title="75"></a>
<a id="cb2-76" class="sourceLine" title="76"></a><span class="co">  That selector has a specificity of 0:3:1</span>
<a id="cb2-77" class="sourceLine" title="77"></a>
<a id="cb2-78" class="sourceLine" title="78"></a><span class="co">  It could also be:</span>
<a id="cb2-79" class="sourceLine" title="79"></a>
<a id="cb2-80" class="sourceLine" title="80"></a><span class="co">  div &gt; div &gt; div &gt; p</span>
<a id="cb2-81" class="sourceLine" title="81"></a>
<a id="cb2-82" class="sourceLine" title="82"></a><span class="co">  That selector has a specificity of 0:0:4</span>
<a id="cb2-83" class="sourceLine" title="83"></a><span class="co">--&gt;</span>
<a id="cb2-84" class="sourceLine" title="84"></a><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"column-left"</span><span class="kw">&gt;</span>
<a id="cb2-85" class="sourceLine" title="85"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"card"</span><span class="kw">&gt;</span>
<a id="cb2-86" class="sourceLine" title="86"></a>    <span class="kw">&lt;div&gt;</span>
<a id="cb2-87" class="sourceLine" title="87"></a>      <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">"headline"</span><span class="kw">&gt;</span>A Headline<span class="kw">&lt;/p&gt;</span>
<a id="cb2-88" class="sourceLine" title="88"></a>    <span class="kw">&lt;/div&gt;</span>
<a id="cb2-89" class="sourceLine" title="89"></a>  <span class="kw">&lt;/div&gt;</span>
<a id="cb2-90" class="sourceLine" title="90"></a><span class="kw">&lt;/div&gt;</span></code></pre>
</div>
<p>While I wrote the selector with a space around the combinator, there is no need for that. The following child selectors are also valid:</p>
<div id="cb3" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb3-1" class="sourceLine" title="1"></a><span class="fu">.column-left</span><span class="op">&gt;</span><span class="fu">.card.</span><span class="op">&gt;</span><span class="fu">.headline</span> {
<a id="cb3-2" class="sourceLine" title="2"></a>
<a id="cb3-3" class="sourceLine" title="3"></a>}
<a id="cb3-4" class="sourceLine" title="4"></a>
<a id="cb3-5" class="sourceLine" title="5"></a>div<span class="op">&gt;</span>div<span class="op">&gt;</span>p {
<a id="cb3-6" class="sourceLine" title="6"></a>
<a id="cb3-7" class="sourceLine" title="7"></a>}</code></pre>
</div>
<p>While the direct selection method discussed in the <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-specificity-advanced-css-selectors-part-2/">previous tutorial</a> can never have an <span class="css-id">#ID</span> specificity greater than 1 (because an HTML element can never have more than 1 ID associated with it), with combinators, it is possible to have an <span class="css-id">#ID</span> specificity greater than 1. Consider the following (based on the <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-a-more-advanced-float-example-part-3/">More Advanced float Example</a>):</p>
<div id="cb4" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb4-1" class="sourceLine" title="1"></a><span class="pp">#wrapper</span> <span class="op">&gt;</span> <span class="pp">#title</span> {
<a id="cb4-2" class="sourceLine" title="2"></a>  <span class="co">/* specificity = 2:0:0 */</span>
<a id="cb4-3" class="sourceLine" title="3"></a>}
<a id="cb4-4" class="sourceLine" title="4"></a>
<a id="cb4-5" class="sourceLine" title="5"></a><span class="pp">#wrapper</span> <span class="op">&gt;</span> <span class="pp">#navbar</span> {
<a id="cb4-6" class="sourceLine" title="6"></a>  <span class="co">/* specificity = 2:0:0 */</span>
<a id="cb4-7" class="sourceLine" title="7"></a>}
<a id="cb4-8" class="sourceLine" title="8"></a>
<a id="cb4-9" class="sourceLine" title="9"></a><span class="pp">#wrapper</span> <span class="op">&gt;</span> <span class="pp">#content</span> <span class="op">&gt;</span> <span class="fu">.column-left</span> <span class="op">&gt;</span> <span class="fu">.card</span> <span class="op">&gt;</span> <span class="fu">.headline</span> {
<a id="cb4-10" class="sourceLine" title="10"></a>  <span class="co">/* specificity = 2:3:0 */</span>
<a id="cb4-11" class="sourceLine" title="11"></a>}</code></pre>
</div>
<h2 id="descendant-combinator">Descendant Combinator</h2>
<p>This is the most commonly used combinator.</p>
<p>The <strong>descendant combinator</strong> is more general than the <strong>child combinator</strong> and is useful for styling different sections of your document without having to specify the <strong>exact</strong> parent-child relationships.</p>
<p>As you recall, when you create your HTML page, <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/a-page-divided/">you <span class="html-tag">div</span>ide the page into various sections</a>. Common sections include:</p>
<ul class="incremental">
<li>Header</li>
<li>Navigation Bar</li>
<li>Main Body</li>
<li>Side Bar</li>
<li>Footer</li>
</ul>
<p>You might like to have common styles within a section, but not have to specify the exact relationship for every element in a section, but give a more general <em>“all <span class="html-tag">p</span> tags in the Main Body, regardless of how the main body is subdivided, have the following styling”</em>.</p>
<p>Consider the clone page <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-a-more-advanced-float-example-part-3/">created in an earlier lesson</a>, it had a <span class="css-id">#content</span> section that contained three columns:</p>
<p>You can style ALL <span class="html-tag">p</span> elements in the document using:</p>
<div id="cb5" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb5-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb5-2" class="sourceLine" title="2"></a><span class="co">  applies to all &lt;p&gt; tags, unless</span>
<a id="cb5-3" class="sourceLine" title="3"></a><span class="co">  overridden by a more specific selector</span>
<a id="cb5-4" class="sourceLine" title="4"></a><span class="co">*/</span>
<a id="cb5-5" class="sourceLine" title="5"></a>p {
<a id="cb5-6" class="sourceLine" title="6"></a>  <span class="co">/* specificity = 0:0:1 */</span>
<a id="cb5-7" class="sourceLine" title="7"></a>}</code></pre>
</div>
<p>You can also style all <span class="html-tag">p</span> elements that are immediate children of the <span class="css-id">#content</span> section using the <strong>child combinator</strong>:</p>
<div id="cb6" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb6-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb6-2" class="sourceLine" title="2"></a><span class="co">  Only applies to &lt;p&gt; elements that are</span>
<a id="cb6-3" class="sourceLine" title="3"></a><span class="co">  immediate children of a #content element,</span>
<a id="cb6-4" class="sourceLine" title="4"></a><span class="co">*/</span>
<a id="cb6-5" class="sourceLine" title="5"></a><span class="pp">#content</span> <span class="op">&gt;</span> p {
<a id="cb6-6" class="sourceLine" title="6"></a>  <span class="co">/* specificity = 1:0:1 */</span>  
<a id="cb6-7" class="sourceLine" title="7"></a>}</code></pre>
</div>
<p>But the <strong>child combinator</strong> will not select <span class="html-tag">p</span> elements if they are nested deeper:</p>
<div id="cb7" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb7-1" class="sourceLine" title="1"></a>
<a id="cb7-2" class="sourceLine" title="2"></a><span class="kw">&lt;div</span><span class="ot"> id=</span><span class="st">"content"</span><span class="kw">&gt;</span>
<a id="cb7-3" class="sourceLine" title="3"></a>  <span class="kw">&lt;p&gt;</span>This will be selected.<span class="kw">&lt;/p&gt;</span>
<a id="cb7-4" class="sourceLine" title="4"></a>  <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">"aside"</span><span class="kw">&gt;</span>
<a id="cb7-5" class="sourceLine" title="5"></a>    <span class="kw">&lt;p&gt;</span>This will NOT be selected<span class="kw">&lt;/p&gt;</span>
<a id="cb7-6" class="sourceLine" title="6"></a>  <span class="kw">&lt;/div&gt;</span>
<a id="cb7-7" class="sourceLine" title="7"></a><span class="kw">&lt;/div&gt;</span></code></pre>
</div>
<p>The <strong>descendant combinator</strong> allows you to create a selector for ALL <span class="html-tag">p</span> elements located inside a <span class="css-id">#content</span> container &#8211; you can select immediate children, grandchildren and more deeply nested elements. The <strong>descendant combinator</strong> is a space character:<a id="fnref2" class="footnote-ref" href="#fn2"><sup>2</sup></a></p>
<div id="cb8" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb8-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb8-2" class="sourceLine" title="2"></a><span class="co">  The descendant combinator is the space character.</span>
<a id="cb8-3" class="sourceLine" title="3"></a><span class="co">*/</span>
<a id="cb8-4" class="sourceLine" title="4"></a><span class="pp">#content</span> p {
<a id="cb8-5" class="sourceLine" title="5"></a>  <span class="co">/* specificity = 1:0:1 */</span>  
<a id="cb8-6" class="sourceLine" title="6"></a>}
<a id="cb8-7" class="sourceLine" title="7"></a>
<a id="cb8-8" class="sourceLine" title="8"></a><span class="co">/*</span>
<a id="cb8-9" class="sourceLine" title="9"></a><span class="co">  The space character acts as a descendant combinator</span>
<a id="cb8-10" class="sourceLine" title="10"></a><span class="co">  when it is alone. If you write a child combinator</span>
<a id="cb8-11" class="sourceLine" title="11"></a><span class="co">  with spaces it is a child combinator, not a</span>
<a id="cb8-12" class="sourceLine" title="12"></a><span class="co">  child + descendant combinator.</span>
<a id="cb8-13" class="sourceLine" title="13"></a><span class="co">*/</span>
<a id="cb8-14" class="sourceLine" title="14"></a><span class="pp">#content</span> <span class="op">&gt;</span> p {
<a id="cb8-15" class="sourceLine" title="15"></a>  <span class="co">/* specificity = 1:0:1 */</span>
<a id="cb8-16" class="sourceLine" title="16"></a>}</code></pre>
</div>
<p>This top selector will select <strong>all</strong> the <span class="html-tag">p</span> elements in the HTML example above, but if they are located anywhere inside a block with an ID of <span class="css-id">#content</span>.</p>
<p>The bottom selector will <strong>only</strong> select those the <span class="html-tag">p</span> elements which are immediate children of a block with an ID of <span class="css-id">#content</span>.</p>
<p>This is useful for defining styles that are localized to particular sections of your webpage without having to rigidly follow the structure of your page:</p>
<div id="cb9" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb9-1" class="sourceLine" title="1"></a><span class="pp">#header</span> p {
<a id="cb9-2" class="sourceLine" title="2"></a>  <span class="co">/*</span>
<a id="cb9-3" class="sourceLine" title="3"></a><span class="co">    applies to all &lt;p&gt; tags inside #header</span>
<a id="cb9-4" class="sourceLine" title="4"></a><span class="co">    specificity = 1:0:1</span>
<a id="cb9-5" class="sourceLine" title="5"></a><span class="co">  */</span>
<a id="cb9-6" class="sourceLine" title="6"></a>}
<a id="cb9-7" class="sourceLine" title="7"></a>
<a id="cb9-8" class="sourceLine" title="8"></a><span class="pp">#navbar</span> a{
<a id="cb9-9" class="sourceLine" title="9"></a>  <span class="co">/*</span>
<a id="cb9-10" class="sourceLine" title="10"></a><span class="co">    applies to all links inside #navbar</span>
<a id="cb9-11" class="sourceLine" title="11"></a><span class="co">    specificity = 1:0:1</span>
<a id="cb9-12" class="sourceLine" title="12"></a><span class="co">  */</span>
<a id="cb9-13" class="sourceLine" title="13"></a>}
<a id="cb9-14" class="sourceLine" title="14"></a>
<a id="cb9-15" class="sourceLine" title="15"></a><span class="pp">#content</span> <span class="fu">.sidebar</span> img {
<a id="cb9-16" class="sourceLine" title="16"></a>  <span class="co">/*</span>
<a id="cb9-17" class="sourceLine" title="17"></a><span class="co">    applies to all images located anywhere</span>
<a id="cb9-18" class="sourceLine" title="18"></a><span class="co">    inside .sidebar, when .sidebar is</span>
<a id="cb9-19" class="sourceLine" title="19"></a><span class="co">    located anywhere inside #content</span>
<a id="cb9-20" class="sourceLine" title="20"></a><span class="co">    specificity = 1:1:1</span>
<a id="cb9-21" class="sourceLine" title="21"></a><span class="co">  */</span>
<a id="cb9-22" class="sourceLine" title="22"></a>}
<a id="cb9-23" class="sourceLine" title="23"></a>
<a id="cb9-24" class="sourceLine" title="24"></a><span class="pp">#content</span> <span class="fu">.aside</span> <span class="op">&gt;</span> img {
<a id="cb9-25" class="sourceLine" title="25"></a>  <span class="co">/*</span>
<a id="cb9-26" class="sourceLine" title="26"></a><span class="co">    applies to images inside #content that</span>
<a id="cb9-27" class="sourceLine" title="27"></a><span class="co">    are direct children of .aside</span>
<a id="cb9-28" class="sourceLine" title="28"></a><span class="co">    specificity = 1:1:1</span>
<a id="cb9-29" class="sourceLine" title="29"></a><span class="co">  */</span>
<a id="cb9-30" class="sourceLine" title="30"></a>}
<a id="cb9-31" class="sourceLine" title="31"></a>
<a id="cb9-32" class="sourceLine" title="32"></a><span class="pp">#footer</span> img {
<a id="cb9-33" class="sourceLine" title="33"></a>  <span class="co">/*</span>
<a id="cb9-34" class="sourceLine" title="34"></a><span class="co">    applies to all images inside #footer</span>
<a id="cb9-35" class="sourceLine" title="35"></a><span class="co">    specificity = 1:0:1</span>
<a id="cb9-36" class="sourceLine" title="36"></a><span class="co">  */</span>
<a id="cb9-37" class="sourceLine" title="37"></a>}</code></pre>
</div>
<p>Remember to pay attention to the <em>specificity</em> of your selector:</p>
<div id="cb10" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb10-1" class="sourceLine" title="1"></a><span class="pp">#content</span> p {
<a id="cb10-2" class="sourceLine" title="2"></a>  <span class="co">/* specificity 1:0:1 */</span>
<a id="cb10-3" class="sourceLine" title="3"></a>}
<a id="cb10-4" class="sourceLine" title="4"></a>
<a id="cb10-5" class="sourceLine" title="5"></a><span class="fu">.aside</span> <span class="op">&gt;</span> p {
<a id="cb10-6" class="sourceLine" title="6"></a>  <span class="co">/* specificity 0:1:1 */</span>
<a id="cb10-7" class="sourceLine" title="7"></a>}
<a id="cb10-8" class="sourceLine" title="8"></a>
<a id="cb10-9" class="sourceLine" title="9"></a><span class="co">/*</span>
<a id="cb10-10" class="sourceLine" title="10"></a><span class="co">  Even though the bottom selector looks more "specific"</span>
<a id="cb10-11" class="sourceLine" title="11"></a><span class="co">  than the top selector, because the top selector says</span>
<a id="cb10-12" class="sourceLine" title="12"></a><span class="co">  "select any &lt;p&gt; found anywhere inside #content" and</span>
<a id="cb10-13" class="sourceLine" title="13"></a><span class="co">  the bottom selector says "only select &lt;p&gt; if it is</span>
<a id="cb10-14" class="sourceLine" title="14"></a><span class="co">  an immediate child of .aside".</span>
<a id="cb10-15" class="sourceLine" title="15"></a>
<a id="cb10-16" class="sourceLine" title="16"></a><span class="co">  However, the calculated specificity of the top selector</span>
<a id="cb10-17" class="sourceLine" title="17"></a><span class="co">  is greater than the specificity of the bottom selector.</span>
<a id="cb10-18" class="sourceLine" title="18"></a><span class="co">*/</span></code></pre>
</div>
<h2 id="next-sibling-combinator">Next Sibling Combinator</h2>
<p>This is also known as the <strong>first sibling combinator</strong> and <strong>adjacent sibling combinator</strong>.</p>
<p>The first two combinators allow you to select HTML elements based on how they are contained within other HTML elements &#8211; either as children, grandchildren, great-grandchildren, etc.</p>
<p>The <strong>next sibling combinator</strong> allows you to select HTML elements in relation to other elements at the same nesting level. Consider the following:</p>
<div id="cb11" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb11-1" class="sourceLine" title="1"></a><span class="co">&lt;!-- This is a parent element --&gt;</span>
<a id="cb11-2" class="sourceLine" title="2"></a><span class="kw">&lt;div&gt;</span>
<a id="cb11-3" class="sourceLine" title="3"></a>  <span class="co">&lt;!--</span>
<a id="cb11-4" class="sourceLine" title="4"></a><span class="co">    The &lt;h1&gt;, &lt;p&gt;, &lt;p&gt;, &lt;ol&gt;, &lt;img&gt;, and &lt;p&gt;</span>
<a id="cb11-5" class="sourceLine" title="5"></a><span class="co">    are siblings and children of &lt;div&gt;</span>
<a id="cb11-6" class="sourceLine" title="6"></a><span class="co">  --&gt;</span>
<a id="cb11-7" class="sourceLine" title="7"></a>  <span class="kw">&lt;h1&gt;</span>A Header<span class="kw">&lt;/h1&gt;</span>
<a id="cb11-8" class="sourceLine" title="8"></a>  <span class="kw">&lt;p&gt;</span>A paragraph.<span class="kw">&lt;/p&gt;</span>
<a id="cb11-9" class="sourceLine" title="9"></a>  <span class="kw">&lt;p&gt;</span>Another paragraph<span class="kw">&lt;/p&gt;</span>
<a id="cb11-10" class="sourceLine" title="10"></a>  <span class="co">&lt;!--</span>
<a id="cb11-11" class="sourceLine" title="11"></a><span class="co">    While &lt;ol&gt; is a child of &lt;div&gt; and a sibling</span>
<a id="cb11-12" class="sourceLine" title="12"></a><span class="co">    to &lt;h1&gt;, &lt;p&gt;, &lt;p&gt;, &lt;img&gt;, and &lt;p&gt;, it is also</span>
<a id="cb11-13" class="sourceLine" title="13"></a><span class="co">    the parent of the &lt;li&gt; elements.</span>
<a id="cb11-14" class="sourceLine" title="14"></a><span class="co">  --&gt;</span>
<a id="cb11-15" class="sourceLine" title="15"></a>  <span class="kw">&lt;ol&gt;</span>
<a id="cb11-16" class="sourceLine" title="16"></a>    <span class="co">&lt;!-- All the &lt;li&gt; elements are siblings --&gt;</span>
<a id="cb11-17" class="sourceLine" title="17"></a>    <span class="kw">&lt;li&gt;</span>Item 1<span class="kw">&lt;/li&gt;</span>
<a id="cb11-18" class="sourceLine" title="18"></a>    <span class="kw">&lt;li&gt;</span>Item 2<span class="kw">&lt;/li&gt;</span>
<a id="cb11-19" class="sourceLine" title="19"></a>    <span class="kw">&lt;li&gt;</span>Item 3<span class="kw">&lt;/li&gt;</span>
<a id="cb11-20" class="sourceLine" title="20"></a>  <span class="kw">&lt;/ol&gt;</span>
<a id="cb11-21" class="sourceLine" title="21"></a>  <span class="kw">&lt;img</span><span class="ot"> src=</span><span class="st">"/valid/image.jpg"</span><span class="kw">&gt;</span>
<a id="cb11-22" class="sourceLine" title="22"></a>  <span class="kw">&lt;p&gt;</span>Maybe a caption?<span class="kw">&lt;/p&gt;</span>
<a id="cb11-23" class="sourceLine" title="23"></a><span class="kw">&lt;/div&gt;</span></code></pre>
</div>
<p>Consider the following rules using the <strong>next sibling combinator</strong> (which is the plus ‘+’ symbol):</p>
<div id="cb12" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb12-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb12-2" class="sourceLine" title="2"></a><span class="co">  Select a &lt;p&gt; element if it immediately follows</span>
<a id="cb12-3" class="sourceLine" title="3"></a><span class="co">  an &lt;h1&gt; element.</span>
<a id="cb12-4" class="sourceLine" title="4"></a>
<a id="cb12-5" class="sourceLine" title="5"></a><span class="co">  You might want the first paragraph following</span>
<a id="cb12-6" class="sourceLine" title="6"></a><span class="co">  &lt;h1&gt; to be special in some way - perhaps a</span>
<a id="cb12-7" class="sourceLine" title="7"></a><span class="co">  slightly larger font or italicized. This could</span>
<a id="cb12-8" class="sourceLine" title="8"></a><span class="co">  also be done by applying a class to the first</span>
<a id="cb12-9" class="sourceLine" title="9"></a><span class="co">  &lt;p&gt; element.</span>
<a id="cb12-10" class="sourceLine" title="10"></a><span class="co">*/</span>
<a id="cb12-11" class="sourceLine" title="11"></a>h1 <span class="op">+</span> p {
<a id="cb12-12" class="sourceLine" title="12"></a>  <span class="co">/* specificity = 0:0:2 */</span>
<a id="cb12-13" class="sourceLine" title="13"></a>}
<a id="cb12-14" class="sourceLine" title="14"></a>
<a id="cb12-15" class="sourceLine" title="15"></a><span class="co">/*</span>
<a id="cb12-16" class="sourceLine" title="16"></a><span class="co">  Select a &lt;p&gt; element if it immediately follows</span>
<a id="cb12-17" class="sourceLine" title="17"></a><span class="co">  an &lt;img&gt; element.</span>
<a id="cb12-18" class="sourceLine" title="18"></a>
<a id="cb12-19" class="sourceLine" title="19"></a><span class="co">  You might use the convention that each image</span>
<a id="cb12-20" class="sourceLine" title="20"></a><span class="co">  in your documents must be followed by a caption.</span>
<a id="cb12-21" class="sourceLine" title="21"></a><span class="co">  You could also apply a class (like .caption) to</span>
<a id="cb12-22" class="sourceLine" title="22"></a><span class="co">  the &lt;p&gt; element instead.</span>
<a id="cb12-23" class="sourceLine" title="23"></a><span class="co">*/</span>
<a id="cb12-24" class="sourceLine" title="24"></a>img p {
<a id="cb12-25" class="sourceLine" title="25"></a>  <span class="co">/* specificity = 0:0:2 */</span>
<a id="cb12-26" class="sourceLine" title="26"></a>}
<a id="cb12-27" class="sourceLine" title="27"></a>
<a id="cb12-28" class="sourceLine" title="28"></a><span class="co">/*</span>
<a id="cb12-29" class="sourceLine" title="29"></a><span class="co">  Select an &lt;li&gt; element if it immediately follows</span>
<a id="cb12-30" class="sourceLine" title="30"></a><span class="co">  an &lt;li&gt; element.</span>
<a id="cb12-31" class="sourceLine" title="31"></a>
<a id="cb12-32" class="sourceLine" title="32"></a><span class="co">  Applied to the HTML code above, it will</span>
<a id="cb12-33" class="sourceLine" title="33"></a><span class="co">  select the &lt;li&gt;s containing 'Item 2' and</span>
<a id="cb12-34" class="sourceLine" title="34"></a><span class="co">  'Item 3' - this is because:</span>
<a id="cb12-35" class="sourceLine" title="35"></a><span class="co">  - &lt;li&gt;Item 1&lt;/li&gt; is the first li in the</span>
<a id="cb12-36" class="sourceLine" title="36"></a><span class="co">    rule and &lt;li&gt;Item 2&lt;/li&gt; is the second</span>
<a id="cb12-37" class="sourceLine" title="37"></a><span class="co">    li in the rule - the one that is</span>
<a id="cb12-38" class="sourceLine" title="38"></a><span class="co">    selected.</span>
<a id="cb12-39" class="sourceLine" title="39"></a><span class="co">  - then &lt;li&gt;Item 2&lt;/li&gt; is the first li in</span>
<a id="cb12-40" class="sourceLine" title="40"></a><span class="co">    the rule and &lt;li&gt;Item 3&lt;/li&gt; is the second</span>
<a id="cb12-41" class="sourceLine" title="41"></a><span class="co">    li in the rule - the one that is</span>
<a id="cb12-42" class="sourceLine" title="42"></a><span class="co">    selected.</span>
<a id="cb12-43" class="sourceLine" title="43"></a><span class="co">  - when &lt;li&gt;Item 3&lt;/li&gt; is evaluated as the</span>
<a id="cb12-44" class="sourceLine" title="44"></a><span class="co">    first li in the rule, because there is</span>
<a id="cb12-45" class="sourceLine" title="45"></a><span class="co">    no subsequent li, this selector isn't</span>
<a id="cb12-46" class="sourceLine" title="46"></a><span class="co">    applied to anything.</span>
<a id="cb12-47" class="sourceLine" title="47"></a><span class="co">*/</span>
<a id="cb12-48" class="sourceLine" title="48"></a>
<a id="cb12-49" class="sourceLine" title="49"></a>li <span class="op">+</span> li {
<a id="cb12-50" class="sourceLine" title="50"></a>  <span class="co">/* specificity = 0:0:2 */</span>
<a id="cb12-51" class="sourceLine" title="51"></a>}</code></pre>
</div>
<p>Let’s apply these selectors to the HTML code above:</p>
<div id="cb13" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb13-1" class="sourceLine" title="1"></a>h1 <span class="op">+</span> p {
<a id="cb13-2" class="sourceLine" title="2"></a>  <span class="kw">font-size</span>: <span class="dv">20</span><span class="dt">px</span><span class="op">;</span>
<a id="cb13-3" class="sourceLine" title="3"></a>  <span class="kw">font-style</span>: <span class="dv">italic</span><span class="op">;</span>
<a id="cb13-4" class="sourceLine" title="4"></a>}
<a id="cb13-5" class="sourceLine" title="5"></a>
<a id="cb13-6" class="sourceLine" title="6"></a>img <span class="op">+</span> p {
<a id="cb13-7" class="sourceLine" title="7"></a>  <span class="kw">font-weight</span>: <span class="dv">bold</span><span class="op">;</span>
<a id="cb13-8" class="sourceLine" title="8"></a>  <span class="kw">text-align</span>: <span class="dv">center</span><span class="op">;</span>
<a id="cb13-9" class="sourceLine" title="9"></a>}
<a id="cb13-10" class="sourceLine" title="10"></a>
<a id="cb13-11" class="sourceLine" title="11"></a><span class="co">/*</span>
<a id="cb13-12" class="sourceLine" title="12"></a><span class="co">  Remove the yellow background from all list</span>
<a id="cb13-13" class="sourceLine" title="13"></a><span class="co">  items, except the first one.</span>
<a id="cb13-14" class="sourceLine" title="14"></a><span class="co">*/</span>
<a id="cb13-15" class="sourceLine" title="15"></a>li <span class="op">+</span> li {
<a id="cb13-16" class="sourceLine" title="16"></a><span class="kw">background-color</span>: <span class="dv">transparent</span><span class="op">;</span>
<a id="cb13-17" class="sourceLine" title="17"></a>}
<a id="cb13-18" class="sourceLine" title="18"></a>
<a id="cb13-19" class="sourceLine" title="19"></a><span class="co">/*</span>
<a id="cb13-20" class="sourceLine" title="20"></a><span class="co">  Select all list items and set the</span>
<a id="cb13-21" class="sourceLine" title="21"></a><span class="co">  background to yellow.</span>
<a id="cb13-22" class="sourceLine" title="22"></a>
<a id="cb13-23" class="sourceLine" title="23"></a><span class="co">  I'm putting this rule here to demonstrate</span>
<a id="cb13-24" class="sourceLine" title="24"></a><span class="co">  selector specificity.</span>
<a id="cb13-25" class="sourceLine" title="25"></a><span class="co">*/</span>
<a id="cb13-26" class="sourceLine" title="26"></a>li {
<a id="cb13-27" class="sourceLine" title="27"></a>  <span class="kw">background-color</span>: <span class="cn">yellow</span><span class="op">;</span>
<a id="cb13-28" class="sourceLine" title="28"></a>}</code></pre>
</div>
<p>Results in the following output:</p>
<div class="html-output">
<style>
h1.x39 + p.x39 {font-size: 20px;  font-style: italic;}p.x39f + p.x39 { font-weight: bold;  text-align: center;}li.x39 + li.x39 {background-color: transparent;}li.x39 {  background-color: yellow;}</style>
<div class="x39">
<h1 class="x39">A Header</h1>
<p class="x39">A paragraph.</p>
<p class="x39">Another paragraph</p>
<ol class="x39">
<li class="x39">Item 1</li>
<li class="x39">Item 2</li>
<li class="x39">Item 3</li>
</ol>
<p class="x39f"><img decoding="async" class="x39" src="https://complete-concrete-concise.com/wp-content/uploads/2018/05/26-dog-compressor.jpg" /></p>
<p class="x39">Maybe a caption?</p>
</div>
</div>
<h2 id="subsequent-sibling-combinator">Subsequent Sibling Combinator</h2>
<p>This is also known as the <strong>general sibling combinator</strong> and <strong>following sibling combinator</strong><a id="fnref4" class="footnote-ref" href="#fn4"><sup>4</sup></a>.</p>
<p>It is similar to the <strong>next sibling combinator</strong> in specifying the relationship between child elements, except that:</p>
<ol class="incremental" type="1">
<li>the selected element does not need to be adjacent to the first element</li>
<li>it selects ALL subsequent elements that match</li>
</ol>
<p>The combinator is the tilde (<strong>~</strong>) symbol.</p>
<p>Consider:</p>
<div id="cb14" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb14-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb14-2" class="sourceLine" title="2"></a><span class="co">  Select all &lt;p&gt; elements following an &lt;h1&gt;</span>
<a id="cb14-3" class="sourceLine" title="3"></a><span class="co">  element as long as the &lt;p&gt; elements are</span>
<a id="cb14-4" class="sourceLine" title="4"></a><span class="co">  all siblings with the &lt;h1&gt; element.</span>
<a id="cb14-5" class="sourceLine" title="5"></a><span class="co">*/</span>
<a id="cb14-6" class="sourceLine" title="6"></a>h1<span class="fu">.important</span> <span class="op">~</span> p {
<a id="cb14-7" class="sourceLine" title="7"></a>  <span class="co">/* specificity = 0:1:2 */</span>
<a id="cb14-8" class="sourceLine" title="8"></a>  <span class="kw">background-color</span>: <span class="cn">yellow</span><span class="op">;</span>
<a id="cb14-9" class="sourceLine" title="9"></a>}</code></pre>
</div>
<p>When applied to the following code:</p>
<div id="cb15" class="sourceCode">
<pre class="sourceCode html"><code class="sourceCode html"><a id="cb15-1" class="sourceLine" title="1"></a><span class="kw">&lt;p&gt;</span>This is a paragraph.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-2" class="sourceLine" title="2"></a><span class="kw">&lt;p&gt;</span>So is this.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-3" class="sourceLine" title="3"></a><span class="kw">&lt;h1</span><span class="ot"> class=</span><span class="st">"important"</span><span class="kw">&gt;</span>This is important!<span class="kw">&lt;/h1&gt;</span>
<a id="cb15-4" class="sourceLine" title="4"></a><span class="kw">&lt;p&gt;</span>This paragraph is selected.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-5" class="sourceLine" title="5"></a><span class="kw">&lt;p&gt;</span>So is this one.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-6" class="sourceLine" title="6"></a><span class="kw">&lt;div&gt;</span>
<a id="cb15-7" class="sourceLine" title="7"></a>  <span class="kw">&lt;p&gt;</span>These paragraphs are not selected.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-8" class="sourceLine" title="8"></a>  <span class="kw">&lt;p&gt;</span>Because they are not siblings.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-9" class="sourceLine" title="9"></a>  <span class="kw">&lt;p&gt;</span>They are nested inside a div.<span class="kw">&lt;/p&gt;</span>
<a id="cb15-10" class="sourceLine" title="10"></a><span class="kw">&lt;/div&gt;</span>
<a id="cb15-11" class="sourceLine" title="11"></a><span class="kw">&lt;p&gt;</span>But this paragraph is selected.<span class="kw">&lt;/p&gt;</span></code></pre>
</div>
<p>Results in the following:</p>
<div class="html-output">
<style>
h1.important.x39a ~ p.x39a {  background-color: yellow;}</style>
<p class="x39a">This is a paragraph.</p>
<p class="x39a">So is this.</p>
<h1 class="important x39a">This is important!</h1>
<p class="x39a">This paragraph is selected.</p>
<p class="x39a">So is this one.</p>
<div class="x39a">
<p class="x39a">These paragraphs are not selected.</p>
<p class="x39a">Because they are not siblings.</p>
<p class="x39a">They are nested inside a div.</p>
</div>
<p class="x39a">But this paragraph is selected.</p>
</div>
<h2 id="closing-thoughts">Closing Thoughts</h2>
<ol class="incremental" type="1">
<li>The four types of combinators allow you to combine various selectors to create selectors based on their relationships. This allows for more powerful selection.</li>
<li>The <strong>child combinator</strong> allows you to precisely specify the parent-child relationship of the elements:</li>
</ol>
<div id="cb16" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb16-1" class="sourceLine" title="1"></a>parent <span class="op">&gt;</span> child <span class="op">&gt;</span> grandchild {
<a id="cb16-2" class="sourceLine" title="2"></a>
<a id="cb16-3" class="sourceLine" title="3"></a>}</code></pre>
</div>
<ol class="incremental" start="3" type="1">
<li>The <strong>descendant combinator</strong> allows you to target elements based on whether they are nested inside other elements without caring about the specific details of the nesting:</li>
</ol>
<div id="cb17" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb17-1" class="sourceLine" title="1"></a>outer-element inner-element {
<a id="cb17-2" class="sourceLine" title="2"></a>
<a id="cb17-3" class="sourceLine" title="3"></a>}</code></pre>
</div>
<ol class="incremental" start="4" type="1">
<li>The <strong>next sibling combinator</strong> allows you to select an element if it immediately follows another element:</li>
</ol>
<div id="cb18" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb18-1" class="sourceLine" title="1"></a>first-element <span class="op">+</span> adjacent-element {
<a id="cb18-2" class="sourceLine" title="2"></a>
<a id="cb18-3" class="sourceLine" title="3"></a>}</code></pre>
</div>
<ol class="incremental" start="5" type="1">
<li>The <strong>subsequent sibling combinator</strong> allows you to select all elements that follow the first element &#8211; as long as they are siblings:</li>
</ol>
<div id="cb19" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb19-1" class="sourceLine" title="1"></a>key-element <span class="op">~</span> following-elements {
<a id="cb19-2" class="sourceLine" title="2"></a>
<a id="cb19-3" class="sourceLine" title="3"></a>}</code></pre>
</div>
<ol class="incremental" start="6" type="1">
<li>The combinators can be combined and you can more use more specific combinations of html elements, IDs, classes, and attributes:</li>
</ol>
<div id="cb20" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb20-1" class="sourceLine" title="1"></a>div<span class="pp">#main</span> <span class="op">&gt;</span> p<span class="fu">.important</span> <span class="op">+</span> <span class="ex">[reversed]</span> {
<a id="cb20-2" class="sourceLine" title="2"></a>
<a id="cb20-3" class="sourceLine" title="3"></a>}</code></pre>
</div>
<ol class="incremental" start="7" type="1">
<li>There is no combinator that allows you to select <strong>preceding</strong> elements:</li>
</ol>
<div id="cb21" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb21-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb21-2" class="sourceLine" title="2"></a><span class="co">  There is no select parent combinator.</span>
<a id="cb21-3" class="sourceLine" title="3"></a>
<a id="cb21-4" class="sourceLine" title="4"></a><span class="co">  For example, you cannot style the parent</span>
<a id="cb21-5" class="sourceLine" title="5"></a><span class="co">  based on the child tag.</span>
<a id="cb21-6" class="sourceLine" title="6"></a><span class="co">*/</span>
<a id="cb21-7" class="sourceLine" title="7"></a>div &lt; p {
<a id="cb21-8" class="sourceLine" title="8"></a>  <span class="co">/* This does NOT exist */</span>
<a id="cb21-9" class="sourceLine" title="9"></a>}
<a id="cb21-10" class="sourceLine" title="10"></a>
<a id="cb21-11" class="sourceLine" title="11"></a><span class="co">/*</span>
<a id="cb21-12" class="sourceLine" title="12"></a><span class="co">  There is NO preceding adjacent sibling</span>
<a id="cb21-13" class="sourceLine" title="13"></a><span class="co">  combinator.</span>
<a id="cb21-14" class="sourceLine" title="14"></a>
<a id="cb21-15" class="sourceLine" title="15"></a><span class="co">  For example, you could not select an &lt;h1&gt;</span>
<a id="cb21-16" class="sourceLine" title="16"></a><span class="co">  element that is followed by a &lt;p&gt; element</span>
<a id="cb21-17" class="sourceLine" title="17"></a><span class="co">*/</span>
<a id="cb21-18" class="sourceLine" title="18"></a>h1 &lt;<span class="op">+</span> p {
<a id="cb21-19" class="sourceLine" title="19"></a>  <span class="co">/* This does NOT exist */</span>
<a id="cb21-20" class="sourceLine" title="20"></a>}
<a id="cb21-21" class="sourceLine" title="21"></a>
<a id="cb21-22" class="sourceLine" title="22"></a><span class="co">/*</span>
<a id="cb21-23" class="sourceLine" title="23"></a><span class="co">  There is no preceding sibling combinator.</span>
<a id="cb21-24" class="sourceLine" title="24"></a>
<a id="cb21-25" class="sourceLine" title="25"></a><span class="co">  For example, you cannot select the &lt;p&gt;</span>
<a id="cb21-26" class="sourceLine" title="26"></a><span class="co">  elements that occur before an &lt;h1&gt; element.</span>
<a id="cb21-27" class="sourceLine" title="27"></a><span class="co">*/</span>
<a id="cb21-28" class="sourceLine" title="28"></a>h1 &lt;<span class="op">~</span> p {
<a id="cb21-29" class="sourceLine" title="29"></a>  <span class="co">/* This does NOT exist */</span>
<a id="cb21-30" class="sourceLine" title="30"></a>}
<a id="cb21-31" class="sourceLine" title="31"></a>
<a id="cb21-32" class="sourceLine" title="32"></a><span class="co">/*</span>
<a id="cb21-33" class="sourceLine" title="33"></a><span class="co">  There is no select antedent combinator.</span>
<a id="cb21-34" class="sourceLine" title="34"></a>
<a id="cb21-35" class="sourceLine" title="35"></a><span class="co">  For example, you cannot style an enclosing</span>
<a id="cb21-36" class="sourceLine" title="36"></a><span class="co">  element based on an enclosed element.</span>
<a id="cb21-37" class="sourceLine" title="37"></a><span class="co">*/</span>
<a id="cb21-38" class="sourceLine" title="38"></a><span class="pp">#main</span> &lt;&lt; <span class="fu">.some-deeply-embedded-element</span> {
<a id="cb21-39" class="sourceLine" title="39"></a>  <span class="co">/* This does NOT exist */</span>
<a id="cb21-40" class="sourceLine" title="40"></a>}</code></pre>
</div>
<ol class="incremental" start="8" type="1">
<li>While it may seem unfair to not allow selecting preceding elements or parents by their children or enclosing elements from interior elements, recall that the page is rendered from top to bottom. Rendering preceding elements would require rendering the page from bottom to top or from innermost elements to outermost elements and would make application of CSS rules far more complex regarding whether top-down, bottom-up, or inner-outer rules take precedence. As well, processing from the inner elements to the outer elements is computationally more expensive &#8211; making page renders slower.</li>
<li>You don’t need to use all combinators all the time. In fact, the <strong>descendant combinator</strong> is the most commonly used one. An analysis of 4632 CSS rules<a id="fnref5" class="footnote-ref" href="#fn5"><sup>5</sup></a> from 5 different websites<a id="fnref6" class="footnote-ref" href="#fn6"><sup>6</sup></a> revealed the following combinator distribution:<a id="fnref7" class="footnote-ref" href="#fn7"><sup>7</sup></a>
<ul class="incremental">
<li>78.5% use the <strong>descendant combinator</strong></li>
<li>2.9% use the <strong>child combinator</strong></li>
<li>0.2% use the <strong>next sibling combinator</strong></li>
<li>0% use the <strong>subsequent sibling combinator</strong></li>
<li>18.4% are single item selector rules<a id="fnref8" class="footnote-ref" href="#fn8"><sup>8</sup></a></li>
</ul>
</li>
<li>To see some real world selector examples using combinators, consider the following from the <a href="https://github.com/WordPress/twentynineteen/blob/master/style.css">WordPress 2019 Theme</a>:<a id="fnref9" class="footnote-ref" href="#fn9"><sup>9</sup></a></li>
</ol>
<div id="cb22" class="sourceCode">
<pre class="sourceCode css"><code class="sourceCode css"><a id="cb22-1" class="sourceLine" title="1"></a><span class="co">/*</span>
<a id="cb22-2" class="sourceLine" title="2"></a><span class="co">  Styles an element with the class .comments-title</span>
<a id="cb22-3" class="sourceLine" title="3"></a><span class="co">  that immediately follows a sibling element with</span>
<a id="cb22-4" class="sourceLine" title="4"></a><span class="co">  an ID of #respond that is located at any level</span>
<a id="cb22-5" class="sourceLine" title="5"></a><span class="co">  inside a block with a class of .comment-form-flex.</span>
<a id="cb22-6" class="sourceLine" title="6"></a>
<a id="cb22-7" class="sourceLine" title="7"></a><span class="co">  specificity = 1:2:0</span>
<a id="cb22-8" class="sourceLine" title="8"></a><span class="co">*/</span>
<a id="cb22-9" class="sourceLine" title="9"></a><span class="fu">.comment-form-flex</span> <span class="pp">#respond</span> <span class="op">+</span> <span class="fu">.comments-title</span> {
<a id="cb22-10" class="sourceLine" title="10"></a>  <span class="kw">display</span>: <span class="dv">block</span><span class="op">;</span>
<a id="cb22-11" class="sourceLine" title="11"></a>}
<a id="cb22-12" class="sourceLine" title="12"></a>
<a id="cb22-13" class="sourceLine" title="13"></a><span class="co">/*</span>
<a id="cb22-14" class="sourceLine" title="14"></a><span class="co">  Styles all elements that are direct children of</span>
<a id="cb22-15" class="sourceLine" title="15"></a><span class="co">  a block with the class .comments-area.</span>
<a id="cb22-16" class="sourceLine" title="16"></a>
<a id="cb22-17" class="sourceLine" title="17"></a><span class="co">  You've seen calc() in the lesson</span>
<a id="cb22-18" class="sourceLine" title="18"></a><span class="co">  "CSS – How and When to Use Float".</span>
<a id="cb22-19" class="sourceLine" title="19"></a>
<a id="cb22-20" class="sourceLine" title="20"></a><span class="co">  'rem' will be covered in a future tutorial,</span>
<a id="cb22-21" class="sourceLine" title="21"></a><span class="co">  but it is a measurement unit like px or %.</span>
<a id="cb22-22" class="sourceLine" title="22"></a>
<a id="cb22-23" class="sourceLine" title="23"></a><span class="co">  specificity = 0:1:0</span>
<a id="cb22-24" class="sourceLine" title="24"></a><span class="co">*/</span>
<a id="cb22-25" class="sourceLine" title="25"></a><span class="fu">.comments-area</span> <span class="op">&gt;</span> <span class="op">*</span> {
<a id="cb22-26" class="sourceLine" title="26"></a>  <span class="kw">margin-top</span>: <span class="fu">calc(</span><span class="dv">2</span> <span class="op">*</span> <span class="dv">1</span><span class="dt">rem</span><span class="fu">)</span><span class="op">;</span>
<a id="cb22-27" class="sourceLine" title="27"></a>  <span class="kw">margin-bottom</span>: <span class="fu">calc(</span><span class="dv">2</span> <span class="op">*</span> <span class="dv">1</span><span class="dt">rem</span><span class="fu">)</span><span class="op">;</span>
<a id="cb22-28" class="sourceLine" title="28"></a>}
<a id="cb22-29" class="sourceLine" title="29"></a>
<a id="cb22-30" class="sourceLine" title="30"></a><span class="co">/*</span>
<a id="cb22-31" class="sourceLine" title="31"></a><span class="co">  Styles any &lt;img&gt; nested within the three classes</span>
<a id="cb22-32" class="sourceLine" title="32"></a><span class="co">  laid out below.</span>
<a id="cb22-33" class="sourceLine" title="33"></a>
<a id="cb22-34" class="sourceLine" title="34"></a><span class="co">  specificity = 0:3:1</span>
<a id="cb22-35" class="sourceLine" title="35"></a><span class="co">*/</span>
<a id="cb22-36" class="sourceLine" title="36"></a><span class="fu">.entry</span> <span class="fu">.post-thumbnail</span> <span class="fu">.post-thumbnail-inner</span> img {
<a id="cb22-37" class="sourceLine" title="37"></a>  <span class="kw">position</span>: <span class="dv">relative</span><span class="op">;</span>
<a id="cb22-38" class="sourceLine" title="38"></a>  <span class="kw">display</span>: <span class="dv">block</span><span class="op">;</span>
<a id="cb22-39" class="sourceLine" title="39"></a>  <span class="kw">width</span>: <span class="dv">100</span><span class="dt">%</span><span class="op">;</span>
<a id="cb22-40" class="sourceLine" title="40"></a>}</code></pre>
</div>
<h2 id="references">References</h2>
<ol class="incremental" type="1">
<li><a href="https://drafts.csswg.org/selectors-4/#descendant-combinators">Descendant Combinator</a></li>
<li><a href="https://drafts.csswg.org/selectors-4/#child-combinators">Child Combinator</a></li>
<li><a href="https://drafts.csswg.org/selectors-4/#adjacent-sibling-combinators">Next Sibling Combinator</a></li>
<li><a href="https://drafts.csswg.org/selectors-4/#general-sibling-combinators">Subsequent Sibling Combinator</a></li>
</ol>
<div class="prev">
<p><a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-specificity-advanced-css-selectors-part-2/">Prev</a></p>
</div>
<div class="next">
<p><a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/attribute-selectors-advanced-css-selectors-part-4/">Next</a></p>
</div>
<div class="clear"></div>
<section class="footnotes">
<hr />
<ol>
<li id="fn1">It is sometimes called <strong>closing angle bracket</strong> or <strong>right pointing angle bracket</strong> &#8211; although these are distinct symbols contrast <strong>‘&gt;’</strong> with <strong>‘〉’</strong><a class="footnote-back" href="#fnref1">↩</a></li>
<li id="fn2">The space character only acts as a <strong>descendant combinator</strong> when it is alone. If you write a <strong>child combinator</strong> with spaces (<span class="css-class">.parent &gt; .child</span>), it is a <strong>child combinator</strong>, not a <strong>child + descendant combinator</strong>.<a class="footnote-back" href="#fnref2">↩</a></li>
<li id="fn4">In earlier versions of the CSS specification, it was called <strong>following sibling combinator</strong>, but the internal anchor was (and still is) <strong>general-sibling combinators</strong> (<a href="https://web.archive.org/web/20170103060540/https://drafts.csswg.org/selectors-3/#general-sibling-combinators">look at the end of the URL</a>).<a class="footnote-back" href="#fnref4">↩</a></li>
<li id="fn5">Yes, it is a lot of rules.<a class="footnote-back" href="#fnref5">↩</a></li>
<li id="fn6">I pulled the CSS from the main landing pages of the following:
<ul class="incremental">
<li>BBC,</li>
<li>Engadget,</li>
<li>The Verge,</li>
<li>Wikipedia,</li>
<li>WordPress</li>
</ul>
<p><a class="footnote-back" href="#fnref6">↩</a></li>
<li id="fn7">Take these numbers as a rough guide to their relative use &#8211; if you find yourself using a lot of <strong>subsequent sibling combinators</strong>, then you are, probably, doing something wrong.<a class="footnote-back" href="#fnref7">↩</a></li>
<li id="fn8">This is not entirely accurate. There are other selector types you can use (which will be covered over the next few tutorials) but, for now, it is accurate enough. One such selector is the <span class="css-pseudo-element">::after</span> pseudo-element you saw in <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-understanding-how-float-works-part-1/#clearfix---the-better-way-to-clear-a-float">one of the float tutorials</a>.<a class="footnote-back" href="#fnref8">↩</a></li>
<li id="fn9">There is plenty in the CSS Styling that you haven’t seen &#8211; but they will be covered in future tutorials.<a class="footnote-back" href="#fnref9">↩</a></li>
</ol>
</section>
<p>The post <a href="https://complete-concrete-concise.com/tutorials/webdev/front-end-basics/css-combinators-advanced-css-selectors-part-3/">CSS Combinators &#8211; Advanced CSS Selectors &#8211; Part 3</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
