Solved

CSS: List Items

Posted on 2012-04-05
7
192 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
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 500 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

810 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