Sub topic 7
Tables
This chapter assumes you can edit a Markdown file on GitHub and know the basics of headings and inline code. Tables are a GitHub Flavored Markdown (GFM) extension. They are not part of CommonMark, so they only render where the renderer supports GFM.
The basic structure
A table has three parts: a header row, a delimiter row, and zero or more body rows. Pipes (|) separate the cells.
| Option | Type | Default |
|--------|--------|---------------|
| `port` | number | `3000` |
| `host` | string | `"localhost"` |The delimiter row is required. It needs at least three characters per cell, and these are usually hyphens. Without it, GitHub shows the text as a plain paragraph.
You do not have to line up the pipes in the source. Aligned pipes only make the file easier to read in an editor.
Column alignment
Colons in the delimiter row set the alignment of each column.
| Delimiter | Alignment |
|---|---|
| `:---` | Left |
| `:---:` | Center |
| `---:` | Right |
| `---` | Default (left) |
Right-align numeric columns so digits line up:
| Option | Type | Default |
|:-------|:------:|--------:|
| `port` | number | `3000` |
| `host` | string | `"localhost"` |What goes in a cell
Cells hold inline content only. You can use emphasis, inline code, links, and images. You cannot use lists, headings, or fenced code blocks, and a cell cannot span several lines. If a cell needs that much structure, a list under a heading is the better choice.
To show a literal pipe inside a cell, escape it with a backslash, even inside a code span:
| Syntax | Meaning |
|---------------|----------------|
| `a \| b` | a or b |Common errors
- Missing delimiter row. The table does not render at all.
- Too few cells in a body row. GitHub fills the missing cells with empty ones.
- Too many cells in a body row. GitHub drops the extra cells without warning.
- Header and delimiter counts differ. The table does not render, because the two rows must have the same number of cells.
- No blank line before the table. Put one between the table and any paragraph above it so the table is read as its own block.
- An unescaped pipe in a cell. GitHub splits the cell in two.
The table ends at the first blank line.
Diagram of the parse
Exercise
A table in your README shows as plain text with visible pipes. The header row looks correct. What do you check first, and what do you check next?
Show answer
First check that a delimiter row such as |---|---| directly follows the header row. Next check that it has the same number of cells as the header row. Also confirm there is a blank line before the table.
Exercise
Write a two-column table with the headers Flag and Description. Right-align the Description column. Put the code a|b in one body cell.
Show answer
| Flag | Description |
|:-----|------------:|
| --mode | Accepts a\|b |