Sub topic 8
Use Tables for Comparisons Across Attributes
This chapter assumes you know basic HTML table elements (table, tr, td), have read the earlier chapters on heading hierarchy and answer-first sections, and can run a short Python script. By the end you should be able to decide when a table is the right format, mark one up so that each cell keeps its meaning when pulled out of the page, and avoid the errors that break that meaning.
Why tables matter for retrieval
A table is the right format when several items are compared across the same set of attributes. Each cell then answers a question of the form "what is the value of attribute B for item A?" That structure is hard to express in a paragraph without repetition, and it is easy to lose in a list.
AI answer systems that retrieve web content generally fetch a page, split it into passages, and select passages that match a query. Tables raise two problems for that process:
- Splitting. A table may be cut across passages. If the header row ends up in one passage and the body rows in another, the numbers in the second passage have no labels.
- Flattening. Many pipelines convert HTML to plain text or Markdown before processing. Merged cells, nested tables, and layout tricks often come out scrambled in that conversion.
No retrieval system publishes a guarantee about how it handles your table. The practical response is to build tables that survive the worst case: small, regular, labelled, and introduced by a sentence that states what the table shows.
Anatomy of a data table
A well-formed data table has these parts:
| Element | Purpose | Notes |
|---|---|---|
| `<caption>` | Names the table | First child of `table`. Say what is compared. |
| `<thead>` | Groups the header row | Holds column headers. |
| `<th scope="col">` | Labels a column | One per column. |
| `<tbody>` | Groups the data rows | Holds the values. |
| `<th scope="row">` | Labels a row | Identifies the item in that row. |
| `<td>` | Holds one value | One value per cell. |
Here is a complete example, as in the topic's reference markup:
<table>
<caption>Plan comparison</caption>
<thead>
<tr>
<th scope="col">Plan</th>
<th scope="col">Monthly price</th>
<th scope="col">Users</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Basic</th>
<td>$10</td>
<td>1</td>
</tr>
<tr>
<th scope="row">Team</th>
<td>$40</td>
<td>10</td>
</tr>
</tbody>
</table>Three details do most of the work:
- The caption gives the table a name that travels with it. A converter that keeps the caption keeps the context.
- Column headers turn
$40into "Team plan, monthly price, $40" for anyone or anything that reads the structure. - Row headers make the first column an identifier rather than just another value. Without
scope="row", the item name is only a cell like any other.
The scope attribute states which direction a header applies to. It also helps screen readers announce a cell with its headers, so the same markup serves accessibility and machine reading.
Lead with the answer, then the table
The answer-first rule from the previous chapter applies to tables. Put one sentence before the table that states the main finding, then let the table carry the detail.
<h2>Which plan fits a small team?</h2>
<p>The Team plan fits groups of up to 10 people at $40 per month.</p>
<table>
<caption>Plan comparison</caption>
<!-- header and body rows as above -->
</table>This helps in two ways. A system that quotes only the first sentence still gives a correct answer. A system that quotes the table has a sentence nearby that describes it. The sentence should not just repeat the table cell by cell. It should state the conclusion a reader would draw from it.
Choosing rows and columns
Decide which dimension is the list of items and which is the list of attributes.
- Put the items being compared in rows. Each row is one plan, product, version, or option.
- Put the attributes in columns. Each column is one measurable property, such as price, limit, or supported platform.
This orientation matters because rows are usually read as units. When a pipeline turns a table into text, each row often becomes one line. A row that describes one complete item is easy to quote. A row that mixes attributes from different items is not.
Keep the table narrow. Five or six columns is a practical ceiling for text-based conversions. If you need more, split the table by theme (for example "Pricing" and "Limits"), each with its own caption and a heading above it.
Rules for cell content
Each cell should hold one value, expressed so it makes sense alone.
- Use units in the cell or header. Write
$10or put "Monthly price (USD)" in the header. A bare10is ambiguous. - Use consistent formats. If one row says
10 GBand another says10240 MB, the comparison is harder for everyone. - Avoid empty cells. Write
None,Not included, or0as appropriate. An empty cell can mean "no", "unknown", or "forgot". - Avoid symbols alone. A checkmark or colour dot carries meaning visually but may disappear in conversion. Write "Yes" or "No", optionally with the symbol.
- Avoid lists inside cells. If a cell needs several values, the attribute is probably better handled as a separate table or a list.
<tr>
<th scope="row">Team</th>
<td>Yes</td>
<td>Not included</td>
</tr>Merged cells, nesting, and layout
Merged cells (rowspan and colspan) make a table readable to a person scanning it, but break the one-value-per-cell grid that machines expect. After conversion, a merged cell may appear once and leave the other positions blank, or it may shift every later cell one column over.
Replace a merged cell by repeating the value, or split the table into two tables under their own headings.
Nested tables have the same problem and add another: a converter cannot tell which outer cell the inner table belongs to. Avoid them.
Do not use tables for page layout. A table that positions a sidebar and a main column is not a data table, and it creates passages that mix unrelated content. Use CSS for layout and keep table for data.
The same applies to tables rendered as images. A screenshot of a pricing table gives a text-based system nothing to read. If the table must be an image, an adjacent real table or a text description is required, and the real table should be the primary version.
Tables built without table elements
Some pages show tabular data using div elements and CSS grid. This looks like a table but exposes no row or column relationships. Unless you add ARIA roles correctly (role="table", role="row", role="columnheader", role="cell"), the structure is invisible. Using the native table element is simpler and more reliable than reproducing it with roles.
Responsive design is the usual reason for this choice. Better options keep the native table: let it scroll horizontally inside a container, or reduce the number of columns.
Checking tables with a script
You can test the most common table problems automatically. The script below uses only the Python standard library. It reports tables that lack a caption, lack header cells, or use colspan or rowspan.
from html.parser import HTMLParser
class TableAudit(HTMLParser):
def __init__(self):
super().__init__()
self.tables = []
self.current = None
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "table":
self.current = {
"caption": False,
"headers": 0,
"merged": False,
"scoped": 0,
}
self.tables.append(self.current)
elif self.current is not None:
if tag == "caption":
self.current["caption"] = True
elif tag == "th":
self.current["headers"] += 1
if attrs.get("scope") in ("col", "row"):
self.current["scoped"] += 1
elif tag == "td" or tag == "th":
pass
if "colspan" in attrs or "rowspan" in attrs:
self.current["merged"] = True
def handle_endtag(self, tag):
if tag == "table":
self.current = None
def problems(table):
found = []
if not table["caption"]:
found.append("missing caption")
if table["headers"] == 0:
found.append("no header cells")
elif table["scoped"] < table["headers"]:
found.append("header cells without scope")
if table["merged"]:
found.append("uses colspan or rowspan")
return found
html = """
<table>
<tr><td>Plan</td><td>Price</td></tr>
<tr><td colspan="2">Basic: $10</td></tr>
</table>
<table>
<caption>Plan comparison</caption>
<tr><th scope="col">Plan</th><th scope="col">Price</th></tr>
<tr><td>Basic</td><td>$10</td></tr>
</table>
"""
audit = TableAudit()
audit.feed(html)
for number, table in enumerate(audit.tables, start=1):
print(number, problems(table) or "ok")Running it prints:
1 ['missing caption', 'no header cells', 'uses colspan or rowspan']
2 okThe script checks structure, not meaning. It cannot tell you whether the caption is useful or whether the values are consistent. Those checks remain manual.
Seeing what a converter sees
A useful manual test is to view your table the way a flattening tool would. Copy the table into a plain-text editor, or run a Markdown converter over the page. Then read the result and ask:
- Can I tell what the table compares without looking at the page?
- Does every number sit next to its label?
- If I read only one row, do I know which item it describes?
If any answer is no, the markup or the content needs work. A table that reads clearly as plain text almost always reads clearly in the original.
How retrieval might split a table
The diagram shows why a long table is risky. A splitter that cuts by size can separate the header row from later rows.
You cannot control where a splitter cuts. You can reduce the damage by keeping tables short, splitting long ones into themed tables with their own captions, and making row headers descriptive so that each row still identifies its item.
Table or list?
Use a table when items share several attributes. Use a list when items have one dimension, such as steps in order or a set of requirements. The next chapter covers that decision in detail. A quick test: if you would write the same attribute name more than twice, put the attribute in a column header.
| Content | Better format | Reason |
|---|---|---|
| Three plans compared on price, users, support | Table | Several items, several shared attributes |
| Steps to reset a router | Ordered list | One dimension, order matters |
| Requirements for an account | Unordered list | One dimension, order does not matter |
| Why a plan was chosen | Paragraph | Reasoning, not a grid of values |
Common table mistakes
- No caption or header cells. The table is only a grid of values.
- Header row made with
tdand bold styling. It looks right but exposes no headers. Useth. - Merged cells for visual grouping. Repeat values or split the table.
- Symbols and colours as values. Add text.
- Blank cells. State what the absence means.
- Table as image. Provide a real table.
- Very wide or very long tables. Split by theme.
- Table with no surrounding sentence. Add an answer-first sentence and a heading above it.
Summary
Use a table when items are compared across shared attributes. Give it a caption, th cells with scope, one value per cell, and a sentence before it that states the conclusion. Keep it narrow, avoid merged cells and images, and test it by reading its plain-text form. Automated checks catch missing structure, and a manual read catches unclear meaning.
Exercise
A page shows a pricing comparison as a grid of div elements, with a bold first row styled with CSS and a screenshot of the same grid below it for mobile users. List four changes that would make this content more usable for a retrieval system, and say what each one fixes.
Show answer
1) Replace the div grid with a native table, so rows and columns are exposed as structure. 2) Mark the first row as th cells with scope="col" (and the first column with scope="row"), so every value has labels. 3) Add a caption that names the comparison, so the table keeps its context when extracted. 4) Remove the screenshot as the primary version, or keep it only alongside the real table, because text-based systems cannot read an image. Optionally, add a sentence before the table that states the main conclusion, and handle mobile with horizontal scrolling rather than a second image.
Exercise
Run the audit script on a table that has a caption, th cells with scope, and one cell with rowspan="2". What does it report, and how would you fix the table?
Show answer
It reports ['uses colspan or rowspan']. To fix it, remove the rowspan and repeat the value in each row it covered, or split the table into two tables under separate headings so every cell holds one value in a regular grid.