HTML Tables
Present tabular data with semantic, accessible HTML tables
Introduction to HTML Tables
What are HTML Tables?
HTML tables display data in rows and columns. They’re ideal for tabular information like schedules, price lists, and comparison charts.
Tables are built using a combination of elements: <table>, <tr>, <th>, and <td>, often organized into <thead>, <tbody>, and <tfoot>. When used correctly, tables make it easy for users and assistive technologies to scan and compare data.
Basic Table Structure
Example: Simple Table
| Language | Type | Usage |
|---|---|---|
| HTML | Markup | Structure of web pages |
| CSS | Style Sheet | Presentation and layout |
| JavaScript | Programming | Interactivity and logic |
HTML Code:
<table>
<thead>
<tr>
<th>Language</th>
<th>Type</th>
<th>Usage</th>
</tr>
</thead>
<tbody>
<tr>
<td>HTML</td>
<td>Markup</td>
<td>Structure of web pages</td>
</tr>
</tbody>
</table>
Semantic Table Elements
Use semantic table elements to make your data more accessible and easier to understand. These elements help browsers, screen readers, and search engines understand which cells are headers, which cells contain data, and how rows and columns relate to each other.
<caption>– brief description of the table<thead>,<tbody>,<tfoot>– group rows logically<th>– header cells (with optionalscopeattribute)<colgroup>and<col>– style or identify full columns
Example: Full Semantic Table
| Team | Tasks Completed | Pending Tasks | Status |
|---|---|---|---|
| Frontend | 42 | 6 | On Track |
| Backend | 39 | 9 | Needs Review |
| Total | 81 | 15 | Overall Good |
HTML Code:
<table>
<caption>Monthly Team Performance Report</caption>
<colgroup>
<col style="background-color: #f2f8ff;">
<col span="2">
<col style="background-color: #f2fff7;">
</colgroup>
<thead>...</thead>
<tbody>...</tbody>
<tfoot>...</tfoot>
</table>
Rowspan and Colspan
rowspan merges cells vertically (across rows), and colspan merges cells horizontally (across columns). These attributes are useful for grouped headings, schedules, and report-style tables.
Example 1: Using colspan for grouped headers
| Student | Marks | Result | ||
|---|---|---|---|---|
| Math | Science | English | ||
| Asha | 88 | 91 | 84 | Pass |
| Rahul | 75 | 80 | 78 | Pass |
Example 2: Using rowspan for timetable grouping
| Day | Time | Subject |
|---|---|---|
| Monday | 10:00 - 11:00 | HTML |
| 11:15 - 12:15 | CSS | |
| Tuesday | 10:00 - 11:00 | JavaScript |
| 11:15 - 12:15 | Bootstrap |
HTML Code:
<tr>
<th rowspan="2">Monday</th>
<td>10:00 - 11:00</td>
<td>HTML</td>
</tr>
<tr>
<td>11:15 - 12:15</td>
<td>CSS</td>
</tr>
<tr>
<th>Student</th>
<th colspan="3">Marks</th>
</tr>
Responsive Tables
On small screens, wide tables can become difficult to read. A simple approach for responsiveness is to wrap your table in a container that allows horizontal scrolling. Bootstrap’s .table-responsive class does this for you.
Example: Responsive Table Container
| Quarter | Revenue | Expenses | Profit |
|---|---|---|---|
| Q1 | $50,000 | $30,000 | $20,000 |
| Q2 | $65,000 | $38,000 | $27,000 |
HTML Code:
<div class="table-responsive">
<table class="table">
...
</table>
</div>
Accessible Tables
For accessibility, always relate header cells and data cells clearly.
- Use
<th scope="col">for column headers. - Use
<th scope="row">for row headers when appropriate. - Provide a
<caption>for context.
Best Practices
- Use tables only for tabular data, not for page layout.
- Keep tables simple; avoid overly complex nesting.
- Use CSS for styling (borders, colors, spacing).
- Ensure tables are readable on smaller screens (consider responsive patterns).