Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only top level element in CSS when I don't know the parent

Trying to select only the top level UL element and exclude any nested UL elements in CSS. I've tried so many different variations but can't find a solution.

As an example, the html code could look like something below. Now I want to go ahead and style only the top level UL and don't apply the styling to the nested ULs. In my scenario, I can't add any classes or IDs to the HTML code - no changes to the HTML; CSS only.

<ul>
    <li>
        <ul>
            <li>
                <ul>
                    <li></li>
                    <li></li>
                </ul>
            </li>
            <li>
                <ul>
                    <li></li>
                    <li></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
like image 437
ITinfrastructure Avatar asked Nov 28 '25 00:11

ITinfrastructure


2 Answers

I settled for *:not(li) > ul to only select top level <ul> elements and exclude <ul> elements that are inside of other <ul> elements.

like image 130
ITinfrastructure Avatar answered Nov 30 '25 16:11

ITinfrastructure


Here's the most direct way of doing it:

ul:not(ul ul)

like image 33
Starscream512 Avatar answered Nov 30 '25 14:11

Starscream512



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!