Solved

CSS: List Items

Posted on 2012-04-05
7
203 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need advice on an App idea......but who do I ask? 2 72
Set css in function 11 56
HTML 5 Input Type Numeric 5 32
carousel control extend past the images 9 30
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

751 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