0

Given the following html:

<ul class="nav">
  <li class="level0">..</li>
  <li class="level0">..</li>
  <li class="level0 active">
    <a href="#">Category Name</a>
    <ul class="level0">
      <li class="level1 active">
        <a href="#">Sub-category</a>
        <ul class="level1">
          <li class="level2 active">
            <a href="#">I only want this link styled</a>
          </li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="level0">..</li>
</ul>

How do I only style that nested link, considering that each parent li also has a class of 'active'? I thought I could use .nav .active:last-child > a which works in the above example, but removing the active class from that li.level2 you would expect then that the li.level1 above it would be styled, but it is not (see jsfiddle below for an example of this).

I may just be having a brainfart but I can't think of a way to target that element with only css. The only thing I can think of is to use javascript to remove the 'active' class from the parent elements, but I feel like there must be some other way.

Here is a more elaborate jsfiddle example that illustrates two test cases: http://jsfiddle.net/K4e37/

Is this possible without changing the markup and without using javascript?

Edit: I wasn't thinking about last-child correctly but here is an updated example which gets pretty close to what I want, just need to not style the higher level elements: http://jsfiddle.net/K4e37/2/

Westwick
  • 2,367
  • 3
  • 28
  • 51

2 Answers2

0

Based on other answers (here, here), the answer to your question is no. As summarized there, "last-of-type" does not work with classes and "last-child" does not work with the sub-nesting structure in your HTML. I think you'll have to change the markup or use Javascript.

Community
  • 1
  • 1
Alex P. Miller
  • 2,128
  • 1
  • 23
  • 20
  • I think you're probably right. I was able to use this to correctly add a new class to the elements I was targetting: $('li.active:not(:has(li.active)) > a').addClass('deepest'); – Westwick Jul 01 '14 at 19:09
0
  1. List item

If you don't even have the 'level' classes, still you can target your specific html link with the below selector( if you only needs that specific link to be styled ). Please link if your requirement fulfills!

Usiong CSS:

ul.nav li ul li ul li.active a { color: #FF0000; }

Cheers :)