Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target the deepest nested ul/ol element with css

Tags:

html

css

In a nested list like the one below, how would one target the ul containing joe without knowing the actual depth in advance?

<ul>
    <li>foo
        <ul>
            <li>bar
                <ul>
                    <li>baz
                        <ul>
                            <li>joe</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

http://jsbin.com/fiqowuhate/1/edit?html,css,output

like image 671
estrar Avatar asked Jan 30 '15 10:01

estrar


People also ask

How do I target a OL Li in CSS?

A space between selectors will get all matching elements below it, no matter how far down in the tree, so your ol li selector will select all li elements, at all three levels. Use the > selector to get direct children. You also need to anchor them to something at the top level, say a div .

What is Target element CSS?

The target selector is used to represent a unique element (the target element) with an id matching the URL's fragment. It can be used to style the current active target element. URLs with a # followed by an anchor name link to a certain element within a document. The element being linked to is the target element.

How do you target the last element?

The :last-child selector allows you to target the last element directly inside its containing element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.


1 Answers

As far as I am personally aware, this is not possible as there is currently no parent selector in CSS, but you could use the jQuery selector $('ul:not(:has(ul))'); to target ul elements without any ul children, and add a class to them.

Example

like image 169
Andy Avatar answered Nov 15 '22 04:11

Andy