Choose lists and tables for the content
What you will learn
- Distinguish unordered items, ordered steps and tabular data.
- Associate table headings with their cells.
Choose a structure that expresses the relationships in the content. Ingredients form a group whose sequence usually does not matter, so an unordered list, ul, is useful. Instructions must happen in sequence, so use an ordered list, ol. Both contain li elements for their items.
Do not type bullet characters or numbers into paragraphs to imitate a list. A real list lets the browser and assistive tools recognise the group. The browser supplies the markers. You can change their presentation later without changing the content's meaning.
A table is useful when the relationship between rows and columns matters. For example, each recipe quantity belongs to a particular number of servings. Use table, tr for rows, th for heading cells and td for data cells. A caption identifies what the table compares.
In a simple table, scope="col" says that a heading labels a column. A row heading can use scope="row". This is enough for our simple serving table. More complicated tables require additional care; splitting one into simpler tables is often easier to understand.
Tables are not a general page-layout tool. A pair of unrelated cards should not become two table cells just to put them next to each other. We will learn Flexbox and Grid for layout later. For now, let lists, paragraphs and tables appear in normal document flow.
Working example
The HTML shown here is the body fragment. The download includes the complete document.
<main><h1>Tomato toast</h1>
<h2>Ingredients</h2><ul><li>Bread</li><li>Tomato</li><li>Olive oil</li></ul>
<h2>Method</h2><ol><li>Toast the bread.</li><li>Add chopped tomato and oil.</li></ol>
<table><caption>Bread needed by serving count</caption><tr><th scope="col">Servings</th><th scope="col">Slices</th></tr><tr><td>1</td><td>2</td></tr><tr><td>2</td><td>4</td></tr></table></main>Try a change
Add salt to the ingredients, add a final serving step, and add a row for three servings. Keep list items inside their lists and cells inside a table row.
What you should observe
The ingredients have bullets, the method has sequence numbers, and the new row pairs three servings with six slices.
Check your understanding
Would a table be the right element for putting a biography next to a portrait?
Read the explanation
No. Those are not row-and-column data. Use meaningful content elements, then use CSS layout when you reach module 4.