HTML Intermediate: Structure, Tables and Forms
HTML Tables
In this lesson you will
- Build a table with rows, headings and cells
- Add a caption and group rows with thead and tbody
- Merge cells with colspan and rowspan
Introduction
Timetables, price lists, sports scores: some information is naturally a grid. HTML tables are made for exactly that. They are not made for laying out a web page. For layout we use CSS, which you will learn later.
The basic table
A table is built from rows. Each row holds cells.
tablewraps the whole thing.tris a table row.this a header cell (bold and centred by default).tdis a normal data cell.
The border and cellpadding attributes are only here so you can see the grid. In real projects you style tables with CSS.
Quick quiz
Which element makes a header cell?
Caption and groups
A well-built table has a title and clear sections:
captiongives the table a title and must be the first thing insidetable.theadgroups the header row,tbodythe main rows andtfootthe summary rows.- On
th,scope="col"orscope="row"tells assistive technology which cells the header describes.
Merging cells
colspan makes a cell stretch across columns. rowspan makes it stretch down rows.
Quick quiz
What does the colspan attribute do?
When not to use a table
Only use a table for real tabular data: information that makes sense in rows and columns. Do not use it just to put things side by side. That is a job for CSS.
Try it yourself
Add a third row to this timetable, then add a caption as the first child of the table.
Summary
table,tr,thandtdbuild a table.- Add
caption,thead,tbodyandtfootfor a clear structure. colspanandrowspanmerge cells.- Use tables for data, not for page layout.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading