T1 HTML · Lesson 4
Lists and tables
Bulleted and numbered lists, lists inside lists and lists of terms; tables written row by row, with heading cells, a caption, a head, a body and a foot; and cells stretched across columns and rows.
- make bulleted and numbered lists, and choose the right one for the content
- put a list inside a list, and write a list of terms and their meanings
- build a table row by row, with heading cells and a caption
- mark a table's head, body and foot, and say which way each heading reads
- stretch a cell across columns or rows without breaking the table
The back cover
Much of what people read every day is a list or a table: a shopping list, the steps of a recipe, a class timetable, a shop bill. HTML has an element for each. Then the browser, and everything else that reads the page, knows exactly what it is looking at.
These pages start with lists. Some have bullets, where order does not matter, and some have numbers, where it does. Lists can sit inside lists, and one kind pairs terms with their meanings.
Then come tables, always written row by row. They have heading cells, a caption, a head, a body and a foot. Some cells stretch across columns or down rows.
Before you start
The story: The worst board in the market
The market square's price board is the busiest panel in the city, and the worst. Prices, weights and stall numbers are written in one long line, and people stand squinting at it, arguing.
"Is the rice twelve or twenty?" a cook asks. "And which stall?" Nobody can tell where one item ends and the next begins. The market warden finds you at the Masons' Hall. "Master Tobin says you can fix it," he says. "The traders cannot read their own prices."
On the same board, the steps for getting a stall licence are scrambled too, in no order at all.
Lessons 2 and 3 built pages of headings, paragraphs, links and pictures. Much of what people put on pages is neither a paragraph nor a picture: a shopping list, the steps of a recipe, a class timetable, a marks sheet. This lesson gives each of these its own elements. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Elements nest like boxes. | <p>Rice is <strong>10% off</strong></p>: strong sits inside p. | List items sit inside lists; cells sit inside rows, rows inside tables. |
| Block and inline. | A div starts a new line; a span sits inside one. | Lists and tables are blocks; each item starts on its own line. |
| Attributes. | <a href="about.html">: a name and a quoted value. | start and reversed number a list; colspan and rowspan stretch a cell. |
| The browser repairs broken HTML. | An em left open runs on into the next paragraph. | A broken table is repaired too, sometimes in surprising ways. |
| Screen readers read the page aloud. | They read alt text where a picture is. | They also say "list, 4 items" and read a table's headings with each cell. |
| Order can matter, or not. | The steps of making chai must go in order; the items in a tiffin need not. | Ordered and unordered lists. |
| Rows and columns. | A class timetable: days down the side, periods across the top. | A table is rows of cells, read row by row. |
| A heading names what is below it. | An h2 Offers names the section under it. | A table's heading cells name each column or row. |
| DevTools' Elements panel. | It shows the page the browser built, after repairs. | It shows the parts a browser adds to a table by itself. |
By the end of this lesson you will be able to:
- make bulleted and numbered lists, and choose the right one for the content;
- put a list inside a list, and write a list of terms and their meanings;
- build a table row by row, with heading cells and a caption;
- mark a table's head, body and foot, and say which way each heading reads;
- stretch a cell across columns or rows without breaking the table.
In one line
Lists hold items, in order or not; tables hold data in rows and columns. You need lesson 2's nesting, block and inline, and attributes.
Lists: ul, ol and li
At a glance
HTML has two everyday lists. A ul, an unordered list, is for items whose order does not matter, like a shopping list; the browser marks each with a bullet. An ol, an ordered list, is for items whose order matters, like the steps of a recipe; the browser numbers them. Each item, in either list, is an li, a list item.
The browser draws the bullets and the numbers itself, so they are never typed. A real list also tells a screen reader how many items are coming. An ol can start from another number, or count down.
Take a guess: Which kind of list fits the steps for making tea: one where order matters, or one where it does not?
Look at two pieces of paper on a kitchen counter. One is a shopping list: rice, toor dal, atta, sugar. You can buy them in any order. The other is a recipe card for chai: boil the water, add the tea leaves, add milk and sugar, strain it. Here the order is the whole point: strain it first and you get nothing. HTML has a list for each kind.
A ul element (ul is for unordered list): a list whose order does not matter. The browser marks each item with a bullet.
<ul>
<li>Rice</li>
<li>Toor dal</li>
<li>Atta</li>
<li>Sugar</li>
</ul>
Checked in Chrome
- four items, each inside the list
- each item is marked with a round bullet
Each item is an li element (li is for list item). The items sit inside the ul, one box inside another, as in lesson 2. You never type the bullets: the browser draws them. The check asks the browser which marker it drew, and it answers disc, a filled round bullet.
An ol element (ol is for ordered list): a list whose order matters. The browser numbers each item, 1, 2, 3, and so on.
<ol>
<li>Boil a cup of water.</li>
<li>Add a spoon of tea leaves.</li>
<li>Add milk and sugar, and boil again.</li>
<li>Strain it into a cup.</li>
</ol>
Checked in Chrome
- four steps
- each step is numbered 1, 2, 3, 4
The items are the same li elements; only the box around them changed, from ul to ol. The browser now numbers them. You never type the numbers either. That pays off when you add a step in the middle: every number after it moves up by itself.
A memory aid
O is for order. If swapping two items would make the list wrong, use ol. If not, use ul.
Why not type the bullets yourself?
You may wonder: why not write the items as lines with a <br> between them and a dot typed at the start of each? On the screen it can look the same. But the browser, and everything that reads the page, no longer knows it is a list.
A screen reader meets a real list and says "list, 4 items" before reading it, so the listener knows how much is coming. It can jump from item to item. With typed dots and line breaks, it reads one long line of text. Search engines, and the CSS step's styles, also work from the real list.
Numbering from somewhere else
An ol has two attributes worth knowing. start="5" starts the numbering at 5, which helps when a list continues after some other text. The attribute reversed, with no value, counts down instead of up, as in a top 3 countdown.
<p>Top 3 hill stations this summer:</p>
<ol reversed>
<li>Munnar</li>
<li>Manali</li>
<li>Ooty</li>
</ol>
Checked in Chrome
- the list counts down: 3, 2, 1
- three items
Like controls in lesson 3, reversed is switched on just by being written. The browser numbers Munnar 3, Manali 2 and Ooty 1.
| Element or attribute | Means | Marker |
|---|---|---|
<ul> | unordered list: order does not matter | a bullet |
<ol> | ordered list: order matters | a number |
<li> | one list item, always inside a ul or an ol | drawn by the list around it |
start="5" | on ol: start counting at 5 | 5, 6, 7 |
reversed | on ol: count down | 3, 2, 1 |
Worked example 1
A railway site shows the stations a Mumbai local train stops at, from Churchgate: Churchgate, Marine Lines, Charni Road, Grant Road. Mark it up as a list, and choose ul or ol.
What we have: four stations, in the order the train reaches them
What we need: a list of the right kind
The plan: ask: would swapping two stations make the list wrong? Then write one li per station
- Swap Marine Lines and Grant Road, and the list tells a passenger the wrong stop comes next. So order matters.
- Order matters, so the list is an ol, and the browser numbers the stops.
<ol>
<li>Churchgate</li>
<li>Marine Lines</li>
<li>Charni Road</li>
<li>Grant Road</li>
</ol>
Checked in Chrome
- four stops
- Churchgate comes first
An ol with four li elements, in the order the train reaches them.
Check: read the numbered list aloud: stop 1 Churchgate, stop 2 Marine Lines. The numbers mean something, so ol was right.
A path that fails: Typing the stations on lines with <br> and numbers typed by hand. It looks like a list, but a screen reader hears one long line, and adding a station means renumbering every line by hand.
When you are stuck: lists
Ask the order question: would swapping two items make it wrong? Yes means ol, no means ul.
Every item gets its own li, and every li sits directly inside the ul or ol.
Never type bullets or numbers: the list draws them.
Your turn
Which list suits the items packed in a school bag: ul or ol?Tap to reveal
- Does the order of packing matter?
<ul>: the order does not matter.
What does li stand for?Tap to reveal
- It is one entry of a list.
List item.
Which attribute makes an ol count down, 3, 2, 1?Tap to reveal
- It is written with no value.
reversed.
Did you know?
Lists were in the very first HTML
Lists are as old as HTML itself. HTML Tags, the first public list of HTML's elements, written around 1991, already says: Opening list tags are UL, a list of multi-line paragraphs, MENU, a list of smaller paragraphs, and DIR, a list of short elements. Of those three, ul is the one this topic uses.
Why it fits here: The ul of this topic has been part of HTML since the first list of tags, older than most of the elements met so far.
Sources: W3C archive: HTML Tags, the first list of tags; Wikipedia: HTML
A checklist that saves lives
An ordered list can be serious. In 2008 the World Health Organization published the WHO Surgical Safety Checklist: a short list of items for the surgical team to check before and after an operation, to prevent harm such as infections or instruments left behind. Several studies have shown it cutting deaths and complications by as much as one third where it is used.
Why it fits here: An ol is for steps whose order and completeness matter; a checklist in an operating theatre is the most serious example there is.
Sources: Wikipedia: WHO Surgical Safety Checklist; WHO: Safe surgery
In one line
ul is a list whose order does not matter (bullets), and ol one whose order matters (numbers). Each item is an li inside it; the browser draws the markers, so never type them.
Lists inside lists, and lists of terms
At a glance
A list can sit inside another list, like the sections of a restaurant menu, each with its own dishes. The inner list goes inside an li of the outer list, after that item's words, and the browser changes the bullet at each level.
A third kind of list pairs terms with their meanings, like the glossary at the back of a textbook. It is the description list, dl. Each term is a dt, the description term, and its meaning is a dd, the description details. There are no bullets: the terms themselves mark the entries.
Take a guess: Where does an inner list go: inside an li of the outer list, or between two li elements?
A restaurant menu has sections, Starters, Main course, Desserts, and each section has its own dishes. That is a list whose items each hold a smaller list. HTML builds it the same way: a list goes inside an item of another list.
<ul>
<li>Starters
<ul>
<li>Paneer tikka</li>
<li>Veg pakora</li>
</ul>
</li>
<li>Main course
<ul>
<li>Dal makhani</li>
<li>Jeera rice</li>
</ul>
</li>
</ul>
Checked in Chrome
- two sections in the outer list
- four dishes, each inside a section's own list
- the inner list is marked with hollow circles
Look where the inner ul sits: inside the li of its section, after the word Starters and before that li closes. So each section's li holds two things: its name and its own list of dishes.
The browser marks the inner list with a different bullet, a hollow circle instead of a filled disc, so the eye sees the levels. A third level gets small squares.
A common mistake
Putting the inner ul between two li elements, directly inside the outer ul. A ul may hold only li elements. The inner list must go inside an li, the one it belongs to.
Lists of either kind can nest inside each other. The steps of a recipe can be an ol whose step 2 holds a ul of the spices to add. Each list keeps its own markers.
A list of terms and their meanings
A glossary at the back of a textbook is a third kind of list. Each entry is a term followed by its meaning. HTML has a list for exactly this, the description list.
A dl element (description list) holding pairs: a dt for each term (description term) and a dd for its meaning (description details).
<dl>
<dt>MRP</dt>
<dd>Maximum retail price: the most a shop may charge.</dd>
<dt>GST</dt>
<dd>Goods and services tax, added to most bills.</dd>
</dl>
Checked in Chrome
- two terms
- two meanings
- each term is followed by its meaning
The browser shows each term on its own line, with its meaning pushed in below it. There are no bullets or numbers: the terms themselves do the job of markers.
| List | For | Its items |
|---|---|---|
<ul> | items in any order | <li>, with bullets |
<ol> | items in an order that matters | <li>, numbered |
<dl> | terms and their meanings | <dt> for a term, <dd> for its meaning |
Worked example 2
A school's page lists its clubs by day: Monday has the Science club and the Chess club; Wednesday has the Music club. Mark it up as a nested list.
What we have: two days, each with its own clubs
What we need: an outer list of days, each day holding a list of its clubs
The plan: an outer ul with one li per day; inside each day's li, after its name, a ul of that day's clubs
<ul>
<li>Monday
<ul>
<li>Science club</li>
<li>Chess club</li>
</ul>
</li>
<li>Wednesday
<ul>
<li>Music club</li>
</ul>
</li>
</ul>
Checked in Chrome
- two days
- Monday holds two clubs
- Wednesday holds one
An outer ul of two days, each li holding the day's name and an inner ul of its clubs.
Check: the checks count the clubs inside each day's own li, which works only if every inner list is in the right place.
A path that fails: Closing Monday's li before its clubs, as <li>Monday</li><ul>...</ul>. The inner list then sits loose in the outer ul, belonging to no day.
When you are stuck: nested lists
Find the item the inner list belongs to. The inner list goes inside that li, before its closing tag.
Indent each level two spaces further, so the boxes show at a glance.
For a term and its meaning, use dl, dt and dd instead.
Your turn
Where does an inner list go: between two li elements, or inside one?Tap to reveal
- A ul may hold only li elements.
Inside the li it belongs to, before that li closes.
Which element holds the meaning of a term in a description list?Tap to reveal
- dt holds the term itself.
<dd>, description details.
How does the browser mark the items of a list inside a list?Tap to reveal
- Think of the bullet's shape.
With a different bullet: hollow circles at the second level, squares at the third.
Did you know?
The dl began as a glossary
The description list is one of the oldest lists of all. The first list of HTML tags already describes it under the heading Glossaries: a glossary, or definition list, is a list of paragraphs each of which has a short title alongside it. Apart from glossaries, it adds, this format is useful for presenting a set of named elements to the reader.
Why it fits here: The dl, dt and dd of this topic still do exactly that job: a short title beside each entry.
A dl is for more than words
Today's HTML standard describes the dl as an association list: groups, each made of one or more names followed by one or more values. It says those groups may be terms and definitions, metadata topics and values, questions and answers, or any other groups of name-value data. A page of frequent questions can be a dl.
Why it fits here: The pairs of this topic are not only words and meanings: any name with its value can be a dt and a dd.
In one line
A list nests inside an li of another list, and the browser changes its bullets per level; a dl pairs terms (dt) with their meanings (dd).
Tables: rows of cells
At a glance
A table is written row by row, never column by column. Each row is a tr, a table row, and holds its cells from left to right. A th cell is a heading and a td cell is data. When every row holds the same number of cells, the columns line up by themselves.
A caption, the first thing inside the table, names what the table is about, and a screen reader reads it first. Tables are for data with rows and columns, such as a timetable or a price list, and not for arranging a page.
Take a guess: To build a timetable in HTML, do you write it one row at a time or one column at a time?
The class timetable on a school notice board has days down the left side and periods across the top. To find what you have on Tuesday in period 3, you run your finger along Tuesday's row to period 3's column. Information that you read across and down like this belongs in a table.
A table element holding rows. Each row is a tr (table row), and each row holds cells: a td (table data) for an ordinary cell, or a th (table heading) for a heading cell.
Rows first, always
Here is the most important idea of this section. HTML writes a table row by row, never column by column. You write the first row, left to right, then the second row, and so on. The columns appear by themselves: the first cell of every row lines up into the first column.
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Rice, 5 kg</td>
<td>650</td>
</tr>
<tr>
<td>Toor dal, 1 kg</td>
<td>180</td>
</tr>
</table>
Checked in Chrome
- three rows: one of headings, two of data
- two cells in every row, so two columns
- row 3, column 2 holds 180
- the heading cells are bold
Read it as the browser does. The first tr holds two th cells, Item and Price: the headings. The next two tr elements hold two td cells each: an item and its price. Because every row has two cells, the table has two columns.
The browser shows th cells in bold and centred, so they look like headings. But the look is not the point, as with strong in lesson 2. A th means "this cell names the others". A screen reader uses that: on the cell 180, it can say "Price, 180", so a listener knows what the number is.
A memory aid
tr is a row, td is data, th is a heading. Rows hold cells; cells never sit loose in a table.
A caption names the table
A table on its own does not say what it is about. The caption element gives it a title, shown above it. It must be the first thing inside the table.
<table>
<caption>Asha Stores: prices this week</caption>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Sugar, 1 kg</td>
<td>45</td>
</tr>
</table>
Checked in Chrome
- the table's caption
- it comes first, before the rows
A caption is to a table what figcaption is to a picture in lesson 3: a line everyone sees, which names what follows. A screen reader reads it first, so a listener knows which table they are in.
Tables are for data, not for layout
Long ago, before CSS could arrange a page, people built whole page layouts out of tables: the menu in one cell, the article in another. Screen readers then announced the page as a table of rows and columns, which made no sense. Use a table only for data that has rows and columns. The CSS step lays out pages.
Worked example 3
Write a table of three students' marks: Asha 92, Ravi 85, Meera 78, with the headings Name and Marks, and the caption Class 9 B: Maths test.
What we have: three students and their marks, two headings and a caption
What we need: a table with a caption, a heading row and one row per student
The plan: the caption first; one tr of th cells; then one tr of td cells per student, each in the same order as the headings
<table>
<caption>Class 9 B: Maths test</caption>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr>
<td>Asha</td>
<td>92</td>
</tr>
<tr>
<td>Ravi</td>
<td>85</td>
</tr>
<tr>
<td>Meera</td>
<td>78</td>
</tr>
</table>
Checked in Chrome
- one heading row and three student rows
- two cells in every row
- Meera's row holds 78 in the Marks column
- a caption
A table with the caption, one row of two th cells, and three rows of two td cells.
Check: every row has as many cells as there are headings, which is what keeps the columns straight.
A path that fails: Trying to write the Name column first, then the Marks column. HTML has no element for a column of cells; tables are written only row by row.
Worked example 4
A shopkeeper asks why her price table shows the word Rice in bold and centred, like a heading. Her row is <tr><th>Rice</th><td>650</td></tr>. What is wrong?
What we have: a data cell marked as a heading
What we need: the slip and the fix
The plan: ask what th means, and whether Rice is a heading here
- th means "this cell names the others".
- In a price list with headings Item and Price, Rice is data under Item, not a heading.
- So Rice should be a td, like 650.
Rice is marked as th; make it <td>Rice</td>.
Check: after the fix, every cell in the data rows is a td, and only the first row's cells are th.
Another way: If the table had no heading row and the item names were meant to label their rows, th would be right, with scope="row"; the next section shows how.
When you are stuck: tables
Write it on paper first as a grid, then type it one row at a time, left to right.
Count the cells in each row: every row should have as many as the heading row.
Headings go in th, everything else in td, and a caption, if any, goes first.
Your turn
Does HTML write a table row by row, or column by column?Tap to reveal
- Which element exists: one for a row, or one for a column?
Row by row, with tr; the columns form from the cells' positions.
What do td and th stand for?Tap to reveal
- One is ordinary data, one names the others.
Table data and table heading.
Where in a table does the caption go?Tap to reveal
- Before or after the rows?
First, before any row.
Did you know?
Tables came years after the web
Tables are younger than the web. The first list of HTML tags appeared in late 1991, but HTML tables were described only in RFC 1942, HTML Tables, published in May 1996 by Dave Raggett of the W3C. Its very first example is a test table with merged cells, the kind of table taught at the end of this lesson.
Why it fits here: The tr, th and td of this topic, and the caption, all come from that 1996 design.
Sources: RFC 1942: HTML Tables; Wikipedia: HTML
The table that sold a computer
In 1979 Dan Bricklin, a Harvard MBA student, and the programmer Bob Frankston made VisiCalc, a table of rows and columns on a computer screen that recalculated itself when a number changed. It is called the first spreadsheet program for personal computers. The Computer History Museum says it turned the personal computer into a business machine and boosted the sales of the Apple II.
Why it fits here: A grid of rows and columns, the shape of this topic's tables, became the most useful screen in business.
Sources: Computer History Museum: Timeline of Computer History, 1979; Wikipedia: VisiCalc
In one line
A table is written row by row: each tr holds cells, th for headings and td for data. The same number of cells in every row makes the columns, and a caption, first, names the table.
A table's head, body and foot
At a glance
A table has three parts, like a shop bill. thead holds the heading row, tbody holds the rows of data, and tfoot holds the totals. If a table has no tbody, the browser adds one around the rows by itself, which matters later when CSS picks out rows.
The scope attribute on a th says which way the heading reads: scope="col" for a heading of the column below it, and scope="row" for a heading of the row beside it. With it, a screen reader can name both headings of any cell, as a finger finds them on a timetable.
Take a guess: A table has rows but no tbody written: does the browser leave it that way?
A shop bill has three parts. At the top, the headings: Item, Qty, Amount. In the middle, the items bought, as many as there are. At the bottom, the total. Each part does a different job, and HTML lets you mark each one.
| Element | Is short for | Holds |
|---|---|---|
<thead> | table head | the row or rows of headings |
<tbody> | table body | the rows of data |
<tfoot> | table foot | the rows of totals or notes at the end |
<table>
<caption>Household spending, October</caption>
<thead>
<tr><th>Item</th><th>Amount</th></tr>
</thead>
<tbody>
<tr><td>Rent</td><td>12,000</td></tr>
<tr><td>Groceries</td><td>6,500</td></tr>
<tr><td>Electricity</td><td>1,800</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td><td>20,300</td></tr>
</tfoot>
</table>
Checked in Chrome
- one row of headings in the head
- three rows of data in the body
- the foot holds the total, 20,300
Each part wraps its rows: the tr elements go inside thead, tbody or tfoot, and the cells inside the tr, as before. The total checks: 12,000 + 6,500 + 1,800 is 20,300.
Why mark the parts at all? The page looks almost the same. But now the browser knows which rows are headings and which is the total. CSS can style each part separately, such as a shaded head and a bold foot. Scripts can sort the body's rows without moving the headings or the total. And screen readers can tell a listener where they are.
The browser adds a body by itself
Here is a surprise. Write a table with rows directly inside it and no tbody, as in the last section, and the browser puts a tbody around the rows itself. Every table in the browser has at least one body.
<table>
<tr><td>Rice</td><td>650</td></tr>
</table>
Checked in Chrome
- the browser added a tbody
- and the row now sits inside it
You can see this in DevTools' Elements panel: the table shows a tbody you never wrote. It matters in the CSS step, where a rule written for table > tr finds nothing, because the rows are inside the tbody.
Which way does a heading read?
Some tables have headings across the top, naming columns. Some have headings down the left side, naming rows. Many have both, like the timetable on the notice board. The attribute scope on a th says which way it reads: scope="col" for a heading of the column below it, scope="row" for a heading of the row beside it.
<table>
<thead>
<tr><th scope="col">Day</th><th scope="col">Period 1</th><th scope="col">Period 2</th></tr>
</thead>
<tbody>
<tr><th scope="row">Monday</th><td>Maths</td><td>Science</td></tr>
<tr><th scope="row">Tuesday</th><td>English</td><td>Hindi</td></tr>
</tbody>
</table>
Checked in Chrome
- the top headings name columns
- the side headings name rows
- Tuesday, period 2: Hindi
With scope written, a screen reader on the cell Hindi can say "Tuesday, Period 2, Hindi", just as your finger finds it on the board. In a small table, browsers can often guess, but scope makes it certain.
Worked example 5
A school canteen's bill has the headings Item and Amount, two items (Samosa 20, Chai 15) and a total. Mark up its head, body and foot, and work out the total.
What we have: two headings, two items with amounts, and a total to find
What we need: a table with thead, tbody and tfoot, and the right total
The plan: headings in thead with scope="col"; each item a row in tbody; the total, 20 + 15, in tfoot
- The total is 20 + 15 = 35.
<table>
<thead>
<tr><th scope="col">Item</th><th scope="col">Amount</th></tr>
</thead>
<tbody>
<tr><td>Samosa</td><td>20</td></tr>
<tr><td>Chai</td><td>15</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td><td>35</td></tr>
</tfoot>
</table>
Checked in Chrome
- a head and a foot
- the body's amounts add up to the foot's total
thead with Item and Amount, tbody with the two rows, tfoot with Total 35.
Check: the second check adds up the body's amounts in the browser and compares them with the total in the foot: 35.
When you are stuck: a table's parts
Sort the rows into three piles: headings, data, totals. Each pile goes in thead, tbody or tfoot.
Rows still need tr, and cells still need td or th, inside each part.
For each th, ask: does it name the column below it (scope="col") or the row beside it (scope="row")?
Your turn
Which part of a table holds the total at the bottom?Tap to reveal
- Head, body or foot?
<tfoot>, the table foot.
A table has rows written directly inside it, with no tbody. What does the browser do?Tap to reveal
- Look in the Elements panel.
It adds a tbody around the rows by itself.
Which scope does a heading in the left column, naming its row, take?Tap to reveal
- It reads across, along its row.
scope="row".
Did you know?
The tbody nobody wrote
The HTML standard spells out exactly how a browser reads a table. When a row or a cell starts directly inside a table, the browser first inserts a tbody element, with no attributes, and then reads the row again inside it. So every table that has rows also has at least one tbody, whether its author wrote one or not.
Why it fits here: This is the surprise of this topic, written down in the standard: the browser builds the tbody itself.
Why the totals sit at the foot
The word foot is also a verb from bookkeeping. A dictionary gives its meaning as: to add up a column of numbers and write the sum at the bottom, as in footed up the bill. The same word means to pay, as in footing the expense. The tfoot of a table sits exactly where that sum always went.
Why it fits here: The tfoot of this topic holds the totals, at the foot of the columns, as a bookkeeper's sum always did.
Sources: American Heritage Dictionary: foot; Wiktionary: foot
In one line
thead holds the headings, tbody the data (the browser adds one if you do not), tfoot the totals; scope="col" or scope="row" says which way each th reads.
Stretching a cell across columns or rows
At a glance
Sometimes one cell should cover two squares, like a three-day holiday drawn as one long box on a calendar. The colspan attribute stretches a cell across columns, and rowspan stretches it down rows. The value says how many.
The cells must then be counted. A square that a spanned cell already covers is filled, so the row it reaches into is written with one cell fewer. An extra cell has nowhere to go and sticks out of the table into a new column.
Take a guess: A cell spans two rows: how many cells does the second row then need to write?
On a wall calendar, a holiday that lasts three days is often drawn as one long box across the three days, not three small boxes. Tables sometimes need the same: one cell that covers the width of two columns, or the height of two rows.
Attributes on a td or th. colspan (column span) makes the cell as wide as that many columns; rowspan (row span) makes it as tall as that many rows.
Across columns: colspan
A marks sheet often has a heading Marks over two columns, Maths and Science. That heading cell must cover both columns.
<table>
<tr><th rowspan="2">Name</th><th colspan="2">Marks</th></tr>
<tr><th>Maths</th><th>Science</th></tr>
<tr><td>Asha</td><td>92</td><td>88</td></tr>
</table>
Checked in Chrome
- Marks spans two columns
- Marks starts where the Maths column starts
- and ends where the Science column ends
The checks ask the browser where it drew each cell. The Marks cell begins at the left edge of the Maths column and ends at the right edge of the Science column: it covers both.
Look at the first row: it has only two cells, Name and Marks, but the table has three columns. Name takes one column and Marks takes two, so the row still adds up to three. Name also has rowspan="2", which is the next idea: it reaches down into the second row.
Down rows: rowspan, and the cell you leave out
Here is a sports day schedule. Two events happen on the main ground, one after the other. Instead of writing Main ground twice, one cell covers both rows.
<table>
<tr><th>Time</th><th>Event</th><th>Place</th></tr>
<tr><td>9:00</td><td>100 m race</td><td rowspan="2">Main ground</td></tr>
<tr><td>9:30</td><td>Relay race</td></tr>
<tr><td>10:00</td><td>Chess</td><td>Hall</td></tr>
</table>
Checked in Chrome
- Main ground spans two rows
- the 9:30 row has only two cells of its own
- Main ground sits in the Place column
The key point is the 9:30 row. It has only two cells, not three, because its Place is already taken: the Main ground cell from the row above reaches down into it. If you wrote a third cell there, it would have nowhere to go.
Count the cells
Every row must fill the same number of columns. A cell with colspan="2" counts as two in its row. A cell with rowspan="2" counts in its own row and in the row below, so the row below leaves one cell out.
Worked example 6
A school timetable has the columns Day, Period 1, Period 2 and Period 3. On Friday, periods 2 and 3 are one long Science practical. Write Friday's row.
What we have: four columns, and one subject that covers two periods
What we need: a row whose cells fill the four columns
The plan: Friday's day cell, then period 1, then one cell with colspan="2" for periods 2 and 3; check the count: 1 + 1 + 2 = 4
<table>
<tr><th>Day</th><th>Period 1</th><th>Period 2</th><th>Period 3</th></tr>
<tr><th>Thursday</th><td>Maths</td><td>English</td><td>Hindi</td></tr>
<tr><th>Friday</th><td>Maths</td><td colspan="2">Science practical</td></tr>
</table>
Checked in Chrome
- Friday's row has three cells
- the practical spans two columns
- it ends where period 3 ends
<tr><th>Friday</th><td>Maths</td><td colspan="2">Science practical</td></tr>
Check: the last check confirms the practical ends exactly where the Period 3 column ends.
Worked example 7
Someone added a Place for the relay race, writing the 9:30 row as three cells: <td>9:30</td><td>Relay race</td><td>Main ground</td>, while the row above keeps rowspan="2". What happens, and what is the fix?
What we have: a row with a cell that the rowspan above has already filled
What we need: what the browser does with it, and the right row
The plan: count the columns the row needs, then see where the extra cell goes
<table>
<tr><th>Time</th><th>Event</th><th>Place</th></tr>
<tr><td>9:00</td><td>100 m race</td><td rowspan="2">Main ground</td></tr>
<tr><td>9:30</td><td>Relay race</td><td>Main ground</td></tr>
</table>
Checked in Chrome
- (the slip: the extra cell is pushed out into a fourth column, past Place)
The extra cell sticks out into a fourth column; write the row as <td>9:30</td><td>Relay race</td>.
Check: the check found the extra cell starting to the right of the Main ground cell, outside the Place column.
A path that fails: Removing the rowspan instead and writing Main ground in both rows. That works, but the table no longer shows that both events share one place, which was the point.
When you are stuck: spanning cells
Draw the table as a grid on paper and shade each spanning cell over the squares it covers.
Then write each row, leaving out every square that a cell from an earlier row already covers.
Add up each row: colspan counts its number; every row's total must match the heading row's.
Your turn
Which attribute makes a cell as wide as two columns?Tap to reveal
- Column span.
colspan="2".
A cell in row 2 has rowspan="2". How many cells does row 3 leave out in that column?Tap to reveal
- The cell from row 2 reaches down into row 3.
One: row 3 writes one cell fewer.
A table has four columns. A row holds a cell with colspan="3". How many more cells does it need?Tap to reveal
- 3 + ? = 4.
One more cell, since 3 + 1 = 4.
Did you know?
Merged cells from the very start
Merged cells are not a later trick. The very first example in RFC 1942, the 1996 specification of HTML tables, is titled A test table with merged cells. In it a heading cell spans two rows and another spans two columns, with ROWSPAN=2 and COLSPAN=2 written right into its tags.
Why it fits here: The colspan and rowspan of this topic were part of HTML tables from their first published example.
Source: RFC 1942: HTML Tables
A heading that spans needs a wider scope
When a heading cell spans several columns, a screen reader needs to know which data cells it names. The W3C's accessibility tutorial says such a header cell should be tied to the data cells of its column group, by giving its scope the value colgroup, and the same principle applies to header cells that span multiple rows.
Why it fits here: A spanned heading of this topic covers more than one column, so its scope must cover more than one column too.
In one line
colspan stretches a cell across columns and rowspan down rows; count the cells, because a spanned square is already filled, and an extra cell sticks out of the table.
Where it is used
Lists and tables are everywhere once you look for them. Many things that do not look like lists are lists underneath.
| What you learned | Where it is used | A real example |
|---|---|---|
| ul | menus and navigation bars, product features, search results | a shop's top menu (Home, Offers, Contact) is a ul, laid out in a row by CSS |
| ol | steps, rankings, directions, the order of play | a bank's "How to open an account" page numbers its steps with an ol |
| nested lists | menus with sub-menus, a book's contents | a contents page: chapters, each with its own list of sections |
| dl, dt, dd | glossaries, product details, FAQ pages | a phone's specification page: Battery, then 5000 mAh; Weight, then 190 g |
| table, tr, th, td | timetables, price lists, marks sheets, bank statements | a railway timetable of trains, stations and times |
| thead, tbody, tfoot | any table with a total or a fixed heading row | a bank statement: headings at the top, transactions in the body, the closing balance in the foot |
| scope | tables read aloud by screen readers | a results portal whose marks table reads "Maths, 92" to a blind student |
| colspan, rowspan | headings over groups of columns, merged timetable slots | a marks sheet's Marks heading over Maths and Science |
Mistakes and debugging
As before, the browser shows no error for any of these. Some show as an odd-looking page; some look fine and quietly break things for screen readers and for the CSS step.
1. A list item with no list
<li>Rice</li>
<li>Toor dal</li>
Checked in Chrome
- (the slip: the items sit in the body, in no list)
The bullets still show, so it looks right. But there is no list: a screen reader does not say "list, 2 items", and the items belong to nothing. The fix: wrap them in a ul or an ol.
2. A label typed inside the list
Writing <ul>Groceries<li>Rice</li></ul> puts loose text inside a ul, which may hold only li elements. The fix: put the label before the list, in a heading or a paragraph: <p>Groceries</p><ul><li>Rice</li></ul>.
3. Text between the rows of a table
<table>
<tr><td>Rice</td><td>650</td></tr>
Total
<tr><td>Dal</td><td>180</td></tr>
</table>
Checked in Chrome
- (the slip: the browser moved the loose word Total out, above the table)
This one surprises everyone. The word Total was written inside the table, but not in any cell. The browser has nowhere to put it, so it moves it out and shows it above the table. The fix: every piece of text in a table goes in a cell: <tr><td>Total</td><td>830</td></tr>.
4. A row with too many cells
As the spans section showed, a row with an extra cell, often one that a rowspan above already covers, sticks out into a new column. The fix: count each row's cells, counting a colspan by its number, and leave out the squares a rowspan fills.
5. Headings written as td
A heading row written with td cells looks like data. A screen reader then cannot say which column a number belongs to. The fix: th for every heading cell, with scope.
6. A table used for layout
Putting a page's menu, article and sidebar in the cells of one big table. The fix: tables only for data in rows and columns; the CSS step arranges pages.
The debugging corner: see the table the browser built
Open DevTools (F12) and the Elements panel, and open up the table. You will see the tbody the browser added, any text it moved out, and every cell in the row it really landed in. Comparing that with your HTML finds most table slips.
Quick card
The lesson on one card.
Every element of the lesson
<ul>
<li>Rice</li>
<li>Spices
<ul><li>Jeera</li><li>Haldi</li></ul>
</li>
</ul>
<ol start="2">
<li>Wash the rice.</li>
<li>Boil it.</li>
</ol>
<dl><dt>MRP</dt><dd>Maximum retail price</dd></dl>
<table>
<caption>Bill</caption>
<thead><tr><th scope="col">Item</th><th scope="col">Qty</th><th scope="col">Amount</th></tr></thead>
<tbody>
<tr><td>Rice</td><td>1</td><td>650</td></tr>
<tr><td>Jeera</td><td>2</td><td>120</td></tr>
</tbody>
<tfoot><tr><td colspan="2">Total</td><td>770</td></tr></tfoot>
</table>
Checked in Chrome
- a list inside a list item
- the steps start at 2
- a term and its meaning
- a caption and a head
- Total spans two columns
- the amounts add up to the total, 770
| Element or attribute | What it is |
|---|---|
<ul>, <ol> | a list in any order (bullets); a list in order (numbers) |
<li> | one item, always inside a ul or an ol; it may hold a list of its own |
start, reversed | on ol: count from a number; count down |
<dl>, <dt>, <dd> | a list of terms; a term; its meaning |
<table>, <caption> | a table of data; its title, first inside it |
<tr> | a row; tables are written row by row |
<th>, <td> | a heading cell; a data cell |
<thead>, <tbody>, <tfoot> | the headings; the data (added by the browser if missing); the totals |
scope="col", scope="row" | a heading of the column below; of the row beside |
colspan, rowspan | a cell across columns; down rows (the row below leaves a cell out) |
The story: No arguments
You rebuild the board in rows and columns. Each row is one item; each column is one kind of fact: the item, its weight, its price, its stall. A heading sits on top of every column, so the cook can run a finger from rice to its price without guessing.
The licence steps become a numbered list, because their order matters, and the market's opening days become a plain list, because it does not.
The warden watches a crowd read the board in silence. "No arguments," he says, amazed. "I have never seen that before."
Reagents gathered: unordered list, ordered list, table row, heading cell, colspan.
Sources
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
- HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)