👀 Live Preview
Simple two-column data table:
| Name | Age |
|---|---|
| John Doe | 25 |
| Jane Smith | 30 |

The <table> tag structures tabular data into rows and columns. This guide covers syntax, tr/th/td, CSS styling, accessibility, tables vs layout, and best practices for beginners.
Rows, columns, and cells.
Core table structure.
Borders, spacing, width.
Present structured information.
Tabular data only.
Headers and captions.
<table> Tag?The <table> tag is an HTML element for creating tables that organize and display data in rows and columns. It is the root container for tabular content on a web page.
Use table for data that belongs in a grid (names, prices, dates). Do not use tables for page layout—use CSS Grid or Flexbox instead.
A table contains rows (tr), header cells (th), and data cells (td). For larger tables, group rows with thead, tbody, and tfoot.
Create a basic table with table, tr, th, and td:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table><table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John Doe</td>
<td>25</td>
</tr>
</table>tr and contains one or more th or td cells.th for column or row headings; use td for data values.border or cellpadding.caption as the first child when the table needs a visible title.| Topic | Code Snippet | Notes |
|---|---|---|
| Table root | <table>...</table> | Container |
| Row | <tr>...</tr> | Table row |
| Header cell | <th>Name</th> | Column label |
| Data cell | <td>25</td> | Cell value |
| CSS borders | border-collapse: collapse | Clean borders |
| Purpose | Tabular data | Not page layout |
Understanding how table elements fit together is essential for readable, accessible markup:
| Element | Name | Purpose |
|---|---|---|
<table> | Table | Root container for the entire table |
<caption> | Caption | Visible title (first child of table) |
<thead> | Table Head | Group of header rows |
<tbody> | Table Body | Group of main data rows |
<tr> | Table Row | One horizontal row of cells |
<th> | Table Header | Header cell for a column or row |
<td> | Table Data | Standard data cell |
A minimal table needs only table, tr, th, and td. As tables grow, add thead and tbody to separate headers from body content.
| Approach | Use for | Avoid for |
|---|---|---|
<table> | Spreadsheets, pricing, schedules | Page columns, nav bars, grids |
| CSS Grid / Flexbox | Page layout, cards, responsive UI | Semantic tabular data labels |
| Accessibility | Tables expose row/column structure | Layout tables confuse screen readers |
The <table> tag has no required attributes. Prefer CSS over obsolete presentational attributes.
<table class="data-table">
<!-- Table content here -->
</table>class / id GlobalHook for CSS to style borders, width, and spacing.
class="data-table"border ObsoleteDeprecated in HTML5. Use border in CSS on th and td instead.
/* CSS */ border: 1px solid #dddcellpadding / cellspacing ObsoleteDeprecated. Use CSS padding on cells and border-collapse on the table.
padding: 8pxwidth CSS preferredHTML width attribute is obsolete. Set width in CSS for responsive control.
width: 100%Customize table appearance with CSS applied to table, tr, th, and td:
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background: #f1f5f9;
}
</style>border-collapse: collapse merges adjacent cell borders into a single line.padding on th and td controls inner spacing (replaces cellpadding).width: 100% lets the table fill its container on responsive layouts.Basic structure, CSS styling, and comparative data tables with copy-ready code and live previews.
Simple two-column data table:
| Name | Age |
|---|---|
| John Doe | 25 |
| Jane Smith | 30 |
Use <table> for structured data presentation, comparisons, and any information that naturally fits rows and columns.
The primary use of the table tag is presenting structured data in a tabular format:
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John Doe</td>
<td>25</td>
</tr>
</table>Apply CSS for borders, padding, and full-width layout instead of obsolete HTML attributes:
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
<td>New York</td>
</tr>
</table>Tables make it easy to compare values across categories such as monthly revenue:
<table>
<tr>
<th>Month</th>
<th>Revenue</th>
</tr>
<tr>
<td>January</td>
<td>$10,000</td>
</tr>
<tr>
<td>February</td>
<td>$12,500</td>
</tr>
</table>th so screen readers announce row/column context.caption to give the table a descriptive title.scope="col" or scope="row" on header cells in complex tables.Data is placed in tr rows with th headers and td values.
The table model arranges cells into columns and rows automatically.
Borders, spacing, and colors are applied with CSS for a polished look.
Users can scan, compare, and understand structured information quickly.
The <table> tag is supported in all major browsers, including Internet Explorer.
All browsers render <table> with the standard table layout model.
Bottom line: Use <table> confidently for tabular data in any browser.
Mastering the <table> tag is essential for presenting structured data on the web. By using semantic th headers, CSS for styling, and avoiding layout misuse, you create well-organized, accessible tables that improve the user experience.
th headersborder-collapse, padding)caption for table titles when helpfulborder, cellpadding, or cellspacing<table>Bookmark these before you build your next data table.
Rows and columns.
PurposeCore structure.
MarkupNot HTML attrs.
DesignUse Grid/Flexbox.
ComparisonAccessible labels.
A11yAll browsers.
Compatibilityth is a header cell. td is a standard data cell. Screen readers use th for context.Practice <table>, <tr>, <th>, and <td> in the Try It editor.
6 people found this page helpful