CSS table border - CSS border collapse


Home - Tutorials - CSS tutorials

In this article I will demonstrate how you can use effectively CSS table borders to style your html tables.

Tutorial info:


Name:CSS table border
Total steps:3
Category:CSS tutorials
Date:2007-11-09
Level:Beginner
Product:See complete product
Viewed:122448

Bookmark CSS table border



AddThis Social Bookmark Button

Step 3 - CSS border collapse


CSS table border

Our actual table is looking quite good, but it would be fine if there will be no such "double" border effect but a simple grid.

Unfortunately border-spacing property is not supported by all today's browser we need to edit a bit the html table and set cellspacing to 0. In this case our table code will be:

Code:
  1. <table cellspacing="0">
  2. <tr><th>Th-1</th><th>Th-2</th><th>Th-3</th></tr>
  3. <tr><td>cell-11</td><td>cell-12</td><td>cell-13</td></tr>
  4. <tr><td>cell-21</td><td>cell-22</td><td>cell-23</td></tr>
  5. </table>

which results the following output.

Th-1Th-2Th-3
cell-11cell-12cell-13
cell-21cell-22cell-23

Hmm, it is not really what we want. It seems as a fat border, but we want a simple one.

Fortunately there is a solution and you can remove the cellspacing parameter from your table as well. The magic property is border-collapse. Setting this property to collapse we will get the final result.

So the final code is this:

Code:
  1. <style type="text/css">
  2. <!--
  3. table {
  4. border: 1px solid #666666;
  5. width:300px;
  6. border-collapse: collapse;
  7. }
  8. td { border: 1px solid #666666; }
  9. th { border: 1px solid #666666; }
  10. -->
  11. </style>
  12. </head>
  13.  
  14. <tr><th>Th-1</th><th>Th-2</th><th>Th-3</th></tr>
  15. <tr><td>cell-11</td><td>cell-12</td><td>cell-13</td></tr>
  16. <tr><td>cell-21</td><td>cell-22</td><td>cell-23</td></tr>
  17. </table>
  18. </body>
  19. </html>
  20.  

And our final table looks like this:

 

Th-1Th-2Th-3
cell-11cell-12cell-13
cell-21cell-22cell-23

This is much better than the original, isn't it? 

 




Previous Step of CSS table border


Tags: CSS table border, css table, css border, table border, css, table, border

CSS table border - Table of contents
Step 1 - Introduction
Step 2 - Border style
Step 3 - CSS border collapse

F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Windows F1
 
 
MaxTutorial
Monthly mortgage payment calculator
WebFormGenerator
Alpari forex

Total time: 0.2947