18
<ul> <li> <span> apply </span> </li> <li> <span> apply </span> </li> <li> <ul> <li> <span> ignore </span> </li> <li> <span> ignore </span> </li> </ul> </li> </ul> 

How can I apply a CSS rule only to span elements from the first level, and make span elements from the nested list ignore the rule?

Can this be done without specifically resetting the properties on the 2nd level spans?

Tried ul > li span but it doesn't seem to work, I get the styles applied to 2nd level too

4
  • 1
    What element is the parent of the outer ul? Commented Apr 23, 2012 at 14:35
  • it's a div? why does that matter? Commented Apr 23, 2012 at 14:37
  • 1
    See my answer. You can use that as the starting point for your selector. Commented Apr 23, 2012 at 14:38
  • @Alex It matters because any rule to effect only the first-level children of ul will effect first-level children of every ul element, therefore we would have to specify a parent element of the ul to style, so it is selectable. Commented Apr 23, 2012 at 14:41

4 Answers 4

24

Put your list in a wrapping div with an ID, for example <div id="ul-wrapper">, and try:

#ul-wrapper > ul > li > span { /* my specific CSS */ } 
Sign up to request clarification or add additional context in comments.

Comments

2
 #container > ul > li span { /* - Your CSS Here - */ } 

Need to specify a container, so that only the first level ul may be selected.

Comments

2

As long as the parent of the outer ul isn't another li, you can use that as the starting point for your selector (example assuming it's a div):

div > ul > li span { /* Whatever */ } 

Comments

0
ul > li > span {border:solid 1px red;} li > ul > li > span {border:none 1px red;}; 

1 Comment

From the question: "Can this be done without specifically resetting the properties on the 2nd level spans?"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.