An HTML table allows web developers to arrange complex data—such as financial reports, student marks, timetables, and product inventories—into a structured matrix of rows and columns. Tables are defined with the <table> element along with essential building blocks like <tr> (table rows), <th> (header cells), and <td> (data cells).
A table is divided into rows, and each row is divided into data cells. The letters td stand for "table data", which represents the actual content of a cell. A data cell can hold plain text, images, hyperlinks, lists, forms, or even nested tables.
In standard HTML5, well-structured tables use semantic group elements that divide the data into clear logical sections for accessibility and styling:
<caption>: Defines an accessible title or summary placed at the top of the table.<thead>: Groups the header row(s) containing column titles.<tbody>: Contains the main body of data records.<tfoot>: Contains summary rows (such as totals, averages, or footnotes).Below is a reference of the traditional attributes associated with the <table> element. While traditional attributes are frequently asked in university BCA exams, modern industry standard dictates using CSS for presentation.
| Attribute | Traditional Function | Modern CSS Equivalent |
|---|---|---|
border |
Specifies the thickness of the table border in pixels (e.g., border="1"). |
border: 1px solid #ccc; |
cellspacing |
Specifies the pixel space between adjacent cells. | border-spacing: 0; or border-collapse: collapse; |
cellpadding |
Specifies padding space between cell border and cell content. | th, td { padding: 10px; } |
align |
Aligns the table on the page (left, center, right). |
margin: 0 auto; (for centering) |
width |
Sets total table width in pixels or percentages. | width: 100%; or max-width: 800px; |
bgcolor |
Sets background color of the entire table or individual row. | background-color: #f8f9fa; |
The <tr> tag defines a horizontal row. Inside a row, we insert either header cells (<th>) or data cells (<td>).
<th> is automatically bold and centered by default. It signals to screen readers that the cell is a label for that row or column.<td> is regular weight and left-aligned by default.Often in practical university lab assignments and professional invoices, a single cell must stretch across multiple columns or rows:
colspan="n": Merges n adjacent columns horizontally into a single cell.rowspan="n": Merges n stacked rows vertically into a single cell.<table border="1" style="border-collapse: collapse; width: 100%;">
<tr>
<th rowspan="2">Student Name</th>
<th colspan="2">Marks</th>
</tr>
<tr>
<th>Theory</th>
<th>Practical</th>
</tr>
<tr>
<td>Rahul Sharma</td>
<td>85</td>
<td>95</td>
</tr>
</table>
Here is the foundational inventory example demonstrating simple 3x4 table structure:
<table border="2" height="200" width="100%">
<tr>
<th>Item No.</th>
<th>Item Name</th>
<th>Item Qty.</th>
</tr>
<tr>
<td>01</td>
<td>Mouse</td>
<td>10</td>
</tr>
<tr>
<td>02</td>
<td>Keyboard</td>
<td>10</td>
</tr>
<tr>
<td>03</td>
<td>Monitor</td>
<td>5</td>
</tr>
</table>
| Item No. | Item Name | Item Qty. |
|---|---|---|
| 01 | Mouse | 10 |
| 02 | Keyboard | 10 |
| 03 | Monitor | 5 |
Standard tables do not naturally squish on narrow mobile screens. If a table has 6+ columns, it will cause horizontal page overflow on smartphones unless wrapped inside a container with overflow-x: auto;:
<div style="overflow-x: auto;">
<table class="responsive-table">
<!-- Table data here -->
</table>
</div>
Assignment Scenario: Create a semantic, accessible semester examination grade sheet featuring a caption, colspan for subject categories, rowspan for student details, total calculations in <tfoot>, and clean CSS styling.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Semester Grade Sheet Table</title>
<style>
.marksheet-container {
max-width: 750px;
margin: 20px auto;
font-family: Arial, sans-serif;
}
table.grade-table {
width: 100%;
border-collapse: collapse;
text-align: center;
}
table.grade-table caption {
font-size: 18px;
font-weight: bold;
padding: 10px;
color: #333;
}
table.grade-table th, table.grade-table td {
border: 1px solid #bbb;
padding: 10px 12px;
}
table.grade-table thead tr {
background-color: #2c3e50;
color: #ffffff;
}
table.grade-table tbody tr:nth-child(even) {
background-color: #f9f9f9;
}
table.grade-table tfoot {
background-color: #ecf0f1;
font-weight: bold;
}
</style>
</head>
<body>
<div class="marksheet-container">
<table class="grade-table">
<caption>Veewom Institute of Technology - BCA Semester I Result</caption>
<thead>
<tr>
<th rowspan="2" scope="col">Roll No</th>
<th rowspan="2" scope="col">Candidate Name</th>
<th colspan="2" scope="colgroup">Web Programming</th>
<th colspan="2" scope="colgroup">C Language</th>
<th rowspan="2" scope="col">Total</th>
</tr>
<tr>
<th scope="col">Theory</th>
<th scope="col">Lab</th>
<th scope="col">Theory</th>
<th scope="col">Lab</th>
</tr>
</thead>
<tbody>
<tr>
<td>BCA-101</td>
<td>Aarav Patel</td>
<td>88</td>
<td>48</td>
<td>82</td>
<td>45</td>
<td>263</td>
</tr>
<tr>
<td>BCA-102</td>
<td>Sneha Sharma</td>
<td>92</td>
<td>50</td>
<td>90</td>
<td>47</td>
<td>279</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="6" style="text-align: right;">Class Average:</td>
<td>271</td>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
<th> defines a header cell; its text is rendered in bold font and centered by default. Screen readers treat <th> as column or row descriptors. <td> defines regular table data; its text is regular weight and left-aligned by default.
colspan merges two or more horizontal columns into a single data cell (e.g., <td colspan="3"> spans across 3 columns). rowspan merges two or more vertical rows into one cell (e.g., <td rowspan="2"> spans across 2 stacked rows).
cellpadding, cellspacing, and bgcolor mix styling into the markup. They are superseded by modern CSS properties like padding and border-collapse: collapse;.
<thead> and <tfoot> at the top and bottom of each printed page, while scrolling the <tbody> independently.
border-collapse: collapse; to the <table> element in CSS.
tr:nth-child(even) { background-color: #f2f2f2; }. This automatically styles every even row without needing custom classes on each row.
width: 80%;) and apply margin-left: auto; margin-right: auto; (shorthand margin: 0 auto;).
scope attribute explicitly tells screen readers whether a header applies to a column (scope="col") or a row (scope="row"). It is an accessibility best practice that boosts usability and technical SEO.