0

I have an app with relatively complex DOM structure with 20+ layers of divs.

If I have a div with class="active" somewhere in the middle of the tree, how can I select it's parent n layers up in the tree using CSS?

For example, how do I select the div marked in uppercase (4 layers up) in the following tree ? :

<DIV>
  <div>
    <div>
      <div>
        <div class="active">
        </div>
      </div>
      <div>
      </div>
    </div>
  </div>
</DIV>

Same considering first-child ? How to select a first-child n layers down the tree ?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
bogatyrjov
  • 5,317
  • 9
  • 37
  • 61

2 Answers2

2

Unfortunately, there is no such thing as a parent selector. You'll have to set a class on the desired element directly, either by hand, or with some server-side code, or via JavaScript.

Community
  • 1
  • 1
Thomas
  • 174,939
  • 50
  • 355
  • 478
-1

Based on this article I found titling CSS4 Preview, it will be possible in CSS4 to style parent elements. The article shows that it would be possible to style parent elements like the following:

$div > div > div > div.active { border: 1px solid #ccc; }

(Given example would style the div nested 3 layers up in the tree related to div.active)

Going back to my app, using PHP and inline CSS, I would be able to control the n (nesting depth).

Until CSS4 though, I will use some kind of jQuery solution.

bogatyrjov
  • 5,317
  • 9
  • 37
  • 61