How to Create a Table with Horizontal separator for Rows only

Discussion in 'HTML & Website Design' started by techbongo, Apr 27, 2011.

  1. #1
    Please see the image shown below. I want to create a HTML tables like this, where table rows (<tr>) are separated by a single line from each other. There are no vertical border for columns. And also 2 consecutive rows are with a different background colors - say all odd rows are light blue colored background and all even rows are white colored background. Please supply the relevant CSS code for this.

    [​IMG]

    Basically I wanted to create some Report/Spreadsheet kind of page where rows of data will be there. So I need it to look like this.
     
    techbongo, Apr 27, 2011 IP
  2. Frost1

    Frost1 Well-Known Member

    Messages:
    519
    Likes Received:
    23
    Best Answers:
    7
    Trophy Points:
    165
    #2
    You can use CSS to make this easy. The CSS is below :

    #tableborder {
    border-bottom-style:solid;
    border-color: #c0c0c0;
    }
    
    Code (markup):
    Also, you should add this ID to your table cells ( td ) . Example shown below :

    <td id="tableborder">
    HTML:
    So, here is another example of how it'll work in a real HTML table :

    <table>
    
    <tr>
    
    <td id="tableborder">example of table</td>
    
    </tr>
    
    <tr>
    
    <td id="tableborder">example of table</td>
    
    </tr>
    
    </table>
    
    HTML:
     
    Frost1, Apr 27, 2011 IP
  3. leunam

    leunam Peon

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    download a 'kompozer' and let him do the the coding.. its like creating a table at excel.
     
    leunam, Apr 28, 2011 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    I don't think CSS 2.1 could create different background color effect in consecutive rows, without the help of javascript (not sure about CSS 3 though).

    Assuming the target table has an id #mytable, the following untested script might give you an idea:
    	var trs = document.getElementById("mytable").getElementsByTagName("tr");
    	for (var i=0; i < trs.length; i++){
    		if (i % 2){
    			trs[i].style.backgroundColor = "gray";
    		}
    		else{
    			trs[i].style.backgroundColor = "white";
    		}
    	}
    HTML:
     
    hdewantara, Apr 29, 2011 IP
  5. xira

    xira Active Member

    Messages:
    315
    Likes Received:
    8
    Best Answers:
    4
    Trophy Points:
    68
    #5
    No CSS2 can't target alternating rows, CSS3 can. But combined with scripting, php/asp/etc. you can have the code write css for alternating rows.
    <?php print '<tr class="bg-color1">' ?>
    <?php print '<tr class="bg-color2">' ?>
     
    xira, Apr 30, 2011 IP