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
Solved

CSS: List Items

Posted on 2012-04-05
7
195 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

856 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