<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>frontenddev &amp;mdash; sandeepk</title>
    <link>https://blogs.dgplug.org/sandeepk/tag:frontenddev</link>
    <description></description>
    <pubDate>Thu, 23 Apr 2026 16:15:34 +0000</pubDate>
    <item>
      <title>CSS: Combinators </title>
      <link>https://blogs.dgplug.org/sandeepk/css-combinators</link>
      <description>&lt;![CDATA[In CSS, combinators are used to select content by combining selectors in specific relationships. There are different types of relationships that can be used to combine selectors.&#xA; &#xA;Descendant combinator&#xA;The descendant combinator is represented by a space &#34; &#34;  and typically used between two selectors. It selects the second selector if the first selector is the ancestor (parent, parent parent&#39;s) element. These selectors are called the descendant selectors.&#xA;&#xA;.cover p {&#xA;    color: red;&#xA;}&#xA;div class=&#34;cover&#34;pText in .cover/p/div&#xA;pText not in .cover/p&#xA;In this example, the text &#34;Text in .cover&#34; will be displayed in red.&#xA;&#xA;Child combinators&#xA;The child combinator is represented by &#34;  &#34; and is used between two selectors. In this, an element is only selected if the second selector is the direct child of the first selector element. This means there should not be any other selector between the first selector element and second element selector.&#xA;&#xA;ul   li {&#xA;    border-top: 5px solid red;&#xA;} &#xA;ul&#xA;    liUnordered item/li&#xA;    liUnordered item&#xA;        ol&#xA;            liItem 1/li&#xA;            liItem 2/li&#xA;        /ol&#xA;    /li&#xA;/ul&#xA;In this example, the li element with the text &#34;Unordered item&#34; will have a red top border.&#xA;&#xA;Adjacent sibling combinator&#xA;The adjacent sibling combinator is represented by &#34;+&#34;  is placed between the two CSS selector. In this element is selected if the selector element is directly followed by the first element selector or only the adjacent sibling&#xA;h1 + span {&#xA;    font-weight: bold;&#xA;    background-color: #333;&#xA;    color: #fff;&#xA;    padding: .5em;&#xA;}&#xA;div&#xA;    h1A heading/h1&#xA;    spanVeggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo&#xA;            melon azuki bean garlic./span&#xA;&#xA;    spanGumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard&#xA;            greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini./span&#xA;/div&#xA;In this example, the first span element will have the given CSS properties.&#xA;&#xA;General sibling combinator&#xA;The general sibling combinator is represented by &#34;~&#34;.  It selects all the sibling element, not only the direct sibling element, then we use the general sibling combinator.&#xA;h1 ~ h2 {&#xA;    font-weight: bold;&#xA;    background-color: #333;&#xA;    color: #fff;&#xA;    padding: .5em;&#xA;}&#xA;article&#xA;    h1A heading/h1&#xA;    h2I am a paragraph./h2&#xA;    divI am a div/div&#xA;    h2I am another paragraph./h2&#xA;/article&#xA;In this example, every h2 element will have the given CSS properties.&#xA;&#xA;CSS combinators provide powerful ways to select and style content based on their relationships in the HTML structure. By understanding combinators, we can create clean, maintainable, and responsive web designs.&#xA;&#xA;Cheers!&#xA;&#xA;References&#xA;MDN Web Docs&#xA;&#xA;CSS&#xA;Combinators&#xA;WebDevelopment&#xA;FrontendDev &#xA;&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>In CSS, combinators are used to select content by combining selectors in specific relationships. There are different types of relationships that can be used to combine selectors.</p>

<h2 id="descendant-combinator">Descendant combinator</h2>

<p>The descendant combinator is represented by a space “ ”  and typically used between two selectors. It selects the second selector if the first selector is the ancestor (parent, parent parent&#39;s) element. These selectors are called the descendant selectors.</p>

<pre><code class="language-css">.cover p {
    color: red;
}
</code></pre>

<pre><code class="language-html">&lt;div class=&#34;cover&#34;&gt;&lt;p&gt;Text in .cover&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Text not in .cover&lt;/p&gt;
</code></pre>

<p><em>In this example, the text “Text in .cover” will be displayed in red.</em></p>

<h2 id="child-combinators">Child combinators</h2>

<p>The child combinator is represented by “&gt;” and is used between two selectors. In this, an element is only selected if the second selector is the direct child of the first selector element. This means there should not be any other selector between the first selector element and second element selector.</p>

<pre><code class="language-css">ul &gt; li {
    border-top: 5px solid red;
} 
</code></pre>

<pre><code class="language-html">&lt;ul&gt;
    &lt;li&gt;Unordered item&lt;/li&gt;
    &lt;li&gt;Unordered item
        &lt;ol&gt;
            &lt;li&gt;Item 1&lt;/li&gt;
            &lt;li&gt;Item 2&lt;/li&gt;
        &lt;/ol&gt;
    &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p><em>In this example, the <code>&lt;li&gt;</code> element with the text “Unordered item” will have a red top border.</em></p>

<h2 id="adjacent-sibling-combinator">Adjacent sibling combinator</h2>

<p>The adjacent sibling combinator is represented by “+”  is placed between the two CSS selector. In this element is selected if the selector element is directly followed by the first element selector or only the adjacent sibling</p>

<pre><code class="language-css">h1 + span {
    font-weight: bold;
    background-color: #333;
    color: #fff;
    padding: .5em;
}
</code></pre>

<pre><code class="language-html">&lt;div&gt;
    &lt;h1&gt;A heading&lt;/h1&gt;
    &lt;span&gt;Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo
            melon azuki bean garlic.&lt;/span&gt;

    &lt;span&gt;Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard
            greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<p><em>In this example, the first <span> element will have the given CSS properties.</em></p>

<h2 id="general-sibling-combinator">General sibling combinator</h2>

<p>The general sibling combinator is represented by “~“.  It selects all the sibling element, not only the direct sibling element, then we use the general sibling combinator.</p>

<pre><code class="language-css">h1 ~ h2 {
    font-weight: bold;
    background-color: #333;
    color: #fff;
    padding: .5em;
}
</code></pre>

<pre><code class="language-html">&lt;article&gt;
    &lt;h1&gt;A heading&lt;/h1&gt;
    &lt;h2&gt;I am a paragraph.&lt;/h2&gt;
    &lt;div&gt;I am a div&lt;/div&gt;
    &lt;h2&gt;I am another paragraph.&lt;/h2&gt;
&lt;/article&gt;
</code></pre>

<p><em>In this example, every <code>&lt;h2&gt;</code> element will have the given CSS properties.</em></p>

<p>CSS combinators provide powerful ways to select and style content based on their relationships in the HTML structure. By understanding combinators, we can create clean, maintainable, and responsive web designs.</p>

<p>Cheers!</p>

<p><strong>References</strong>
– <a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators" rel="nofollow">MDN Web Docs</a></p>

<p><a href="/sandeepk/tag:CSS" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">CSS</span></a>
<a href="/sandeepk/tag:Combinators" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Combinators</span></a>
<a href="/sandeepk/tag:WebDevelopment" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">WebDevelopment</span></a>
<a href="/sandeepk/tag:FrontendDev" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">FrontendDev</span></a></p>
]]></content:encoded>
      <guid>https://blogs.dgplug.org/sandeepk/css-combinators</guid>
      <pubDate>Sun, 28 May 2023 14:42:23 +0000</pubDate>
    </item>
  </channel>
</rss>