0

I guess I am not getting css child combinators.

I am trying to target just the first level on the li's with the following:

ul > li { color: green; }
                <ul>
                    <li>Home</li>
                    <li>
                        Products
                        <ul>
                            <li>Product 1 </li>
                            <li>Product 2</li>
                            <li>Product 3</li>
                        </ul>
                    </li>
                    <li>Contact</li>
                    <li>News</li>
                </ul>

http://jsfiddle.net/5vB3h/

NOTE: I also tried removing the spaces between >, with no luck.

1
  • I have solved. see answer with most downvotes for working fiddle ;) Commented May 30, 2013 at 20:44

6 Answers 6

2

You're using them fine, but all (properly marked-up) <li>s are children of <ul>s. You can specify the parent (in your jsFiddle, body):

body > ul > li

Or reverse the styles with the more specific case:

li ul > li {
    color: black;
}

In the case of color, you need to use the second option anyways, because color is inherited. Here's the updated jsFiddle.

Sign up to request clarification or add additional context in comments.

3 Comments

Please provide a jsFiddle link
I am trying to over-ride, adding more css. I can just use simple css - ie. li {} li li {}
@gutierrezalex: Go ahead; see my other comment.
1

Your rule targets the child list items of any list. What you can do is create a second rule to recolor the other sub list items. For example:

ul > li {
    color: green;
}
li li {
    color:black
}

jsFiddle example

7 Comments

This is exactly what I am trying to avoid.
Why are you trying to avoid that?
@gutierrezalex: That's the only way to do it, though.
What is the point of using CSS Child Combinators, then? I can just use ul li, then over ride with li li. Makes sense?
There are plenty of reason to use various CSS combinators, however in your specific case, this is one simply way to solve your issue.
|
0

ul will match all the <ul> elements. Since every <li> is a child of one of the <ul>s…

You need to be more specific about which <ul> you mean. Perhaps add a class to it.

1 Comment

Or use a child selector on the parent of the <ul> too, such as div > ul > li { color: green; }.
0

ul > li will select all the li elements in your document because they are all the children of ul elements.

If you apply a class to the parent like <ul class="top">, then you can use ul.top > li.

1 Comment

and yours work does it recursive... see a fiddle of your NOT working solution jsfiddle.net/5vB3h/8
0

Add a class

li {color: blue;}
/* ^ added because maybe initial property is color: inherit;
     If not, someone correct me */
ul.a > li { color: red; }

After this, add class to ul like <ul class="a" ...

http://jsfiddle.net/5vB3h/7/

1 Comment

I am trying to NOT over ride with css. For that, I can just jsfiddle.net/5vB3h/6
-2

EDIT (worked it out):

Okay so I ballsed up. Below is wrong.

ul:first-child > li { color: green; }

I found that when applying:

div>ul>li{color:green}

all lis went green... turns out that the li magically inherit the color of the li (odd behaviour as I assume the content had color:#000)

anyway... You need to explicitly set the color: to soemthing other than green to see the style working.

fiddle here

//html

<div>
    <ul>
  <li>Home</li>
  <li>
    Products
    <ul>
        <li>Product 1</li>
        <li>Product 2</li>
        <li>Product 3</li>
    </ul>
  </li>
  <li>Contact</li>
  <li>News</li>
  </ul>
</div>

//css

li {color:black} //you have to have this (or something like * {color:black} OR body/html {color:black} as li seem to automatically inherit parent li color property
div>ul>li{ color: green; } //have to have parent.

13 Comments

Do you want me to do a 'dummy' guide?
@Paul: That won't be necessary, but I think people would be happy with a correct answer.
@recursive there you go - correct answer, odd behaviour (perhaps within jsfiddle maybe not to do with inherited color attribute that needs to be explicitly declared)
thanks for creating the fiddle minitech - see updated fiddle ;) jsfiddle.net/5vB3h/5
I am trying to NOT over ride, adding more CSS. For that, I can just use li {} li li {}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.