HTML <table> Tag

Beginner
⏱️ 7 min read
📚 Updated: Jun 2026
🎯 3 Examples
Tables

What You’ll Learn

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.

01

Table Basics

Rows, columns, and cells.

02

tr, th, td

Core table structure.

03

CSS Styling

Borders, spacing, width.

04

Data Tables

Present structured information.

05

Not for Layout

Tabular data only.

06

Accessibility

Headers and captions.

What Is the <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.

Valid HTML5 — Tabular Data Only

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.

📝 Syntax

Create a basic table with table, tr, th, and td:

syntax.html
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
  </tr>
</table>

Name and Age Example

name-age-table.html
<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>25</td>
  </tr>
</table>

Syntax Rules

  • Every row starts with tr and contains one or more th or td cells.
  • Use th for column or row headings; use td for data values.
  • Style tables with CSS, not obsolete HTML attributes like border or cellpadding.
  • Add a caption as the first child when the table needs a visible title.

⚡ Quick Reference

TopicCode SnippetNotes
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 bordersborder-collapse: collapseClean borders
PurposeTabular dataNot page layout

📋 Table Structure

Understanding how table elements fit together is essential for readable, accessible markup:

ElementNamePurpose
<table>TableRoot container for the entire table
<caption>CaptionVisible title (first child of table)
<thead>Table HeadGroup of header rows
<tbody>Table BodyGroup of main data rows
<tr>Table RowOne horizontal row of cells
<th>Table HeaderHeader cell for a column or row
<td>Table DataStandard 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.

⚖️ Tables vs Page Layout

ApproachUse forAvoid for
<table>Spreadsheets, pricing, schedulesPage columns, nav bars, grids
CSS Grid / FlexboxPage layout, cards, responsive UISemantic tabular data labels
AccessibilityTables expose row/column structureLayout tables confuse screen readers

🧰 Attributes

The <table> tag has no required attributes. Prefer CSS over obsolete presentational attributes.

attributes.html
<table class="data-table">
  <!-- Table content here -->
</table>
class / id Global

Hook for CSS to style borders, width, and spacing.

class="data-table"
border Obsolete

Deprecated in HTML5. Use border in CSS on th and td instead.

/* CSS */ border: 1px solid #ddd
cellpadding / cellspacing Obsolete

Deprecated. Use CSS padding on cells and border-collapse on the table.

padding: 8px
width CSS preferred

HTML width attribute is obsolete. Set width in CSS for responsive control.

width: 100%

🎨 Styling and Formatting

Customize table appearance with CSS applied to table, tr, th, and td:

styling-and-formatting.html
<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.

Examples Gallery

Basic structure, CSS styling, and comparative data tables with copy-ready code and live previews.

👀 Live Preview

Simple two-column data table:

NameAge
John Doe25
Jane Smith30

📚 Common Use Cases

Use <table> for structured data presentation, comparisons, and any information that naturally fits rows and columns.

Data Presentation

The primary use of the table tag is presenting structured data in a tabular format:

data-presentation.html
<table>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>25</td>
  </tr>
</table>
Try It Yourself

CSS Styled Table

Apply CSS for borders, padding, and full-width layout instead of obsolete HTML attributes:

styled-table.html
<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>
Try It Yourself

Comparative Data

Tables make it easy to compare values across categories such as monthly revenue:

comparative-data.html
<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>
Try It Yourself

♿ Accessibility

  • Use meaningful headers — Mark column labels with th so screen readers announce row/column context.
  • Add a caption — Use caption to give the table a descriptive title.
  • Use scope on th — Add scope="col" or scope="row" on header cells in complex tables.
  • Never use tables for layout — Layout tables create confusing experiences for assistive technology users.

🧠 How <table> Works

1

Author defines rows and cells

Data is placed in tr rows with th headers and td values.

Markup
2

Browser builds the grid

The table model arranges cells into columns and rows automatically.

Layout
3

CSS styles the presentation

Borders, spacing, and colors are applied with CSS for a polished look.

Design
=

Organized tabular data

Users can scan, compare, and understand structured information quickly.

Browser Support

The <table> tag is supported in all major browsers, including Internet Explorer.

Baseline · HTML4 / HTML5

Tables everywhere

All browsers render <table> with the standard table layout model.

100% Core tag support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported · EOL
Full support
Opera Fully supported
Full support
<table> tag 100% supported

Bottom line: Use <table> confidently for tabular data in any browser.

Conclusion

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.

💡 Best Practices

✅ Do

  • Use tables for tabular data only
  • Provide concise, meaningful th headers
  • Style with CSS (border-collapse, padding)
  • Add caption for table titles when helpful

❌ Don’t

  • Use tables for page layout
  • Rely on obsolete border, cellpadding, or cellspacing
  • Skip header cells on data tables
  • Overload tables with nested layout complexity

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <table>

Bookmark these before you build your next data table.

6
Core concepts
📑 02

tr th td

Core structure.

Markup
🎨 03

CSS Styling

Not HTML attrs.

Design
⚖️ 04

Not Layout

Use Grid/Flexbox.

Comparison
05

th Headers

Accessible labels.

A11y
🌐 06

100% Support

All browsers.

Compatibility

❓ Frequently Asked Questions

It organizes tabular data into rows and columns. Use it for data grids, not page layout.
th is a header cell. td is a standard data cell. Screen readers use th for context.
No. Those attributes are obsolete in HTML5. Use CSS for borders and padding instead.
No. Use CSS Grid or Flexbox for layout. Tables are for tabular data only.
Yes. Full support in every major browser including Internet Explorer.

Build your first data table

Practice <table>, <tr>, <th>, and <td> in the Try It editor.

Try basic table example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful