title: Nested CSS Classes – Child class to override parent class?
body: <p>The goal is to have an element's parent dictate it’s style. So <code>.dark .light p</code> would have light text, <code>.dark p</code> would have dark text, and <code>.light .dark .light p</code> would have light text.</p>
  
  <p>The <code>p</code> element might not even be a direct child of <code>.dark</code> or <code>.light</code> so <code>.light .dark div.wrap div.inner p</code> would have dark text.</p>
  
  <p>Because of the cascading nature of CSS, it seems to prefer the last rule. Is this even possible?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>/* Doesn't work - switching the order isn't helpful
     as the opposite problem occurs */
  
  .dark p {
    color: #000;
  }
  .light p {
    color: #aaa;
  }
  /* Works, but in my use case I need to specify attributes on 
     specific element. */
  
  /* 
  .dark {
    color: #000;
  }
  
  .light {
    color: #aaa;
  } 
  */
  
  /*  Works but not realistic if paragraph is nested deeper */
  
  /*
  .dark &gt; p {
    color: #000;
  }
  
  .light &gt; p {
    color: #aaa;
  }
  */
  
  /* Only works on the first two levels */
  
  /* 
  .dark p {
    color: #000;
  }
  
  .light p {
    color: #aaa;
  }
  
  .dark .light p {
    color: #aaa;
  } 
  
  .light .dark p {
    color: #000;
  } 
  */</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="light"&gt;
    &lt;p&gt;Light text&lt;/p&gt;
    &lt;div class="dark"&gt;
      &lt;p&gt;Dark text&lt;/p&gt;
      &lt;div class="light"&gt;
        &lt;p&gt;Light text&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="dark"&gt;
    &lt;p&gt;Dark text&lt;/p&gt;
    &lt;div class="light"&gt;
      &lt;p&gt;Light text&lt;/p&gt;
      &lt;div class="dark"&gt;
        &lt;p&gt;Dark text&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="http://codepen.io/acondiff/pen/KaaqxP" rel="nofollow noreferrer">http://codepen.io/acondiff/pen/KaaqxP</a></p>