CHAPTER 28 · HTML
Table columns and styling: make the data easier to read
Learn what colgroup and col describe, then add a small amount of CSS for borders, spacing and column backgrounds. Every new styling rule is explained before you are asked to change it.
Download example files (ZIP)Keep the jobs separate
Your first small CSS rule
table { border-collapse: collapse; }
th, td { border: 1px solid #687466; padding: 8px; text-align: left; }Complete program: readable-table.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>A Readable Practice Count</title>
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #687466; padding: 8px; text-align: left; }
thead { background-color: #e8efdf; }
</style>
</head>
<body>
<h1>Read the practice counts clearly</h1>
<table>
<caption>Examples checked this week</caption>
<thead>
<tr><th scope="col">Topic</th><th scope="col">Checked examples</th></tr>
</thead>
<tbody>
<tr><th scope="row">Lists</th><td>3</td></tr>
<tr><th scope="row">Links</th><td>4</td></tr>
</tbody>
</table>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Column groups are descriptions of columns
<colgroup>
<col>
<col span="2" class="count-columns">
</colgroup>- 1Rows and cells: the actual data
- 2Colgroup and col: column descriptions
- 3CSS rules: borders, spacing and backgrounds
- 4Cell classes: text alignment
A col is not the parent of table cells. Only specific CSS properties apply to columns.
Complete program: column-counts.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Compare Planned and Checked Work</title>
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #687466; padding: 8px; text-align: left; }
.count-columns { background-color: #fff1ce; }
.number { text-align: right; }
</style>
</head>
<body>
<h1>Compare the two practice counts</h1>
<table>
<caption>Planned and checked examples by topic</caption>
<colgroup>
<col>
<col span="2" class="count-columns">
</colgroup>
<thead>
<tr><th scope="col">Topic</th><th scope="col">Planned</th><th scope="col">Checked</th></tr>
</thead>
<tbody>
<tr><th scope="row">Lists</th><td class="number">5</td><td class="number">3</td></tr>
<tr><th scope="row">Links</th><td class="number">4</td><td class="number">4</td></tr>
</tbody>
</table>
<p>The two shaded columns contain counts, as their headers also say.</p>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Col does not style every property
Spacing, stripes and width
Problem and solution: the numbers will not align
Common mistakes and useful tips
Putting data text inside colgroup instead of cells. Writing a closing col tag. Giving colgroup both span and child col elements. Expecting every CSS property on a column to affect cells. Using colour as the only way to label important data. Copying obsolete border, bgcolor, cellpadding or cellspacing attributes into new pages. Editing presentation to disguise incorrect table structure.
Practice questions
What is a CSS selector? How do border and padding differ? What does col span="2" describe? Where should colgroup appear relative to caption and thead? Why is text-align applied to numeric td cells rather than col in the example? Add a third topic with planned and checked counts to column-counts.html. Keep both numeric columns shaded and align only their data cells to the right.
Check your practice answers
The part of a CSS rule that chooses which elements receive its declarations. Border draws an edge. Padding creates inner space between that edge and cell content. Two consecutive column positions, beginning at that col's place in column order. After caption and before the row groups such as thead. Col is not the cell's parent and does not support text alignment as a way to style the cell text. Apply that property to the cells themselves. Add one body row with a topic th and two numeric td cells carrying class="number". The existing column description still covers the same three columns, so it does not need another col.
Interview questions
Does colgroup create or contain a table's records?
Why can a column background be hidden?
How do border-spacing and padding differ?
Can styling replace meaningful headers?
Quick revision
Use CSS for appearance and HTML for meaningful table structure. Colgroup and col describe consecutive columns without containing their values. Column properties are limited; style text on actual cells. Borders, padding and backgrounds should improve readability without carrying meaning alone.