views:

222

answers:

3

How to give style to list item only if item has another list inside otherwise style should not apply? using css only.

See example here http://jsbin.com/udora

I want to remove arrows from all other items except "Articles" and "Pitching Past the 7th Inning"

I realized it's not possible with css any working jquery solution with demo?

A: 

You can't write a css rule based on the descendants of the desired element.

But you could try applying the style to the inner ul, something like

li ul { foo: bar }

or if you need to not apply to 'Articles' then

li li ul { foo: bar }

Alternatively, in jquery you can say

$('li:has(ul)').css('foo', 'bar');
Rob Van Dam
but li li { foo: baz; } will be applied to second LI not first LI
metal-gear-solid
Sorry, my original answer was way off.
Rob Van Dam
So apply a style to all LI then remove it on the second one?
Brendan Long
!important might come in handy
Brendan Long
ur jquery solution not workin, it's working opposite what i want
metal-gear-solid
+1  A: 

you could use the ul id and do something like this,

#sitemap>li>li {
    type: apply-style;
}

EDIT:

#sitemap>li means the li child of an element with id="sitemap" and similarly, #sitemap>li>li means the li child of the li child of an element with id #sitemap.

read more about css selectors here

EDIT2:

actually since the second li has to be under a second ul,

#sitemap>li>ul>li {
}

just keep in mind that > means direct child.

NOTE: This will not work in IE6.

Sujoy
pls explain more
metal-gear-solid
A simpler version of this would be #sitemap li li (not quite the same however). Keep in mind using child selectors ">" won't work in Internet Explorer 6
Ben Rowe
no it's not working http://jsbin.com/egixu
metal-gear-solid
i added example in question see it
metal-gear-solid
@jitendra, ofc, you got a ul under the first li, so try this way,#sitemap>li>ul>li {}
Sujoy
u r not getting my question "How to give style to list item only if item has another list inside otherwise style should not apply?"
metal-gear-solid
as of now, even in css3 I believe there is no way to get to the parent of an element, so if you want to select parent elements I think javascript is the way to go!
Sujoy
@Sujoy - You should state clearly that your solution will not work in IE6
SamGoody
+1  A: 

What you have available in CSS (CSS-2 precisely) .. is descendant selector .. that is what others have been suggesting you .. and that is also from where(link) you came up with this idea ..

The idea your searching for is called .. Ascendant selector .. (where you go for the element which contains the particular element within) which is unfortunately not available with CSS-2 but it has been proposed in CSS-3 ..
Everywhere, they suggest to use classes and Ids as an alternative (which is more painful stuff .. in design and maintainance point of view ..) ... ultimately proving this is the handicap with CSS-2 ..

infant programmer
this article has something different
metal-gear-solid
ya the article isn't basically on Ascendant selector ..
infant programmer
u wrote "not available with CSS-1 but it has been proposed in CSS-2 .. " yes tell me how we can do with any css2 property
metal-gear-solid
uh...proposed for css2? hasn't css2 been out for a long time already? it's css3 that's still in the works.
Mark
oops .. I was confused .. It was CSS2 and CSS3 I must have been talking about ..
infant programmer