4

I need to color ONLY the last li with the class default using only CSS. Editing the HTML is not an option. IE9+ is fine. How do I color only the last link? This is just an example and the menu is dynamic so using last child or specifying the exact last link is not an option. I have a nav menu like this:

<ul>
    <li class="default">
        <a href="#">About Us</a>
        <ul>
            <li class="default">
                <a href="#">Where we live</a>
                <ul>
                    <li class="default">
                        <a href="#">Make this link red only</a>
                    </li>
                    <li>
                        <a href="#">Directions</a>
                    </li>
                </ul>
            </li>
        </ul>
     </li>
 </ul>
ifthiselsethat
  • 206
  • 2
  • 10

2 Answers2

4

As for a pure CSS approach, IE9 supports these pseudo selectors. This could also work in something like jquery as well. However, the problem with this code is if you have more than one default, then it would colour those.

li.default > a:only-child {
  color:red;            
}​
Michael K.
  • 555
  • 2
  • 15
  • I have been working with this and got `li.default>a:only-child { color:red; }` which works when the active element is on the last level but on the second and first levels this does not work – ifthiselsethat Nov 27 '12 at 23:32
  • @faithfulprogrammer then maybe `li.default > a { color:red; }` in addition to the above selector? – Michael K. Nov 27 '12 at 23:40
  • Works if there is definitely only one default on the last level. – Tim Booker Nov 27 '12 at 23:41
  • +1 for getting the closest using pure CSS. I will mark this as the accepted answer because I am gathering that what I truly am looking for cannot be acheived with just CSS. I will be using apathetic's jquery to solve this problem. Thanks for your help guys! – ifthiselsethat Nov 28 '12 at 13:58
  • I would normally say go for the CSS if it works, but when it comes to using jQuery for simple highlighting like this, I'd recommend a server side (e.g. PHP) solution instead as it's much lighter and won't slow anything down (most webhosts support PHP nowadays) – Baumr Nov 28 '12 at 17:52
-1

I think that this PHP solution would be best in your case (here's a PHPfiddle demo I made: hit 'Run' and wait a few seconds).

To implement this, add this to the top of your HTML file:

<?php $thisPage='Make this link red only'; ?>
<html><head>

And then add in this PHP (inside the <?php and ?> tags) to all of your navigation items:

     <ul>
        <li <?php if ($thisPage=='About Us') echo ' id="currentpage"'; ?>>
            <a href="#">About Us</a>
            <ul>
                <li<?php if ($thisPage=='Where we live') 
          echo ' id="currentpage"'; ?>>
                    <a href="#">Where we live</a>
                    <ul>
                        <li<?php if ($thisPage=='Make this link red only') 
          echo ' id="currentpage"'; ?>>
                            <a href="#">Make this link red only</a>
                        </li>
                        <li <?php if ($thisPage=='Directions') 
          echo ' id="currentpage"'; ?>>
                            <a href="#">Directions</a>
                        </li>
                    </ul>
                </li>
            </ul>
         </li>
     </ul>

And then set your CSS to:

#currentpage a { color: red; }

You'll also have to rename your index.html as index.php, or the better solution would be to use .htaccess (assuming you're running an Apache server) to parse PHP inside HTML files.

Community
  • 1
  • 1
Baumr
  • 6,124
  • 14
  • 37
  • 63