tags:

views:

160

answers:

5

i have list like that:

<ul>
 <li><a...>...</a></li>
 <li>...</li>
</ul>

where both type of listelements are there multiple times in arbitrary order. Is there a way to format those li's differently? (different list-style-image) The only difference is that the one kind contains a link and the other one doesnt.

+2  A: 

sure. If you give each different li a class you can do it simple. Or you can always do this if you can't use classes.

ul li
{
    styles....
}
ul li a 
{ 
    styles.... 
}

The styles in the first class will apply to all li elements and styles in the second class will apply to the < a > tags respectively.

Jeremy B.
the content is coming from a cms so i try to avoid changes in the html code to get better usability. I know the second possibility but i only change the a tags, so the li elements will be basically the same. So if i want to change the list style image i would have to give the "a"tag a background image. That would be pretty dirty
Flo
@Flo - If you can't change the **right** thing, then dirty is your only option.
Nick Craver
One question I've always had then. Is how is:ui li { ... }different fromui > li { ... }
Matt
You'll add a style only for child anchor element
zihotki
@Matt: The "x > y" selector targets y elements that are direct descendants of x, while the "x y" selector targets all y elements that are a child of an x element at any level.
Guffa
yes, this is dirty, but if you can't control the markup in any way, and don't want to have to use Javascript you are quite limited. while this is not the best solution, it is one that will get you through.
Jeremy B.
+5  A: 

No, there is no way in CSS to specify a selector depending on the child elements.

You would have to add something to distinguish the li elements themselves, like a class on all li elements that contains links.

If you can use jQUery, you could add the class to the li elements that contains anchor tags:

$('li:has(a)').addClass('linkItem');

A non-jQuery solution could look like this:

var items = document.getElementsByTagName('LI');
for (var i=0; i<items.length; i++) {
  if (items[i].getElementsByTagName('A').length > 0) {
    items[i].className = 'linkItem';
  }
}
Guffa
i also thought of that and i think its one of the nicest solutions (if theres nothing within css), but since sadly not all browsers support jquery i hoped to find a better one
Flo
@Flo What browsers that you're targeting don't support jQuery? Every major desktop browser supports it, and even a few mobile ones.
Dan Herbert
@Flo: I added a non-jQuery solution also.
Guffa
not very wide spreaded but i think jquery stopped ie support for the old versions (5.5 for sure, 6 i dont know, 7+ works).im not sure what i do have to support for this project
Flo
wow thanks this looks really nice as there should be no problems with browsers. I just tried it and somehow it doesnt work. I tried to change the li und a to lowercase and replace the ' by ", but that was not the Problem. Any Idea?
Flo
@Flo: I have tested the code and it works fine. Remember that you have to run the code after the elements are loaded, either in a script tag at the end of the page or in the onload event for the page.
Guffa
A: 

You can't do this with CSS alone, you could use Javascript to accomplish this. Here's an example using jQuery:

$('ul li a').each(function() {
  $(this).parent().css('list-style-image', 'url("/path/image.gif")');
});

This will set the style for the li tags, not the a tags. Technically, the list-style-image property is supposed to be set for ul tags, not li, but most (all?) browsers handle it the way you would expect when you style the li tags individually.

wsanville
A: 

Hello there

I would add a <p></p> tag like this:

<ul>
 <li><a...>...</a></li>
 <li><p></p></li>
</ul>

And then apply 2 different styles like this:

ul a {display:block; padding:3em; background: #ccc;}
ul p {display:block; padding:3em; background: #aaa;}

I would not recommend using javascript for this, some people block javascript ect. but it depends. I would perfer css/html.

Edit: For some reason you can write <p></p> without making it code - Fixed

Also I might have overlooked that you wanted to apply list-style-image, then this will not work.

Allan Kimmer Jensen
A: 

This is what classes are for. In HTML:

<ul>
 <li class="linked"><a...>...</a></li>
 <li>...</li>
</ul>

and in CSS

ul li {...}
ul li.linked {...}
graphicdivine