In the previous lesson you built tables with rows and cells, and you saw in DevTools that the browser added a tbody element of its own accord. That was a clue: a table's rows aren't a flat list; they belong to groups, each with a different role. Some rows give the columns their titles (the head), some hold the data (the body, which can be split into several blocks) and some contain notes or totals (the foot). In this lesson you will learn to mark up those groups with thead, tbody and tfoot, to use th as a row header too and to describe columns with colgroup and col. With all of this you will bring The Blue Cup's specialities and coffees together in a single menu, get back the categories that were lost and create the café's weekly opening hours.
Contents
- The three row groups:
thead,tbodyandtfoot thead: the table headtbody: the body, and why there can be severaltfoot: the table foot, in the code and on screenthin rows and in columns- Describing columns:
colgroupandcol - What all this structure is for
- Project: the complete menu and the weekly opening hours
The three row groups: thead, tbody and tfoot
Think of an invoice or a printed bank statement running to several pages: at the top, the column titles; in the middle, the transactions; at the bottom, the total. HTML has an element for each of those areas:
| Element | What the name stands for | Contains | How many per table? |
|---|---|---|---|
thead |
table head | The column header rows | At most one |
tbody |
table body | The data rows | One or more |
tfoot |
table foot | Summary rows: totals, notes, clarifications | At most one |
All three are row groups: they contain tr elements, never loose cells.
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Espresso</td>
<td>€1.40</td>
</tr>
<tr>
<td>Cortado</td>
<td>€1.50</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Prices include VAT</td>
<td></td>
</tr>
</tfoot>
</table>graph TD
T["table"] --> H["thead"]
T --> B["tbody"]
T --> F["tfoot"]
H --> HR["tr (column titles)"]
B --> BR1["tr (data)"]
B --> BR2["tr (data)"]
F --> FR["tr (note or total)"]
An important rule: if you use any of these groups, all the rows must be inside a group. Don't mix loose rows with a thead.
thead: the table head
thead groups the row (or rows) containing the column titles. Usually it's a single row of th elements:
- It goes before any
tbody. - It can have more than one row (for example, a row of column groups above another row with the individual columns; you will do this in the next lesson with cells that span several columns).
- Its cells don't all have to be
th, but in practice they almost always are.
What changes visually? Nothing: the browser gives thead no special appearance. Its value lies in what it communicates. For example, when you print a long table that runs over several sheets, browsers repeat the thead at the top of each page, so you don't lose the column titles.
tbody: the body, and why there can be several
tbody groups the data rows. If you don't write it, the browser creates it, as you saw in DevTools; but as soon as you use thead or tfoot, it's best always to write it explicitly so that the structure is clear.
What's interesting is that a table can have several tbody elements. Each one is a block of related rows, like the chapters of a book. This is how you get back the grouping the nested coffee list had:
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<th>Black coffees</th>
</tr>
<tr>
<td>Espresso</td>
<td>€1.40</td>
</tr>
<tr>
<td>Americano</td>
<td>€1.60</td>
</tr>
</tbody>
<tbody>
<tr>
<th>Milk coffees</th>
</tr>
<tr>
<td>Cortado</td>
<td>€1.50</td>
</tr>
<tr>
<td>Cappuccino</td>
<td>€2.20</td>
</tr>
</tbody>
</table>- The two
tbodyelements share the same head: the columns are the same for the whole table. - The first row of each group carries the category name in a
th. - Each
tbodycan have its ownidorclass: anidlets you link directly to that group (as you did with the sections in Special Links: Anchors, Email, Phone and Downloads), and a class will let you style it.
You will have noticed that the category row has only one cell, even though the table has two columns. The browser accepts this and leaves the rest of the row empty, but it isn't ideal: the HTML validator warns that the row is shorter than the others, and visually the title is boxed into the first column. The right thing is for that cell to span the full width of the table, and that's done with the colspan attribute, the star of the next lesson. Leave it as it is for now.
Several tbody elements or several tables?
| Situation | Best option |
|---|---|
| The groups share the same columns and it makes sense to compare them | One table with several tbody elements |
| The groups have different columns (for example, coffees with a size and breads with a weight and type of flour) | Separate tables |
| The groups are different topics that the user looks up separately | Separate tables, each under its own h2/h3 heading |
tfoot: the table foot, in the code and on screen
tfoot groups the rows that summarise or clarify the table: totals, averages, footnotes, the source of the data.
Where to write it
There's a historical detail here worth knowing:
- In HTML 4,
tfoothad to be written before thetbody, straight afterthead. The idea was that the browser would know about the foot before receiving thousands of rows of data. - The first versions of HTML5 allowed both positions: before the
tbodyor after all thetbodyelements. - The current standard (the HTML Living Standard you saw in What Is HTML?) only allows the second:
tfootgoes at the end, after all thetbodyelements, which is also the natural reading order.
Where it's displayed
Wherever you write it, the browser always displays tfoot at the bottom of the table:
<!-- Old order (HTML 4): tfoot before the tbody -->
<table>
<thead>…</thead>
<tfoot>…</tfoot>
<tbody>…</tbody>
</table>
<!-- Recommended order (HTML5): tfoot at the end -->
<table>
<thead>…</thead>
<tbody>…</tbody>
<tfoot>…</tfoot>
</table>Both tables look exactly the same: head, body and foot, in that order, because browsers still understand the old order. If you come across it in legacy code, the table will work, but the HTML validator will flag it as an error: in new code, always write tfoot at the end.
thead |
tbody |
tfoot |
|
|---|---|---|---|
| Position in the code | First | After thead |
At the end (before tbody only in old code) |
| Position on screen | Top | Middle | Always at the bottom |
| When printing long tables | Repeated on every page | Split across pages | Usually repeated on every page |
th in rows and in columns
So far you have used th only in the head, as the title of each column. But a th can also be the title of a row. On the menu, the product name isn't just another piece of data: it's what identifies the whole row. When you read "€1.50", what you need to know is "for which product?" (the row header) and "what is this figure?" (the column header).
<tbody>
<tr>
<th>Espresso</th>
<td>30 ml</td>
<td>€1.40</td>
</tr>
<tr>
<th>Cortado</th>
<td>90 ml</td>
<td>€1.50</td>
</tr>
</tbody>This way, each data cell has two headers: one above it (its column) and one to its left (its row). Visually, the product name is now displayed in bold and centred; you will adjust this with CSS in Styling Tables if you prefer a different look.
A typical example of a table with headers in both directions is a timetable: the days are column titles and the services are row titles. The top left corner cell, where they meet, can be a th with a title ("Service") or be left empty.
<thead>
<tr>
<th>Service</th>
<th>Monday</th>
<th>Tuesday</th>
</tr>
</thead>
<tbody>
<tr>
<th>Counter</th>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
</tr>
</tbody>How does a browser or a screen reader know whether a th is a row header or a column header? In simple tables it works it out from the position, but the reliable way to state it is the scope attribute, which you will see in Accessible Tables: caption, scope and headers. For now, concentrate on the structure.
Describing columns: colgroup and col
As you saw in the previous lesson, HTML describes tables row by row and there's no column element that contains cells. But sometimes you need to refer to a whole column: to give it a width or a background colour, or simply to indicate that several columns form a group. That's what colgroup and col are for.
colgroup(column group) is a group of columns.col(column) represents one column, or several consecutive ones with thespanattribute. It's a void element, with no closing tag.
They are written straight after the opening table tag (and after an optional table title, which you will see in Accessible Tables), before thead:
<table>
<colgroup>
<col class="col-product">
<col span="2" class="col-details">
<col class="col-price">
</colgroup>
<thead>
<tr>
<th>Product</th>
<th>Size</th>
<th>Allergens</th>
<th>Price</th>
</tr>
</thead>
…
</table>Line by line:
<colgroup>opens the column description.- The first
colcorresponds to column 1 (Product). <col span="2">represents two columns in a row: column 2 (Size) and column 3 (Allergens).- The last
colis column 4 (Price). - The total number of columns described (1 + 2 + 1) matches the table's four columns.
You can also use several colgroup elements with their own span and no col inside, to form groups of columns:
<colgroup></colgroup> <!-- column 1: the service name -->
<colgroup span="5"></colgroup> <!-- columns 2 to 6: Monday to Friday -->
<colgroup span="2"></colgroup> <!-- columns 7 and 8: Saturday and Sunday -->An empty colgroup with no span counts as one column.
What changes on screen? Nothing. colgroup and col display no content: they are structural hooks. Their usefulness will become clear in two lessons in this module:
- In Accessible Tables, column groups will allow a header such as "Weekend" to be associated with the columns it groups.
- In Styling Tables, you will use
colto give a whole column a background or a width without touching each cell (and you will discover which CSS properties it supports and which it doesn't).
What all this structure is for
If thead, tbody, tfoot, colgroup and col barely change how the table looks, why bother? Because structure is information:
- Screen readers: they tell headers apart from data and can announce, for each cell, which row and column it belongs to.
- Printing: the head (and often the foot) are repeated on every page.
- CSS: you can style the head, each body group or the foot with simple selectors (
thead th,tfoot td) instead of filling the code with classes. - JavaScript: a script that sorts the table by price can reorder just the rows in the
tbody, without moving the head or the foot. - Maintenance: anyone opening the code can see at a glance where the titles, the data and the notes are.
Project: the complete menu and the weekly opening hours
- A single table for the whole menu
Right now menu.html has two sections with prices: div#specialities (a list or, if you did Exercise 3 in the previous lesson, a two-column table) and div#coffees (the coffee table). Both share the same characteristics, so you are going to bring them together in a single table with four columns: Product, Size, Allergens and Price.
Replace both div elements (#specialities and #coffees) with this single block, and delete the paragraph <p><small>Prices include <abbr title="Value Added Tax">VAT</abbr>.</small></p> at the top of the page, because that note moves to the table foot:
<div class="menu-section" id="prices">
<h2>Price list</h2>
<table>
<colgroup>
<col class="col-product">
<col span="2" class="col-details">
<col class="col-price">
</colgroup>
<thead>
<tr>
<th>Product</th>
<th>Size</th>
<th>Allergens</th>
<th>Price</th>
</tr>
</thead>
<tbody id="specialities">
<tr>
<th>House specialities</th>
</tr>
<tr>
<th>Autumn seasonal coffee</th>
<td>200 ml</td>
<td>None</td>
<td>€2.50</td>
</tr>
<tr>
<th>Butter croissant</th>
<td>1 piece</td>
<td>Gluten, milk, egg</td>
<td>€2.40</td>
</tr>
<tr>
<th>Lemon sponge cake</th>
<td>1 slice</td>
<td>Gluten, milk, egg</td>
<td>€2.20</td>
</tr>
<tr>
<th>Sourdough bread</th>
<td>500 g loaf</td>
<td>Gluten</td>
<td>€3.00</td>
</tr>
</tbody>
<tbody id="coffees">
<tr>
<th>Black coffees</th>
</tr>
<tr>
<th>Espresso</th>
<td>30 ml</td>
<td>None</td>
<td>€1.40</td>
</tr>
<tr>
<th><i lang="it">Ristretto</i></th>
<td>20 ml</td>
<td>None</td>
<td>€1.40</td>
</tr>
<tr>
<th>Americano</th>
<td>200 ml</td>
<td>None</td>
<td>€1.60</td>
</tr>
</tbody>
<tbody id="milk-coffees">
<tr>
<th>Milk coffees</th>
</tr>
<tr>
<th>Cortado</th>
<td>90 ml</td>
<td>Milk</td>
<td>€1.50</td>
</tr>
<tr>
<th>White coffee</th>
<td>200 ml</td>
<td>Milk</td>
<td>€1.80</td>
</tr>
<tr>
<th>Cappuccino</th>
<td>180 ml</td>
<td>Milk</td>
<td>€2.20</td>
</tr>
<tr>
<th>Flat white</th>
<td>160 ml</td>
<td>Milk</td>
<td>€2.40</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Prices include <abbr title="Value Added Tax">VAT</abbr>. All our coffees can be made with plant-based milk at no extra charge. If you have any allergies or intolerances, please ask our staff.</td>
</tr>
</tfoot>
</table>
</div>Look at the decisions:
colgroupdescribes the four columns: Product, a group of two detail columns and Price. It changes nothing on screen; you will use it in Styling Tables to highlight the price column.theadcontains the single row of column titles.- Three
tbodyelements, one per category, each with its title row. You have got back the distinction between black and milk coffees that was lost in the previous lesson. - The group
ids keep the existing links working:#specialities(the menu's page index and the gallery link here) and#coffees(the menu's page index and the home page, withmenu.html#coffees) now take you to the corresponding group in the table. Thediv'sidchanges toprices, which describes the whole section. - Each product name is a
th: it's the header of its row. Names in another language still usei lang="…". - No cell is empty: products without allergens say "None", because an empty cell in the allergens column would be dangerously ambiguous.
tfootholds the VAT note (with itsabbr, as it was on the page) and adds the allergen information.- The category rows and the foot row have a single cell in a four-column table. You will fix this in the next lesson with
colspan.
Also update the page index (ul.page-index): the links to #specialities and #coffees still work, so you just need to check that the link texts ("House specialities", "Coffees") match the titles of the groups.
- The weekly opening hours in
index.html
index.htmlIn the home page's div#opening-hours section, after the paragraph "We are open every day from 8:00 to 20:00", add a table with the opening hours for each service. The services are the ones you already know from the exercise in Block Quotes and Preformatted Text, where you tried to line them up with pre, plus the new lunch menu:
<h3>Weekly opening hours by service</h3>
<table>
<colgroup></colgroup>
<colgroup span="5"></colgroup>
<colgroup span="2"></colgroup>
<thead>
<tr>
<th>Service</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<tr>
<th>Counter</th>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
<td>8:00–20:00</td>
</tr>
<tr>
<th>Bread sales</th>
<td>8:00–14:00</td>
<td>8:00–14:00</td>
<td>8:00–14:00</td>
<td>8:00–14:00</td>
<td>8:00–14:00</td>
<td>8:00–13:00</td>
<td>8:00–13:00</td>
</tr>
<tr>
<th>Lunch menu</th>
<td>13:00–16:00</td>
<td>13:00–16:00</td>
<td>13:00–16:00</td>
<td>13:00–16:00</td>
<td>13:00–16:00</td>
<td>No service</td>
<td>No service</td>
</tr>
<tr>
<th>Bread-making workshop</th>
<td>No service</td>
<td>No service</td>
<td>No service</td>
<td>No service</td>
<td>No service</td>
<td>10:00–13:00</td>
<td>No service</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>The bread-making workshop is held on the first Saturday of every month. Places are limited: <a href="bookings.html">book your place</a>.</td>
</tr>
</tfoot>
</table>Look at the decisions:
- The
h3sits under the section's "Opening hours and location"h2: the heading hierarchy is still correct. - The table has headers in both directions: days in the
thead(columns) and services as athat the start of each row. The corner is titled "Service". - The three
colgroupelements split the eight columns into three groups: the services column, the weekdays (span="5") and the weekend (span="2"). They aren't visible yet; they will be the basis for the "Monday to Friday / Weekend" header in the next lesson. - Where there's no service, the cell says so instead of being left empty.
- The times use an en dash "–" between start and finish, the same in every cell: consistency makes the table easier to read.
- The foot explains the workshop exception and links to the bookings page. Inside a cell you can put links just as in any paragraph.
Save and open index.html. You will see a wide, very repetitive table: "8:00–20:00" seven times, "No service" five times in a row… That repetition is exactly what you will solve in the next lesson by merging cells.
- Checking the structure
With Live Server and DevTools:
- In
menu.html, expand the table in the Elements panel: you will seecolgroup,thead, threetbodyelements andtfoot. This time the browser hasn't had to invent anything. - From
index.html, click the link tomenu.html#coffees: the page should scroll to the black coffees group. - Open the browser's print preview (
Ctrl+PorCmd+P) withmenu.html. If the table runs to more than one page, check that the head is repeated.
Common Mistakes and Tips
- Putting cells directly inside
thead,tbodyortfoot: a group contains onlytrrows, and the cells go inside those. - Mixing loose rows with groups: if you use
thead, put the data rows in atbodytoo. - Writing two
theador twotfootelements: there can only be one of each. The ones that can be repeated are thetbodyelements. - Writing
tfootbefore thetbody: that was the HTML 4 order and browsers still display it correctly, but the current standard requires it at the end. - Using
thfor only some row headers: if the product name is a row header, it must be one in every row. - Using
tfootfor any last row: the foot is for summaries, totals or notes, not for the last product in the list. - Having the column count of
col/colgroupnot match the table's real columns: count thespanvalues. - Expecting
colgroupto change anything on screen: without CSS, nothing is visible. - Tip: when you change a section's
id, search the whole project for the links that pointed to it. In VS Code,Ctrl+Shift+F(Cmd+Shift+Fon macOS) searches across all files.
Exercises
Exercise 1. Reorder this jumbled code so that it follows the structure recommended in HTML5, and say whether it would look different in the browser before and after reordering:
<table>
<tfoot>
<tr><td>Prices include VAT</td></tr>
</tfoot>
<tbody>
<tr><th>Espresso</th><td>€1.40</td></tr>
</tbody>
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr><th>Product</th><th>Price</th></tr>
</thead>
</table>Exercise 2. The Blue Cup is going to sell coffee beans to take home. Create a table with thead, two tbody elements and tfoot, with the columns "Coffee", "Pack" and "Price": a "Single origins" group (Colombia, 250 g bag, €7.50; Ethiopia, 250 g bag, €8.20) and a "House blends" group (Blue Cup Blend, 250 g bag, €6.90; Decaf Blend, 250 g bag, €7.10). Use the coffee name as the row header. In the foot, state: "We grind your coffee for free while you wait". Add a colgroup in which the "Price" column has the class col-price.
Exercise 3. In the weekly opening hours, how many cells does each row of the tbody have? And the tfoot row? What would happen if you added a "Bank holidays" column at the end? List all the changes needed, including the colgroup.
Solutions
Solution 1.
<table>
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr><th>Product</th><th>Price</th></tr>
</thead>
<tbody>
<tr><th>Espresso</th><td>€1.40</td></tr>
</tbody>
<tfoot>
<tr><td>Prices include VAT</td></tr>
</tfoot>
</table>The correct order is colgroup, thead, tbody, tfoot. In the original code, the tfoot before the tbody (the HTML 4 order) would still have been displayed at the bottom, although the current standard no longer allows it. More serious is placing colgroup and thead after the body: that has never been valid in any version of HTML. Would it look different? Probably not: the browser always displays thead at the top and tfoot at the bottom, whatever their position in the code. That's precisely why these mistakes go unnoticed, and why it's worth checking the order (and validating the code, as you will learn in Validating HTML Code) rather than trusting what you see.
Solution 2.
<table>
<colgroup>
<col>
<col>
<col class="col-price">
</colgroup>
<thead>
<tr>
<th>Coffee</th>
<th>Pack</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<th>Single origins</th>
</tr>
<tr>
<th>Colombia</th>
<td>250 g bag</td>
<td>€7.50</td>
</tr>
<tr>
<th>Ethiopia</th>
<td>250 g bag</td>
<td>€8.20</td>
</tr>
</tbody>
<tbody>
<tr>
<th>House blends</th>
</tr>
<tr>
<th>Blue Cup Blend</th>
<td>250 g bag</td>
<td>€6.90</td>
</tr>
<tr>
<th>Decaf Blend</th>
<td>250 g bag</td>
<td>€7.10</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>We grind your coffee for free while you wait.</td>
</tr>
</tfoot>
</table>Another valid option for the colgroup is <col span="2"> followed by <col class="col-price">. As in the project, the group rows and the foot row have a single cell; in the next lesson you will stretch them across the full width.
Solution 3. Each row of the tbody has 8 cells: a th with the service and seven td elements, one per day. The tfoot row has a single cell (for now). To add a "Bank holidays" column:
- Add
<th>Bank holidays</th>at the end of thetheadrow. - Add a
tdat the end of each of the four rows in thetbody. - Adjust the
colgroup: for example, add a fourth<colgroup></colgroup>(bank holidays are neither weekdays nor the weekend) or change the last one tospan="3"if you decide to group them with the weekend.
Conclusion
Your tables now have a complete structure: thead for the column titles, one or more tbody elements to group the data into categories, and tfoot for notes and totals, which in HTML5 is written at the end and is always displayed at the bottom. You know how to use th as both a column header and a row header, and how to describe columns and column groups with colgroup and col, even though they aren't visible yet. The Blue Cup's menu is now a single table with specialities, black coffees and milk coffees, sizes, allergens and a footnote, and the home page shows the weekly opening hours for each service. But two things jar: the category rows and the foot row have a single cell in a table with several columns, and the opening hours repeat the same data over and over again. In the next lesson, Merging Cells: colspan and rowspan, you will learn to merge cells to solve both problems and you will create the lunch menu with a shared price.
HTML Course
Module 1: Introduction to HTML
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
