Note that there are some explanatory texts on larger screens.

plurals
  1. POCSS class and descendant selectors practices
    primarykey
    data
    text
    <p>I would like to form a better understanding of when it is appropriate to use classes to target content as opposed to other available options, specifically, descendant selectors.</p> <p>In many cases, instead of using classes, it is possible to use descendant selectors to target the same elements. If the same thing can be accomplished in both ways, what is the logic between deciding an option ?</p> <p>Here are some example scenarios:</p> <p>1.</p> <p>a.</p> <pre><code>&lt;div class="main"&gt; &lt;div&gt; &lt;div&gt; /* target */ &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; .main &gt; div &gt; div </code></pre> <p>b.</p> <pre><code>&lt;div class="main"&gt; &lt;div&gt; &lt;div class="content"&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; .content </code></pre> <p>2.</p> <p>a.</p> <pre><code>&lt;div class="main"&gt; &lt;div&gt; /* target */ &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div&gt; /* target */ &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; .main &gt; div:first-child .main &gt; div:last-child </code></pre> <p>b.</p> <pre><code>&lt;div class="main"&gt; &lt;div class="content1"&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="content2"&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; .content1 .content2 </code></pre> <p>3.</p> <p>a.</p> <pre><code>&lt;div class="main"&gt; &lt;div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; .main &gt; div div </code></pre> <p>b.</p> <pre><code>&lt;div class="main"&gt; &lt;div class="content"&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; .content div </code></pre> <p>What is the logic between deciding to use classes or descendant selectors ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload