August 27, 2015

css selector for a node which doesn't contain a given selector with class attribute

Abc123’s Question:

How do I select the entire h4 node that doesn’t contain h3 with class=”avoid”? In this case I want to select the first and fourth h4 node.

<div>
<h4>
    <h1><h1>
    <h2><h2>
</h4>
<h4>
    <h1><h1>
    <h2><h2>
    <h3 class="avoid"><h3>
</h4>
    <h4>
    <h1><h1>
    <h2><h2>
    <h3 class="avoid"><h3>
</h4>
<h4>
    <h1><h1>
    <h2><h2>
</h4>
</div>

First of all. Having heading tags inside another is not valid. Saying that..

CSS is not capable of selecting a parent based on nested elements.

You need JS to do such selections. A good read is this.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!