To create a table in html, use a <table> element, add one <tr> for every row and put cells inside each row: <th> for header cells and <td> for data cells. This html table tutorial builds one step by step, from a minimal grid to an accessible, styled table with a caption and sections.
Use tables for tabular data, meaning information with rows and columns, such as prices, schedules or results. Do not use them for page layout; CSS grid and flexbox handle layout.
<th>A table starts with <table>. Each row is a <tr> (table row), and each cell inside a row is a <td> (table data).
This shows two rows and two columns, with no borders by default. Browsers insert an implied <tbody> around the rows, which is why you may see it in the developer tools even though you did not write it.
<th>Header cells describe the column or row they belong to. Use <th> instead of <td> for them. Browsers display th text bold and centred by default, and assistive technology uses it to announce which header applies to each data cell.
For a header that applies to a column, add scope="col"; for a row header, use scope="row". The attribute removes any doubt for screen readers, especially in larger tables.
A <caption> gives the table a visible title and an accessible name. It must be the first child of <table>. Group rows with <thead>, <tbody> and <tfoot> so that the structure is explicit.
The sections do not change the look by themselves, but they let you style headers separately, repeat headers when printing long tables and give assistive technology a clear map.
| Element | Role |
|---|---|
<table> | The whole table |
<caption> | Title of the table |
<thead> | Header rows |
<tbody> | Main data rows |
<tfoot> | Summary rows |
<tr> | One row |
<th> | Header cell |
<td> | Data cell |
Old tutorials use the border, cellpadding and bgcolor attributes. These are obsolete. Use CSS.
border-collapse: collapse merges the double borders between cells into single lines. The :nth-child(even) rule adds zebra striping, which helps readers follow a row across wide tables.
A table with many columns can overflow a small screen. Wrap it in a scrollable container instead of letting the page scroll sideways.
The tabindex="0" lets keyboard users scroll the region, and the label gives it a name.
<td> for headers and styling them bold. Use <th> so that screen readers know they are headers.<td> outside a <tr>.border, align, width and cellspacing attributes instead of CSS.<th> and <td>?th is a header cell that labels a row or column, while td is a normal data cell. Screen readers use th to announce headers with the data.
<thead>, <tbody> and <tfoot>?No, they are optional, but they make the structure clearer and are useful for styling, printing and accessibility.
Use the colspan attribute on a <td> or <th>, and rowspan to span rows. Merged cells are covered in a related article.
Use CSS: set border on th and td, and border-collapse: collapse on the table.
Build a table from <table>, <tr>, <th> and <td>, then add a <caption>, <thead>, <tbody> and <tfoot> for structure. Use scope on header cells, style with CSS rather than obsolete attributes, and wrap wide tables in a scrollable region. Reserve tables for real tabular data, not layout.