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

HTML describes the table's content and relationships. CSS, short for Cascading Style Sheets, describes its appearance. A table can be structurally correct before it has borders, colours or extra spacing.

Think of a handwritten register. Its labels and values are the information; ruled lines and a shaded column help you read that information. Improving the ruling does not fix a value written under the wrong label.

We will use a few CSS rules here because they make the table examples easier to inspect. You do not need to learn the whole CSS language first. Keep the table HTML meaningful even if those rules are removed.

Your first small CSS rule

Put a style element in head. Its content is CSS, not ordinary HTML tags.

Example fragment — use inside a complete program
table { border-collapse: collapse; }
th, td { border: 1px solid #687466; padding: 8px; text-align: left; }

A selector chooses elements. Table chooses table elements; th, td is a list selecting both header and data cells. Curly braces contain the settings, called declarations. A declaration has a property name, a colon, a value and usually a semicolon.

PropertyWhat this example asks for
border-collapse: collapseShare adjacent table borders rather than drawing separated cell borders
border: 1px solid #687466Draw a one-CSS-pixel solid line in the chosen colour
padding: 8pxAdd eight CSS pixels of inner space around cell content
text-align: leftAlign the cell text to the left
background-colorPaint a background without replacing the cell's meaning

Px is a CSS length unit. The # followed by six hexadecimal digits is a colour notation. You may use the values supplied while learning. Unlike HTML's integer image width attributes, CSS lengths here do include units.

Complete program: readable-table.html

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>
Browser previewDocument title: A Readable Practice Count

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.

Expected result: cell borders appear, text has room inside each cell and the header group has a pale background. The data still means three checked list examples and four checked link examples. Delete the style element temporarily and compare: the structure and words remain, while presentation changes.

Column groups are descriptions of columns

Colgroup describes a group of consecutive table columns. Col describes one column, or several consecutive columns through its span attribute. They do not contain the table's cell text. The actual values still belong to th and td inside rows.

Place column groups after caption and before the row groups. Col is void and has no closing tag. Colgroup normally has opening and closing tags.

HTML fragment — part of a document
<colgroup>
  <col>
  <col span="2" class="count-columns">
</colgroup>

The first col describes the first column. The next col describes the next two. Together they account for three column positions. Class supplies a reusable label for selecting elements. A CSS selector starting with a dot, such as .count-columns, chooses that class.

An alternative form is an empty colgroup with span, such as colgroup span="2". If colgroup has its own span attribute, do not also give it child col elements. Choose the form that expresses your groups clearly.

Keep structure and styling separate
  1. 1Rows and cells: the actual data
  2. 2Colgroup and col: column descriptions
  3. 3CSS rules: borders, spacing and backgrounds
  4. 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

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>
Browser previewDocument title: Compare Planned and Checked Work

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.

Expected result: the Planned and Checked columns have a pale background. Their data cells align numbers to the right through the number class. The Topic column stays unshaded. Notice that the background class is on col, while the text-alignment class is on the actual td cells.

Col does not style every property

Columns have special CSS behaviour. Background and width are useful column properties; borders have effects under the collapsed-border model, and visibility has special rules. Ordinary font, text colour, padding and text-align settings on col do not flow down into cells as if the cells were its children. Cells are children of rows, not children of col.

Apply text alignment, font choices and padding to the actual th or td cells instead. A col background also sits underneath cell and row backgrounds; an opaque cell background can cover it. If your column colour seems to disappear, inspect those layers before assuming span is wrong.

Spacing, stripes and width

Padding creates space inside a cell. Border-spacing controls gaps between cell borders in the separate-border model; it has no effect when border-collapse is collapse. These are different kinds of space.

For alternate body-row shading, tbody tr:nth-child(even) selects the even-numbered sibling rows inside each tbody. That counting starts again in each group. If a row class would make the intention clearer, use one rather than memorising a complex selector.

Column width can influence layout, but the browser must still consider content and the table's layout rules. It is not a guarantee that very long content will fit a tiny width. Do not hide or clip important values simply to preserve a decorative size.

Problem and solution: the numbers will not align

Problem: a student writes text-align: right on col and expects all the cells in that column to inherit it.

Solution: put a suitable class on the actual numeric cells and select that class in CSS, as the program does. A col describes columns, but it is not the parent of those cell elements.

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

  1. What is a CSS selector?
  2. How do border and padding differ?
  3. What does col span="2" describe?
  4. Where should colgroup appear relative to caption and thead?
  5. Why is text-align applied to numeric td cells rather than col in the example?
  6. 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
  1. The part of a CSS rule that chooses which elements receive its declarations.
  2. Border draws an edge. Padding creates inner space between that edge and cell content.
  3. Two consecutive column positions, beginning at that col's place in column order.
  4. After caption and before the row groups such as thead.
  5. 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.
  6. 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?

No. It describes column groups. Records remain in rows and cells. Column descriptions and row data are different parts of the table model.

Why can a column background be hidden?

Table backgrounds are layered. An opaque background on a cell or row can cover the column background underneath. Check the relevant styles before changing the HTML.

How do border-spacing and padding differ?

Border-spacing separates adjacent cell borders in the separate-border model. Padding creates space inside each cell. Collapsed borders do not use border-spacing gaps.

Can styling replace meaningful headers?

No. Colour, alignment and bold text can help sighted readers but do not establish the same relationships as real th headers and appropriate scope or headers references.

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.