Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CSS Alternate row colors

Posted on 2016-09-12
8
Medium Priority
?
291 Views
Last Modified: 2016-09-13
Hello expert,

Have read several sites that say the row in a table can have
alternate row color contrast using

      table tr:nth-child(odd){
            background: #e6e6e6;
      }
      table tr:nth-child(even){
            background: #ffffff;
      }

But have tried this (complete page code
copied herewith below) with no luck.

Have tried using
<tr class="odd">
but then each <tr> must
specified individually.

Is there a more efficient way of
making the tables easier to read?

Thanks.

Allen Pitts, Dallas Texas


++++++++++++++begin html page code+++++++++++++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Table alternating row colors</TITLE>
<style type="text/css">
      .TFtable{
            width:100%;
            border-collapse:collapse;
      }
      .TFtable td{
            padding:7px; border:#4e95f4 1px solid;
      }

      /*  Define the background color for all the ODD background rows  */
      tr.odd{
            background-color: #e6e6e6;
      }
      /*  Define the background color for all the EVEN background rows  */
      tr.even{
            background-color: #ffffff;
      }
</style>
</HEAD>

<BODY >


<table class="TFtable">
      <tr class="odd"><td>Text1</td> <td>Text2</td> <td>Text3</td></tr>
      <tr class="even"><td>Text4</td> <td>Text5</td><td>Text6</td></tr>
      <tr class="odd"><td>Text</td> <td>Text</td><td>Text</td></tr>
      <tr class="even"><td>Text</td> <td>Text</td><td>Text</td></tr>
</table>
</BODY>
</HTML>

++++++++++++++end html page code+++++++++++++++++++++++
0
Comment
Question by:Allen Pitts
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41795067
This works for me
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Table alternating row colors</TITLE>
<style type="text/css">
      .TFtable{
            width:100%;
            border-collapse:collapse;
      }
      .TFtable td{
            padding:7px; border:#4e95f4 1px solid;
      }
      tr {
            background-color: #e6e6e6;
      }
	  tr:nth-child(even) {
          background-color: #ffffff;
	  }
</style>
</HEAD>

<BODY >


<table class="TFtable">
      <tr><td>Text1</td> <td>Text2</td> <td>Text3</td></tr>
      <tr><td>Text4</td> <td>Text5</td><td>Text6</td></tr>
      <tr><td>Text</td> <td>Text</td><td>Text</td></tr>
      <tr><td>Text</td> <td>Text</td><td>Text</td></tr>
</table>
</BODY>
</HTML>

Open in new window


Working sample here

For interest

tr:nth-child(even) same as tr:nth-child(2n)
tr:nth-child(odd) same as tr:nth-child(2n+1)

In the sample above we define the default style for tr then override with the tr:nth-child(even)
0
 
LVL 10

Expert Comment

by:ecarbone
ID: 41795208
try this:
1. You don't have to assign classes to your table rows.

2. Remove 'table' from your css declarations. In other words, use this:

tr:nth-child(odd){
    background: #e6e6e6;
 }

 tr:nth-child(even){
     background: #ffffff;
 }
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 41795245
Is it possible that it wasn't working because you had background colors specified for your TDs? (This could override any background specified for the TRs, since the TDs coule be filling up all of the space within the TRs.)
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 60

Expert Comment

by:Julian Hansen
ID: 41795505
Is it possible that it wasn't working because you had background colors specified for your TDs?
There are no background colours specified for <td> in the source - unless I am missing something.

Sidebar:
It is not necessary to define both an nth-child(odd) and nth-child(even). You can specify a default for <tr> and then either override the odd or even rows as required.
0
 

Author Comment

by:Allen Pitts
ID: 41796375
Hello Experts,

Have tried Mr. Hansen's excellent solution: has no effect:
all cells grey.

What is strange is the working sample
works as desired but the code copied into
the htm page on the local drive does not.

The result is all rows grey. In other words
the tr tag works but the   tr:nth-child(even)
does not.

Tried just   tr:nth-child(even)
Result: All cells white

Stranger still did a View Source on the working sample
and copied into local page. Result: Same, All rows grey

Tried (ecarbone) removing  .TFtable  and .TFtable td
elements. Result: same(all cells grey)  with no border or width.

It seems to be a browser compatibility issue.
Research says  tr:nth-child(even) does not
work IE 8 and younger. Using IE 11.

Unable to test on FireFox and Chrome
due to corporate restrictions.

Code tested copied herewith below.

Could someone with IE 11 try this so
it can be known if it is a browser compatibility
issue?

Thanks

Allen In Dallas

++++++++++++++begin html+++++++++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Table alternating row colors</TITLE>
<style type="text/css">
      .TFtable{
            width:100%;
            border-collapse:collapse;
      }
      .TFtable td{
            padding:7px; border:#4e95f4 1px solid;
      }
      tr {
            background-color: #e6e6e6;
      }
        tr:nth-child(even) {
          background-color: #ffffff;
        }
</style>
</HEAD>

<BODY >


<table class="TFtable">
      <tr><td>Text1</td> <td>Text2</td> <td>Text3</td></tr>
      <tr><td>Text4</td> <td>Text5</td><td>Text6</td></tr>
      <tr><td>Text</td> <td>Text</td><td>Text</td></tr>
      <tr><td>Text</td> <td>Text</td><td>Text</td></tr>
</table>
</BODY>
</HTML>

++++++++++++++end html+++++++++++++++++++
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 41796401
It could be the doctype declaration. Try:

<!DOCTYPE html>

Open in new window

0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41796482
Not DOCTYPE
Code is definitely good - see screen shot of page on IE11 Emulating IE9
Are you sure you are not emulating a different version of IE.
I can guarantee the code is good for modern browsers - if it is not working on your side then it is the browser. IE8 will not work, anything above this is fine.
F12 to get to your console - click the emulation tab and ensure you are not on IE8 (or lower) emulation
Screenshot
1
 

Author Closing Comment

by:Allen Pitts
ID: 41796612
Hello Mr. Hansen,

This is wierd. When opened using IE 11 from shared drive on network the tr:nth-child (even) does not work.

When opened from C: , local hard drive, the alternating rows appear. I believe it had I guessed: something to do with the highly regulated LAN.
For instance I am having to
Closing this question using my iphone because when I try to
Close on computer I get 'This operation has been canceled due to. restrictions on effect on this computer. please contact your system administrator.'
Big brother is watching.

Thanks
Allen
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

916 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