Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › HTML Snippets › Reference › <table>

HTML Reference

<table> — The Table Element

The element for tabular data — rows and columns of information. Not for page layout.

Last updated: October 2026

Definition: The <table> element displays data in rows and columns. It contains <tr> (rows), <th> (header cells), and <td> (data cells). Optional grouping elements — <thead>, <tbody>, <tfoot> — improve accessibility and styling.

🎬 Live Example

A complete table with header, body, and footer.

Language Type Year
HTMLMarkup1993
CSSStyling1996
JavaScriptProgramming1995
PythonProgramming1991

💡 Header cells use <th>. Data cells use <td>. Screen readers use this to announce headers when navigating.

📝

Basic Syntax

<table>
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>Alice</td>
        <td>30</td>
    </tr>
</table>

Every row needs the same number of cells unless you use colspan or rowspan.

⚙️ Attributes Reference

Element Attribute Purpose
<table>borderLegacy. Use CSS border instead.
<th>colspanSpan this header across N columns.
<th>rowspanSpan this header across N rows.
<th>scopecol, row, colgroup, rowgroup. Helps screen readers.
<td>colspanSpan this cell across N columns.
<td>rowspanSpan this cell across N rows.
<td>headersAssociate this cell with specific header IDs.
1

Complete Table with thead, tbody, tfoot

Use the grouping elements for accessibility and styling.

<table>
    <caption>Programming Languages</caption>

    <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Year</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <th scope="row">Python</th>
            <td>1991</td>
        </tr>
        <tr>
            <th scope="row">JavaScript</th>
            <td>1995</td>
        </tr>
    </tbody>

    <tfoot>
        <tr>
            <td colspan="2">Total languages: 2</td>
        </tr>
    </tfoot>
</table>

The <caption> element gives the table a title — critical for accessibility.

2

colspan and rowspan

<!-- Span 2 columns -->
<tr>
    <th colspan="2">Programming Languages</th>
</tr>

<!-- Span 2 rows -->
<tr>
    <td rowspan="2">Web</td>
    <td>HTML</td>
</tr>
<tr>
    <td>CSS</td>
</tr>

Warning: Complex spans are hard to maintain and confusing for screen readers. Use them only when the data structure genuinely requires it.

🎯 When to Use Tables vs Grid

Use case Tool
Data table (prices, specs, comparison)<table>
Page layout (header, sidebar, content)CSS Grid
Card grid, photo galleryCSS Grid
Navigation menuFlexbox + <ul>

💡 Rule: If the data only makes sense in a grid of rows and columns, use <table>. For everything else, use CSS.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the HTML <table> tag?

The <table> element creates tabular data — rows and columns of information. It is for data, not layout. Use CSS Grid or Flexbox for page layout.

What is the difference between th and td?

th is a header cell (bold and centered by default). td is a data cell. Screen readers use th to announce the column header when reading a cell.

What is the difference between thead, tbody, and tfoot?

thead contains header rows. tbody contains the main data rows. tfoot contains footer rows (totals, summaries). They help screen readers and allow styling the header to stay fixed while scrolling.

What do colspan and rowspan do?

colspan makes a cell span multiple columns. rowspan makes a cell span multiple rows. Use them sparingly — complex spans are hard to maintain and can confuse screen readers.

Should I use tables for layout?

No. Tables are for tabular data. Using them for page layout breaks accessibility, hurts responsiveness, and is considered bad practice since the 2000s. Use CSS Grid or Flexbox.

🎯 What's Next?

← All HTML Reference