HTML & CSSEasy
What is the <table> structure?
An HTML table is built from <table>, optional <caption>, row groups (<thead>, <tbody>, <tfoot>), rows (<tr>) and cells: <th> for headers and <td> for data. Use tables only for tabular data (rows and columns that relate to each other), never for page layout.
The pieces
<caption>: the table's title; it is the table's accessible name.<thead>/<tbody>/<tfoot>: group rows. Browsers can repeat<thead>on every printed page.<tr>: a row.<th scope="col">/<th scope="row">: header cells;scopetells assistive tech which cells they describe.<td>: data cells.colspanandrowspanmerge cells.<colgroup>/<col>: style whole columns (width, background).
Accessibility
Screen readers announce the matching header as users move between cells, but only if you use <th> and scope. For complex tables with multi-level headers, link cells to headers with headers="id1 id2".
Styling tips
border-collapse: collapsemerges double borders.table-layout: fixedmakes column widths predictable and rendering faster.- Wrap wide tables in a container with
overflow-x: autoso they scroll on mobile instead of breaking the layout. - Zebra striping:
tbody tr:nth-child(even).
Interview tip
Mention that layout tables are an old anti-pattern; today Flexbox and Grid handle layout, and tables are reserved for data.
Code example
html
1<style>2 .wrap { overflow-x: auto; }3 table { border-collapse: collapse; min-width: 420px; font-family: system-ui; }4 caption { font-weight: 700; padding: 8px; text-align: left; }5 th, td { border: 1px solid #ccc; padding: 8px 12px; text-align: left; }6 thead th { background: #0b1020; color: white; }7 tbody tr:nth-child(even) { background: #f3f4f6; }8 tfoot td { font-weight: 700; }9</style>10<div class="wrap">11 <table>12 <caption>Q3 sales by region</caption>13 <thead>14 <tr><th scope="col">Region</th><th scope="col">July</th><th scope="col">August</th></tr>15 </thead>16 <tbody>17 <tr><th scope="row">North</th><td>$12,400</td><td>$13,050</td></tr>18 <tr><th scope="row">South</th><td>$9,800</td><td>$10,200</td></tr>19 <tr><th scope="row">West</th><td colspan="2">Data pending</td></tr>20 </tbody>21 <tfoot>22 <tr><th scope="row">Total</th><td>$22,200</td><td>$23,250</td></tr>23 </tfoot>24 </table>25</div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.