Web Notes: Tables & Cells
<tablecellspacing=12>
<table class="test">
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
<table cellspacing="10">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
https://www.w3schools.com/tags/att_table_cellspacing.asp
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_table_cellspacing
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_cellspacing
<style>
table, th, td {
border: 1px solid black;
padding: 0px;
}
table {
border-spacing: 0px;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
CSS Table Border
When you apply CSS border code to the table element (i.e. the <table> tag), the border only appears around the actual table - not the individual cells.
Like this: <table style="border:1px solid black;">
<tr>
<th>Table Header</th><th>Table Header</th>
</tr>
<tr>
<td>Table cell 1</td><td>Table cell 2</td>
</tr>
<tr>
<td>Table cell 3</td><td>Table cell 4</td>
</tr>
</table>
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|
You can also change the border style to thick, dotted, dashed, or anything that CSS understands. You can change its color too. Here's some examples:
<table style="border:1px dotted black;">
<tr>
<th>Table Header</th><th>Table Header</th>
</tr>
<tr>
<td>Table cell 1</td><td>Table cell 2</td>
</tr>
<tr>
<td>Table cell 3</td><td>Table cell 4</td>
</tr>
</table>
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|
<table style="border:1px dashed black;">
<tr>
<th>Table Header</th><th>Table Header</th>
</tr>
<tr>
<td>Table cell 1</td><td>Table cell 2</td>
</tr>
<tr>
<td>Table cell 3</td><td>Table cell 4</td>
</tr>
</table>
<table style="border:3px dashed blue;">
<tr>
<th>Table Header</th><th>Table Header</th>
</tr>
<tr>
<td>Table cell 1</td><td>Table cell 2</td>
</tr>
<tr>
<td>Table cell 3</td><td>Table cell 4</td>
</tr>
</table>
<table style="border:5px double black;">
<tr>
<th>Table Header</th><th>Table Header</th>
</tr>
<tr>
<td>Table cell 1</td><td>Table cell 2</td>
</tr>
<tr>
<td>Table cell 3</td><td>Table cell 4</td>
</tr>
</table>
Borders on Table Cells
To the table cells within the table, you need to apply the border code against the individual table cells.
In the following example, I've applied a border against the table cells and the table header cells. I've also applied a different colored border around the table (as I did in the previous example). The different color will help distinguish between the table border and the cell borders:
<table style="border:1px solid black;">
<tr>
<th style="border:1px solid red;">Table Header</th><th style="border:1px solid red;">Table Header</th>
</tr>
<tr>
<td style="border:1px solid red;">Table cell 1</td><td style="border:1px solid red;">Table cell 2</td>
</tr>
<tr>
<td style="border:1px solid red;">Table cell 3</td><td style="border:1px solid red;">Table cell 4</td>
</tr>
</table>
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|
Border Collapse
You can remove the space between the different borders by using the CSS border-collapse property. You can apply this property against the HTML table element.
When you apply this against the table element, you'll notice that the table border simply disappears, or "collapses". You'll also notice that the space between the cells collapse too.
Example:
<table style="border:1px solid black;border-collapse:collapse;">
<tr>
<th style="border:1px solid red;">Table Header</th><th style="border:1px solid red;">Table Header</th>
</tr>
<tr>
<td style="border:1px solid red;">Table cell 1</td><td style="border:1px solid red;">Table cell 2</td>
</tr>
<tr>
<td style="border:1px solid red;">Table cell 3</td><td style="border:1px solid red;">Table cell 4</td>
</tr>
</table>
| Source Code |
Result |
|
| Table Header |
Table Header |
| Table cell 1 |
Table cell 2 |
| Table cell 3 |
Table cell 4 |
|