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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Displaying links using CSS

    hello all

    I have a very basic CSS question.

    I have to a list of database records on my webpage. These are being displayed in a table. Alternate rows of the table have different backgrounds. Now one of the columns of the table has to contain a link. How can I make the background color of link also alternate with every row.
    Code:
    tr.even
    {
      background-color: #f2f2d5;
    }
    tr.odd
    {
      background-color: #d0d0a9;
    }
    Simply speaking I can create two separate classes of links same as I did for rows. But I am looking for a better way in CSS. Can anybody suggest something?

    Thanks and Regards
    Vikram

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You can make use of CSS 3 nth-child() pseudo-class to target odd/even elements:
    Code:
    tr:nth-child(even) {
    background-color: #f2f2d5;
    }
    
    tr:nth-child(odd) {
    background-color: #d0d0a9;
    }
    Apparently, it's support is (yet) limited to Chrome, Konqueror, Opera 9.5 (as per time being)

    For further reading:
    http://www.w3.org/TR/css3-selectors/#nth-child-pseudo
    http://kimblim.dk/csstest/#ex7c
    http://reference.sitepoint.com/css/pseudoclass-nthchild

    You can however make us of JS to ease the job for you, but that is if you are opting for it.

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks Rangana.
    No I don't want to use JavaScript. Rather can't use it now, having become a fan of CSS.

    And the thing you suggested, if it doesn't work on firefox, IE, then may be I should not be going into it.

    I found a better way. I had the following structure...

    html content
    ------------------------------------
    Code:
    <tr class='even'>
      <td><a href='#'>Link1</a></td>
    </tr>
    <tr class='odd'>
      <td><a href='#'>Link1</a></td>
    </tr>
    CSS content
    -------------------------------------
    Code:
    tr.even {
      background-color: white;
    }
    tr.odd {
      background-color: green;
    }
    a:hover, a:active {
      background-color: orange;
      color: white;
    }
    a:visited, a:link {
      text-decoration: none;
      color: inherit;
    }
    This way, I am able to display the link in whatever color the row is in. Although see those underlined parts for "color" property. They don't work. On hovering over the link the text-color does not change to white.

    Can anybody explain why?

    Thanks and Regards
    Vikram

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Code:
    td a:hover, td a:active {
      background-color: orange;
      color: white;
    }
    td a:visited, td a:link {
      text-decoration: none;
      color: inherit;
    }
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    vikram8jp (02-18-2009)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    On hovering over the link the text-color does not change to white.

    Can anybody explain why?
    There may be some other more specific selectors overriding the above selector. See www.htmldog.com/guides/cssadvanced/specificity

    You may easily track them by firebug. Could you post a link to your page(a sample)?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    vikram8jp (02-18-2009)

  • #6
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Thumbs up

    @ Rangana

    Thanks for the trick Rangana. Although I did it like this....
    Code:
    td a:hover, td a:active {
      background-color: orange;
      color: white;
    }
    td a:visited, td a:link {
      text-decoration: none;
      color: inherit;
    }
    Removed those shaded areas. That worked.

    @ Abduraooft

    The material you suggested taught me the concept of specificity (Hard to pronounce, but equally effective ) Thanks a lot.


  •  

    Posting Permissions

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