Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS to select top level of a ul

li > ul > li selects all li elements which are deeper than the first level of a ul.

li selects all li elements

li:not(li > ul > li) should select all li elements which are no deeper than the first level of a ul--that is, only first level elements--but it doesn't. Why?

Thanks!

like image 608
Matthew James Davis Avatar asked Mar 29 '13 14:03

Matthew James Davis


People also ask

Which CSS selector has the highest priority?

Id selector has highest priority because of unique nature of the ID attribute definition. We have two classes with one ID selector here it will apply font-size:12px and font-weight:500 due to the specificity rule as ID selector has highest priority after inline CSS.

How do I select a specific list in CSS?

To specify the style for a list, use the list-style property to specify the type of marker. The selector in your CSS rule can either select the list item elements li , or it can select the parent list element ul so that its list elements inherit the style.

What is UL selector?

The first selector (“ ul ”) still selects all <ul> elements, whether they have a class or not. Classes can be added as you find you need them to make the appearance of the page match your intent.


1 Answers

The reason li:not(li > ul > li) does not work is because the li > ul > li is not a simple selector (as Felix Kling noted in the comments to your question).

The easiest way to get the top level is to give a class or id to the outer most ul and then do:

.ulClassNameOrID > li {}

However, the following gets what you desire also (see fiddle) as it does not select any ul that is a direct child of a previous li (so is not a sublist of the outer list):

:not(li) > ul > li {}
like image 178
ScottS Avatar answered Oct 27 '22 12:10

ScottS