Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CSS: List Items

Posted on 2012-04-05
7
Medium Priority
?
220 Views
Last Modified: 2012-04-06
Im trying to get a bunch of <li> to have a different color for each.

Here is my code so far:
#ex ul {
      margin-left: 0;
      padding-left: 0;
      display: inline;
}
#ex ul li {
      margin-left: 0;
      margin-bottom: 0;
      padding: 4px;
      border: 1px solid #cccccc;
      list-style: none;
      display: inline;
      font-family:Arial, Helvetica, sans-serif;
      font-size: 12px;
}
<div id="ex">
  <ul>
    <li>SS</li>
    <li>VS</li>
    <li>MS</li>
    <li>DA</li>
  </ul>
</div>

I cannot seem to get the <li> to have a separate background color from the other <li>.

Any help would be appreciated.

Thanks.
0
Comment
Question by:a204801
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37814858
Here's a jquery solution and a css solution:
<head>
    <title>Untitled Page</title>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <style type="text/css">
    #ex ul, #ex1 ul {
      margin-left: 0;
      padding-left: 0;
      display: inline;
    }
    #ex ul li, #ex1 ul li {
      margin-left: 0;
      margin-bottom: 0;
      padding: 4px;
      border: 1px solid #cccccc;
      list-style: none;
      display: inline;
      font-family:Arial, Helvetica, sans-serif;
      font-size: 12px;
    } 
    li.color1
    {
        background-color:#def
    }  
    li.color2
    {
        background-color:#fed
    } 
    li.color3
    {
        background-color:#edf
    } 
    li.color4
    {
        background-color:#fde
    }  
    </style>
    <script type="text/javascript">
    var colors = ['#def', '#fed', '#edf', '#fde'];
    $(document).ready(function(){
        $('#ex ul li').each(function(i){
            $(this).css('background-color', colors[i]);
        });
    });
    </script>
</head>
<body> 
<div id="ex">
  <ul>
    <li>SS</li>
    <li>VS</li>
    <li>MS</li>
    <li>DA</li>
  </ul>
</div>
<br />
<div id="ex1">
  <ul>
    <li class="color1">SS</li>
    <li class="color2">VS</li>
    <li class="color3">MS</li>
    <li class="color4">DA</li>
  </ul>
</div>
</body>

Open in new window

0
 
LVL 16

Accepted Solution

by:
HagayMandel earned 2000 total points
ID: 37815040
What's wrong with basic CSS?

The CSS:
#ex #li1 {	background-color: #66FF00;}
#ex #li2 {	background-color: #CCCCFF;}
#ex #li3 {	background-color: #999933;}
#ex #li4 {	background-color: #0099FF;}
#ex ul {
      margin-left: 0;
      padding-left: 0;
      display: inline;
}
#ex ul li {
      margin-left: 0;
      margin-bottom: 0;
      padding: 4px;
      border: 1px solid #cccccc;
      list-style: none;
      display: inline;
      font-family:Arial, Helvetica, sans-serif;
      font-size: 12px;
}

Open in new window


The HTML:

<div id="ex">
 <ul>
    <li id="li1">SS</li>
    <li id="li2">VS</li>
    <li id="li3">MS</li>
    <li id="li4">DA</li>
</ul>
</div>

Open in new window

0
 
LVL 12

Expert Comment

by:Anuradha Goli
ID: 37815686
apply different styles for each li with class , so that it can be re-used.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 38

Expert Comment

by:Tom Beck
ID: 37815750
Nothing wrong with basic CSS, that's why I gave an example in my first post. I used class rather than id because I agree with @anuradhay about re-use.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37817363
That excepted answer makes no logical sense.
0
 

Author Comment

by:a204801
ID: 37817679
tommyBoy,

Can you elaborate?  It works.  Im curious why it doesnt make sense.

Thanks, Mac
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37818012
Of course it works, it's essentially the same css solution I gave except that I used class instead of id.

Use IDs when there is only one occurence per page (like the menu itself). Use classes when there are one or more occurences per page (like the <li>s). That's the accepted logic when creating a stylesheet. If you used ids to identify every little element on the page then why even have a stylesheet. Just use inline styles instead. The next person that has to troubleshoot the page will thank you for eliminating that unnecessary level of abstraction.

<div id="ex">
 <ul>
    <li style="background-color:#def">SS</li>
    <li style="background-color:#fed">VS</li>
    <li style="background-color:#edf">MS</li>
    <li style="background-color:#fde">DA</li>
</ul>
</div>

Your sample code only shows a simple menu and the associated css. In a real world scenario, there will likely be another element on the page with a background color that matches that of the menu item. In that case, using classes would allow you to simply apply the class="color1" to that second element. By using id you have eliminated that possibility and must now create a new css definition to handle the same background color for the second element. Sound logical?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question