Lesson 6 of 23
In progress
Lists and tables, used for the right job
Group items with ordered, unordered and description lists, and build accessible data tables without using tables for layout.
Lists and tables look simple, and both are easy to misuse. A list says "these items belong together"; a table says "these values relate across rows and columns". Neither is a layout tool.
What you will learn
- Choose between
ul,olanddl. - Build a table with headers that screen readers can associate with cells.
- Recognise the layout-table anti-pattern.
Three kinds of list
ul is unordered: the order does not matter, such as features or ingredients. ol is ordered: sequence matters, such as steps. dl is a description list of term and definition pairs.
Editable. Runs in your browser.
<h2>Method</h2>
<ol>
<li>Mix the ingredients.</li>
<li>Rest the dough for an hour.</li>
<li>Bake until golden.</li>
</ol>
<h2>Glossary</h2>
<dl>
<dt>Proofing</dt>
<dd>Letting shaped dough rise before baking.</dd>
</dl>
Press Run to see the output.
Only li children belong directly inside ul or ol. If you need a paragraph inside an item, put it inside the li. Nesting a list inside an li creates a sub-list, which is correct and common for navigation.
Lists are also the standard structure for navigation. A nav containing a ul of links is easier to style and to navigate with a screen reader than a row of loose anchors.
Tables are for data
A table needs a caption, header cells and a body. th marks headers, and scope says whether a header applies down a column or across a row.
Editable. Runs in your browser.
Press Run to see the output.
caption names the table and is announced before the content, which matters when a page has several tables. thead and tbody separate structure from data; browsers use them for printing and sticky headers. scope="row" on the first cell of each row makes "Python, 30, 30" understandable instead of three numbers.
Do not merge cells casually. colspan and rowspan are valid, but they make the header associations harder to follow, and screen reader support varies. If a table needs many merged cells, a simpler layout might communicate better.
Tables are not layout
Before Flexbox and Grid, tables were used to position page sections. The result is a document that announces "table with 12 rows" before reading anything, and that collapses on a phone.
If you are reaching for a table because you want columns side by side, you want CSS layout. Keep tables for values that genuinely relate across two dimensions: prices by plan, scores by student, hours by day.
Tables also need care on narrow screens. A wide table can overflow its container, which forces the whole page to scroll sideways. The usual fix is a wrapper with overflow-x: auto and a sensible minimum width, added in the CSS module.
Lists inside tables and tables inside lists
Both are legal and sometimes right: a cell containing a list of features, or a list of tables in a report. Keep nesting shallow. If a cell contains three nested lists and a form, the data probably wants a different structure.
Editable. Runs in your browser.
Press Run to see the output.
A nested list belongs inside the li it extends, not between two li elements. Putting it outside produces invalid markup that browsers repair in different ways.
Using a table to lay out a form or a page makes the content read as tabular data and breaks down on small screens. If the goal is visual alignment, use CSS grid. A screen reader announcing "row 3, column 2" for a label is a strong sign the table is doing the wrong job.
Build a table of three tools with columns for name, price and free plan (yes or no). Add a caption and column headers, and make the tool name a row header. Then add an ordered list of three steps to get started with one of them.
Show solution
Editable. Runs in your browser.
<h2>Getting started</h2>
<ol>
<li>Download the editor.</li>
<li>Install the language extension.</li>
<li>Open a folder and create a file.</li>
</ol>
Press Run to see the output.
The table answers "which tool, at what price, with what plan" across two dimensions. The ordered list is a sequence, which is what ol is for.
Recap
ulfor unordered items,olfor sequence,dlfor term and definition pairs.- A data table needs a caption,
thheaders andscopeso cells can be associated. - Never use tables for page layout; that is CSS’s job.
Next, you will build forms that collect information without trapping the person filling them in.
Can I style list bullets?
Do tables need a tbody?
Progress is saved in this browser only. No account, no email.