Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Australia
    Posts
    356
    Thanks
    143
    Thanked 0 Times in 0 Posts

    Alternate background colors for list items

    For example, question has been posed elsewhere on the web:

    http://stackoverflow.com/questions/3...for-list-items

    I know I can manually assign classes or ids, but that's not satisfactory because the list will be dynamic and changing and i don't want to have to go thru and manually change the classes everytime a new item is inserted in which disrupts the alternating pattern. moreover it's a very long list...

    As for JQuery, another suggestion in the forum in the link, I'm still a html css learner, with no JavaScript knowledge, so unless you can give me a crash course in JavaScript better scrap that idea.

    Bottom line, I want or more efficient, or automated solution to alternating background colors for list items.

    If you're wondering it's for the vertical menus on my site: http://www.mathannotated.com/ so that alternating links can be easily discerned from ea. other.

  • #2
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts

    Re

    I would make the links as a list, and then add classes to each <li> tag. TO make them alternate colours.

    Code:
    <ul id="menu">
    <li class="shaded">Link</li>
    <li>Link2</li>
    <li class="shaded">Link3</li>
    and so on and then in the CSS
    .shaded {
    background: #eee;
    }

    Really this is the only way that I know using css and html, you have to manually add the link so it is no big deal to add a class if you need to, if you want something dynamic then you would need to look for a script, but I have not seen a script for this. Found this dont know if it helps http://scripts.top4download.com/alte...tem/ozjfo.html

  • #3
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,737
    Thanks
    22
    Thanked 1,836 Times in 1,820 Posts
    Hello ptrcao,
    A class would be how you would normally do it.

    When you don't have control over the alternating classes on your li, you still want control over your colors via CSS.

    Try this:
    Make the CSS that styles your li's, as if you had a list with one color on the li and one color on the li.alternate.

    Once that CSS is in place it's easy then to have a jQuery that adds the .alternate to every other li.
    Try using this snippet -
    Code:
    $(document).ready(function(){ 
      $('#myList li:nth-child(odd)').addClass('alternate'); 
    });


    And, just for fun, this works in FF3.x -
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
    html, body {
    	margin: 0;
    	background: #FC6;
    }
    #container {
    	width: 800px;
    	margin: 30px auto;
    	background: #999;
    	overflow: auto;
    }
    li { background: green; } 
    li:nth-child(odd) { background: red; } 
    </style>
    </head>
    <body>
        <div id="container">
            <ul id="nav"> 
                <li><a href="link">linky</a></li> 
                <li><a href="link">linky</a></li> 
                <li><a href="link">linky</a></li> 
                <li><a href="link">linky</a></li> 
                <li><a href="link">linky</a></li> 
            </ul> 
        <!--end container--></div>
    </body>
    </html>
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •