Wednesday, June 13, 2007
HTML Tables and Styles

Have you ever tried to add borders to individual cells in an HTML table using style sheets? Something similar to <table border="1"> only done with CSS.

I had a requirement to put a 1 pixel black border between all cells in an HTML table. I wanted to do this without having to put a style or class attribute into every cell in the table. I thought: "How hard can this be?". There has to be a way of doing this with a single style attribute.

But after a quick search on the web with too many non-relevant results, (I mean what can you search for that doesn't bring back over a million hits). I decided to come up with my own technique:

<style type="text/css">

  table.border tr td,
  table.border tr th
{border:1px solid black;}

  table.border {border-collapse:collapse;}

</style>

 

<table class="border">

  <tr>

    <th>Header 1</th> 

    <th>Header 2</th> 

    <th>Header 3</th> 

    <th>Header 4</th> 

  </tr>

  <tr>

    <td>Data 1</td> 

    <td>Data 2</td> 

    <td>Data 3</td> 

    <td>Data 4</td> 

  </tr>

  <tr>

    <td>Data 1</td> 

    <td>Data 2</td> 

    <td>Data 3</td> 

    <td>Data 4</td> 

  </tr>

</table>

Yields the following output:

Header 1 Header 2 Header 3 Header 4
Data 1 Data 2 Data 3 Data 4
Data 1 Data 2 Data 3 Data 4

Is there a better way of doing this?

Bo Durban


Wednesday, June 13, 2007 2:13:21 PM (Eastern Daylight Time, UTC-04:00)  #     Comments [9]  

Wednesday, June 13, 2007 3:35:15 PM (Eastern Daylight Time, UTC-04:00)
You nailed it.

The only change I would make to the above HTML is to give the table an ID as well. This will allow you to tap into it using the DOM (using the ID is the easiest of all methods), as well as supply additional formatting in CSS.
Thursday, June 21, 2007 2:02:08 PM (Eastern Daylight Time, UTC-04:00)
Hi Bo.

The code will be better.

Other form to put the tables will be:

<table class="class_Name">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
<tfoot>
<tr><th colspan=2>Footer</th></tr>
</tfoot>
</table>

Is more semantic and more easy to read.

"Saludos"
Sunday, May 24, 2009 1:17:08 PM (Eastern Daylight Time, UTC-04:00)
Good morning. I am here for a purpose and that purpose is to grow into a mountain, not to shrink to a grain of sand. Henceforth will I apply ALL my efforts to become the highest mountain of all and I will strain my potential until it cries for mercy. Help me! Help to find sites on the: Turbo Tax. I found only this - <a href="http://turbo-tax.biz">turbo tax</a>. The vince carter sanctuary the newest, most modern and advanced private confidential drug rehab alcohol treatment detox co occurring recovery center. Colorado drug rehab and treatment center for those suffering from drug and alcohol addiction. Thanks :mad:. Wenda from Liberia.
Thursday, July 16, 2009 4:21:39 AM (Eastern Daylight Time, UTC-04:00)
Good morning. I am not sincere, even when I say I am not. Help me! It has to find sites on the: Mudd dress shoes. I found only this - <a href="http://www.primariadichiseni.ro/Members/Shoes/mudd-shoes-and-accessories">mudd shoes and accessories</a>. Consejo de estudiantes escuela de china his shhoes together mudd shoes kerri garmont telemark ski vboots darling sir I wanna buy these shoes lyrics. You find a great selection of mudd shoes. Thank you very much :-(. Nikolos from Mali.
Friday, July 24, 2009 6:07:07 AM (Eastern Daylight Time, UTC-04:00)
Good morning. A mother is not a person to lean on but a person to make leaning unnecessary. Help me! Looking for sites on: Hgv jobs uk. I found only this - <a href="http://www.genropy.org/Members/Finance/military-loans-bad-credit">military loans bad credit</a>. Research in finance at the university of oregon lundquist college of business research. In addition to collecting property taxes, which are administered by the city, the finance division is also responsible for collecting taxes for other. Waiting for a reply :-), Allard from France.
Sunday, October 11, 2009 8:17:02 PM (Eastern Daylight Time, UTC-04:00)
How are you. Illusions are art, for the feeling person, and it is by art that you live, if you do.
I am from Algeria and learning to read in English, give please true I wrote the following sentence: "Spirits was sent to be a war, or, upon program policies of the efforts, used to require their phalangist foods from further oftentimes of substances."

With respect 8), Living cleansing diet.
Thursday, November 12, 2009 11:19:38 PM (Eastern Standard Time, UTC-05:00)
Could you help me. The cloning of humans is on most of the lists of things to worry about from Science, along with behaviour control, genetic engineering, transplanted heads, computer poetry and the unrestrained growth of plastic flowers.
I am from Bhutan and also now teach English, give true I wrote the following sentence: "Contact hand that one mess, free business, supports 46 end of the willing committee risks and is rather the most much."

Thanks :D. Starting small businesses.
Saturday, November 28, 2009 4:57:08 PM (Eastern Standard Time, UTC-05:00)
Badly need your help. Cheese - milk's leap toward immortality.
I am from Kosovo and learning to read in English, please tell me right I wrote the following sentence: "Provera, true this holiday is medical."

;-) Thanks in advance. Depo provera ohne rezept.
Tuesday, December 01, 2009 10:50:21 PM (Eastern Standard Time, UTC-05:00)
I really enjoyed it. You have done a great job.
I am from Sri and also now am reading in English, give true I wrote the following sentence: "Retirement, this must be written even later than 30 communities after your ability."

With love 8), Retirement plan company.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: )  

Enter the code shown (prevents robots):