HTML Tables
> Display structured data using table, caption, thead, tbody, tr, th, td, and accessible header relationships.
**Cyber Code Academy · HTML Tutorial · Lesson 11 · Beginner**
## 1. What is an HTML table?
An HTML table displays data arranged into rows and columns. Use it for information that has a genuine row-and-column relationship, such as schedules, marksheets, price comparisons, and reports.
Do not use tables to position page headers, sidebars, or other general layout sections. Modern CSS layout tools are designed for that purpose.
## 2. Basic table structure
“`html
| Course | Duration |
|---|---|
| HTML | 2 weeks |
| CSS | 3 weeks |
“`
– table creates the table.
– tr creates a table row.
– th creates a header cell.
– td creates a data cell.
The browser displays data in rows and columns. Without CSS, the browser may show little or no visible border.
## 3. Add a table caption
The caption provides a visible title or description for the table.
“`html
| Course | Duration |
|---|---|
| HTML | 2 weeks |
“`
A caption helps users understand what information the table presents.
## 4. Group table sections
Use thead, tbody, and tfoot to group header, body, and footer rows.
“`html
| Course | Fee |
|---|---|
| HTML | ₹2,000 |
| CSS | ₹3,000 |
| Total | ₹5,000 |
“`
Browsers may insert a tbody element in the DOM when markup contains rows directly inside table. Writing the intended sections explicitly makes the source clearer.
## 5. Header scope
Use scope to identify the direction of a header relationship:
– scope=”col” identifies a column header.
– scope=”row” identifies a row header.
This improves the relationship between headings and data cells for assistive technology.
## 6. Merge cells using colspan
colspan makes a cell span multiple columns:
“`html
| Student Details | |
|---|---|
| Name | Course |
| Alex | HTML |
“`
## 7. Merge cells using rowspan
rowspan makes a cell span multiple rows:
“`html
| Course | Batch |
|---|---|
| HTML | Morning |
| Evening |
“`
Use merged cells only when the data relationship genuinely requires them.
## 8. Add CSS for readability
HTML supplies table structure. CSS controls visual presentation:
“`css
table {
width: 100%;
border-collapse: collapse;
}
caption {
font-weight: 700;
text-align: left;
margin-bottom: 0.75rem;
}
th, td {
border: 1px solid #cbd5e1;
padding: 0.75rem;
text-align: left;
}
thead {
background: #e2e8f0;
}
“`
For smaller screens, consider a responsive wrapper with horizontal scrolling rather than shrinking data until it becomes unreadable.
## 9. Accessible complex tables
For complex tables with multiple levels of headers, simple scope may not be enough. You may need carefully associated header IDs and headers attributes. Keep tables as simple as possible.
## 10. Common mistakes
– Using table for website layout.
– Forgetting header cells.
– Using td for every cell, even column headings.
– Creating tables without a meaningful caption when one would help.
– Merging cells unnecessarily.
– Making a table too wide for small screens without a scrolling strategy.
## 11. Practice exercises
1. Create a student marksheet with roll number, name, HTML marks, CSS marks, and total.
2. Add a caption and thead/tbody sections.
3. Use scope=”col” for column headers.
4. Use a row header for each student name.
5. Create a timetable with colspan for a shared break period.
6. Add CSS borders, padding, and a header background.
## 12. Knowledge check
1. What is the purpose of table, tr, th, and td?
2. Why use caption?
3. What are thead, tbody, and tfoot?
4. What is the difference between colspan and rowspan?
5. How does scope help accessibility?
6. Why should tables not be used for general page layout?
**Next lesson:** HTML Forms Introduction.
Want to learn this professionally?
Explore practical IT training at Cyber Code Academy.
View Academy Courses