views:

62

answers:

5

Hi all , please help me to style this list , I need to set different background image for each list item, but class are same.

<ul>
<li class="sameforall">menu 1</li>
<li class="sameforall">menu 2</li>
<li class="sameforall">menu 3</li>
<li class="sameforall">menu 4</li>
</ul>

I know this one , but it works only for fist item :(

ul:first-child li{
/*my css*/
} 
+1  A: 

I would avoid the use of first-child since it's not fully supported and where it is, it's probably still buggy. In regards to referring to the other elements or childs, your best shot would be to give them a different id and style them using it. Like this:

 <ul class="sameforall">
   <li id='first' >menu 1</li>
   <li id='second'>menu 2</li>
   <li id='third' >menu 3</li>
   <li id='forth' >menu 4</li> 
 </ul>

Then you would refer to those elements in the css file like this:

#first{/*Your css*/}

If you want to see a list of support browsers for the nth-child visit this page it contains a table with some of the most popular browser versions and the support issues they may have with it.

thisMayhem
There is no point putting the same class on every single <li> when you can just put it on the <ul>
danixd
True, saw it when reading your reply. Updated, thanks.
thisMayhem
A: 

you need :nth-child() btw it should be

ul li:fist-child
antpaw
+5  A: 

Why would you give all the li's the same class?

Give the ul a class to style the contained li's, then give the li's their own class, like so:

<ul class="sameforall">
   <li class="one">menu 1</li>
   <li class="two">menu 2</li>
   <li class="three">menu 3</li>
   <li class="four">menu 4</li>
</ul>

.sameforall {color: red;}
   .sameforall .one {background-color: blue;}
   .sameforall .two {background-color: green;}
   .sameforall .three {background-color: pink;}
   .sameforall .four {background-color: purple;}

You can't access the HTML, CSS3 supports :nth-child() psuedo selecting - http://css-tricks.com/how-nth-child-works/

<ul>
   <li class="sameforall">menu 1</li>
   <li class="sameforall">menu 2</li>
   <li class="sameforall">menu 3</li>
   <li class="sameforall">menu 4</li>
</ul>

.sameforall:nth-child(1) { background-color: blue; }
.sameforall:nth-child(2) { background-color: green; }
.sameforall:nth-child(3) { background-color: pink; }
.sameforall:nth-child(4) { background-color: purple; }

Note, this won't work in most old browsers.

danixd
Thanks but I can't change this html , I have got access only to style it via CSS
Tom
Better edit the question or you might not get an answer. I'd imagine this is impossible to work properly as nth-child() isn't supported in most of the old browsers, that includes :first-child, :last-child etc.
danixd
np , if there is any way to style it only on new browsers , it would be great too.
Tom
in that case :nth-child will work:http://css-tricks.com/how-nth-child-works/
danixd
class names have to start with a character, so <li class="1"> etc. is invalid. But anyway, since you cant change the HTML and child selecting via CSS is not supported that well, the only way for you is to do it with JavaScript.
Alex
ah , tanks a lot danixd
Tom
no , I can't add JS either
Tom
You are right, silly mistake there, thanks Alex
danixd
A: 

Since you can't change the markup and child selecting via CSS is not supported that well, the only way for you is to do it with JavaScript.

<ul>
    <li class="sameforall">menu 1</li>
    <li class="sameforall">menu 2</li>
    <li class="sameforall">menu 3</li>
    <li class="sameforall">menu 4</li>
</ul>
<script type="text/javascript">
    var listItems   = document.getElementsByTagName("ul")[0].getElementsByTagName("li");
    var numChildren = listItems.length; 
    for(var i = 0; i < numChildren; i++) {
        var item = listItems[i]; 

        // -> do whatever you want with each item.
        switch(i) {
            case 0: item.style.backgroundImage = 'url(item-1.gif);'; break;
            case 1: item.style.backgroundImage = 'url(item-2.gif);'; break;
            case 2: item.style.backgroundImage = 'url(item-3.gif);'; break;
        }            
    }
</script>
Alex
Thanks for js , unfortunately I can't add JS either
Tom
A: 

you need to go with the nth-child method.

Here the stuff is well detailed. Hope this will help you.

http://www.w3.org/TR/css3-selectors/

Wind Chimez