Solved

CSS: List Items

Posted on 2012-04-05
7
182 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now