Tables: organise related data into rows and columns
A timetable is easier to compare when each room appears beside its session. Build your first data tables, explain every cell's role, and give headers and captions real meaning rather than using tables as page layout.
Imagine a notice with three repeated facts: session name, room and starting time. A paragraph for each session works, but readers must keep searching for each time. A table arranges related data so the same kind of value appears in the same column.
A row runs across. A column runs down. A cell is one place where a value or header is held. Choose a table when those two-direction relationships help readers compare the data.
Do not put a page's sidebar, main article and footer into table cells just to position them. Learn CSS for layout. A list or description list is often clearer for a short collection of facts without a useful row-and-column relationship.
The four starting elements
Element
Job
Example
table
Contains the data table
The whole session timetable
tr
Creates a row
One session's facts
th
Identifies a header cell
Room
td
Identifies an ordinary data cell
Room 6
A row contains cells, not just unwrapped values. The position of a cell within each row creates a column relationship. HTML does not require a separate column element for this basic structure.
The first row labels the columns. The second row has two corresponding values. Scope="col" explicitly says the header applies to its column. It belongs on th, not on td. We will examine more complex header relationships and merged cells in the next tables lessons.
Build meaning across and down
1Caption: name the whole table
2Tr: one row
3Th: label a column or row
4Td: place a value under the correct labels
A missing cell can shift meanings. Read each row against the headers.
Caption: name the table itself
A caption supplies a visible title for a table. Put it as the table's first element child, before its rows or row groups. It is not a substitute for column headers: the caption names the whole table, while each header names a relationship within it.
Avoid vague captions such as Table when a useful one can say Friday practice sessions. A caption helps someone understand which table they have reached, including when navigating through several tables with assistive tools.
Complete program: session-table.html
session-table.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Friday Practice Timetable</title></head><body><h1>Choose a Friday practice session</h1><p>All times below are India Standard Time.</p><table><caption>Friday practice sessions</caption><tr><thscope="col">Session</th><thscope="col">Room</th><thscope="col">Start</th></tr><tr><td>HTML notice</td><td>Room 6</td><td>10 am</td></tr><tr><td>Link practice</td><td>Room 8</td><td>11 am</td></tr></table></body></html>
Browser previewDocument title: Friday Practice Timetable
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: a caption, a header row and two session rows appear in a three-column table. In an unstyled browser view, there may be no visible grid lines. The table is still present. Borders, spacing and alignment are styling decisions; adding a table does not automatically promise a spreadsheet-like grid.
Read the Link practice row across: its room is Room 8 and start is 11 am. Then read the Room column down: its entries belong to the sessions in those rows. This is the relationship the table describes.
Header cells are not just bold text
Browsers often render th in bold and centre it. That appearance is not the reason to choose th. It identifies a header that labels other cells. A bold word inside td does not create that same relationship.
A row header names the data across its own row. Use th scope="row" when that is the intended role. A table can contain both row and column headers. Keep the header text short and specific.
Complete program: practice-progress.html
practice-progress.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Weekly HTML Practice Progress</title></head><body><h1>Check the weekly practice plan</h1><table><caption>Practice topics and completed examples</caption><tr><thscope="col">Topic</th><thscope="col">Planned examples</th><thscope="col">Completed examples</th></tr><tr><thscope="row">Lists</th><td>4</td><td>3</td></tr><tr><thscope="row">Links</th><td>3</td><td>3</td></tr></table><p>One planned lists example remains. These are fictional practice counts.</p></body></html>
Browser previewDocument title: Weekly HTML Practice Progress
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: Topic, Planned examples and Completed examples are column headers. Lists and Links are row headers. A value such as 3 gains meaning from both its row and column: three completed lists examples is different from three planned links examples.
What browsers may add
When source places tr directly inside table, the HTML parser normally creates a tbody group in the document tree. That is why browser developer tools may show tbody even though you did not type it. It does not mean the browser changed your saved source file.
Later we will write thead, tbody and tfoot ourselves to organise larger tables. Tfoot is the real element name; do not invent tfooter. Write explicit row and cell closing tags while learning, even where the HTML syntax permits some omissions.
Keep the grid understandable
For these simple tables, keep the same number of cell positions in each row. A missing middle td does not politely preserve its intended blank slot; later values can appear under the wrong header. An intentionally empty td reserves a cell, but consider whether clear text such as Not recorded communicates the state better.
Cells can contain suitable text, links, lists and other content, but put that content inside the cell. Do not insert a paragraph directly between rows and expect it to remain there: table parsing has special error-recovery rules. Put explanations before or after the table, or within the appropriate cell.
Styling and small screens
Use CSS for borders, cell padding and alignment. Avoid copying old border, cellpadding, cellspacing or bgcolor attributes into new teaching examples. We are keeping these first programs unstyled so that the table structure is easy to recognise.
Long tables can exceed a phone's width. Good later styling can provide an appropriate scroll container or a more suitable presentation. Do not make the text unreadably tiny just to fit everything. Keep real headers in the document and avoid replacing them only with visual decoration.
Problem and solution: a time appears under Room
Problem: a session row omits its room cell, so the time becomes the second cell and appears under Room.
Solution: restore the missing value or an explicit cell saying Room not assigned. Read every row against the headers. A browser rendering without an error message does not prove that your data relationships are correct.
Common mistakes and useful tips
Using a table to arrange the whole page visually.
Writing bold td text instead of a real th header.
Putting caption outside table.
Counting a missing cell as an automatic blank place.
Writing td directly inside table without a row.
Expecting default borders or sorting from HTML alone.
Assuming a table calculates totals or filters data automatically. Those features require additional behaviour.
Practice questions
Explain row, column and cell using the session timetable.
Which element identifies a header cell?
How do scope="col" and scope="row" differ?
What does a caption name, and where does it go?
Does a basic HTML table automatically draw grid lines and sort its rows?
Build a three-column table for three fictional sessions. Add a useful caption and column headers, then deliberately remove a room cell. Explain the incorrect alignment and repair it.
Check your practice answers
A row holds one session's facts across. A column groups the same kind of fact down the table. A cell holds one header or value at their intersection.
Th; td holds ordinary data.
Col identifies a column header. Row identifies a header for the row's data.
It names the whole table and belongs inside table as its first element child.
No. Borders need styling, and sorting needs additional behaviour.
Keep three corresponding cells per simple row. After removing a middle cell, later data can sit under the wrong header. Restore that cell with a valid value or a clear unassigned-room message.
Interview questions
When is table the right HTML element?
When the content is data with meaningful row and column relationships. It should not be used merely to position unrelated page sections.
What is the difference between th and td?
Th identifies a header that labels related cells. Td identifies an ordinary data cell. Their meaning matters even when CSS makes them look similar.
Why can tbody appear in developer tools without being in the source?
The HTML parser normally inserts that row-group element when it processes rows written directly inside table. The parsed document tree can therefore include structure not explicitly typed in the file.
Does correct-looking output prove the table is accessible?
No. Check meaningful headers, useful captions, correct data associations and keyboard or assistive-tool behaviour where relevant. Visual alignment alone is not a complete check.
Quick revision
Table contains rows; rows contain header and data cells.
Caption names the table; th labels related cells.
Keep the grid and its meanings consistent.
Separate data structure from borders, layout and interactive behaviour.