Sample Tables with CSS
Examples of How to Modify Tables with CSS
A Standard Table with Head and Body
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses this HTML:
<table>
<thead>
<tr><th>Name</th><th>Alias</th></tr>
</thead>
<tbody>
<tr><td>Jennifer Kyrnin</td><td>Web Design / HTML Guide</td></tr>
<tr><td>Mark Kyrnin</td><td>PC Hardware / Reviews Guide</td></tr>
<tr><td>Shasta</td><td>Moe</td></tr>
<tr><td>McKinley</td><td>Kinkin</td></tr>
</tbody>
</table>
And this CSS:
table { margin: 1em; }
td, th { padding: .3em; }
A Standard Table with a CSS Border
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses the same HTML as above and this CSS:
table { margin: 1em; border: 1px solid #ccc; }
td, th { padding: .3em; }
Borders on the Cells
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses the same basic HTML (see above) and this CSS:
table { margin: 1em; }
td, th { padding: .3em; border: 1px #ccc solid; }
Pushing the Cells Together
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses the same basic HTML (see above) and this CSS:
table { margin: 1em; border-collapse: collapse; }
td, th { padding: .3em; border: 1px #ccc solid; }
Color on the Header
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses the same basic HTML (see above) and this CSS:
table { margin: 1em; border-collapse: collapse; }
td, th { padding: .3em; border: 1px #ccc solid; }
thead { background: #fc9; }
Color on the Body
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses the same basic HTML (see above) and this CSS:
table { margin: 1em; border-collapse: collapse; }
td, th { padding: .3em; border: 1px #ccc solid; }
thead { background: #fc9; }
tbody { background: #9cf; }
Highlight Rows
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses this HTML:
<table class="hilite" id="highlight">
<thead>
<tr><th>Name</th><th>Alias</th></tr>
</thead>
<tbody>
<tr><td>Jennifer Kyrnin</td><td>Web Design / HTML Guide</td></tr>
<tr><td>Mark Kyrnin</td><td>PC Hardware / Reviews Guide</td></tr>
<tr><td>Shasta</td><td>Moe</td></tr>
<tr><td>McKinley</td><td>Kinkin</td></tr>
</tbody>
</table>
Uses this CSS:
table { margin: 1em; border-collapse: collapse; }
td, th { padding: .3em; border: 1px #ccc solid; }
thead { background: #fc9; }
tbody { background: #9cf; }
#highlight tr.hilight { background: #c9f; }
Uses this JavaScript:
function tableHighlightRow() {
if (document.getElementById && document.createTextNode) {
var tables=document.getElementsByTagName('table');
for (var i=0;i<tables.length;i++)
{
if(tables[i].className=='hilite') {
var trs=tables[i].getElementsByTagName('tr');
for(var j=0;j<trs.length;j++)
{
if(trs[j].parentNode.nodeName=='TBODY') {
trs[j].onmouseover=function(){this.className='hilight';return false}
trs[j].onmouseout=function(){this.className='';return false}
}
}
}
}
}
}
window.onload=function(){tableHighlightRow();}
Highlight a Row by Clicking on It
Double click on the same row to turn off the highlight.
| Name | Alias |
|---|---|
| Jennifer Kyrnin | Web Design / HTML Guide |
| Mark Kyrnin | PC Hardware / Reviews Guide |
| Shasta | Moe |
| McKinley | Kinkin |
Uses this HTML:
<table class="clickon" id="clickme">
<thead>
<tr><th>Name</th><th>Alias</th></tr>
</thead>
<tbody>
<tr><td>Jennifer Kyrnin</td><td>Web Design / HTML Guide</td></tr>
<tr><td>Mark Kyrnin</td><td>PC Hardware / Reviews Guide</td></tr>
<tr><td>Shasta</td><td>Moe</td></tr>
<tr><td>McKinley</td><td>Kinkin</td></tr>
</tbody>
</table>
Uses this CSS:
table { margin: 1em; border-collapse: collapse; }
td, th { padding: .3em; border: 1px #ccc solid; }
thead { background: #fc9; }
tbody { background: #9cf; }
#clickme tr.clicked { background: #ff0; }
Uses this JavaScript:
function tableHighlightRow() {Jennifer Kyrnin
if (document.getElementById && document.createTextNode) {
var tables=document.getElementsByTagName('table');
for (var i=0;i<tables.length;i++)
{
if(tables[i].className=='clickme') {
var trs=tables[i].getElementsByTagName('tr');
for(var j=0;j<trs.length;j++)
{
if(trs[j].parentNode.nodeName=='TBODY') {
trs[j].onclick=function(){this.className='clicked';return false}
trs[j].ondblclick=function(){this.className='';return false}
}
}
}
}
}
}
window.onload=function(){tableHighlightRow();}

