AD JB Toolbox 01 EN

HTML Tutorial / Table Basics: table, tr, th, and td

Use an HTML table for data whose values relate by rows and columns. A table contains rows (tr); each row has header cells (th) or data cells (td). Do not use a table merely to position parts of a page.

The basic table structure

This small table records how many pieces of fruit are in stock. The first row names the columns; the following rows hold values.

<table>
  <tr>
    <th>Fruit</th>
    <th>Stock</th>
  </tr>
  <tr>
    <td>Apples</td>
    <td>12 pieces</td>
  </tr>
  <tr>
    <td>Pears</td>
    <td>8 pieces</td>
  </tr>
</table>

Paste the code into an HTML file's body. Each tr forms one horizontal row, and its cells appear from left to right. The browser's default table style may show little or no border; add borders with CSS only after the data structure is correct. When planning a table, sketch its columns and rows first. To add another fruit, add another data row with a fruit name and stock value.

Fruit Stock
Apples 12 pieces
Pears 8 pieces

Separate header and body rows

Use thead and tbody when you want to group header and data rows explicitly. A summary row can go in tfoot. These elements organize rows; they do not create colors or borders by themselves. You can omit them in a small table, although the browser's HTML parser may insert a tbody into the document tree even when it was not written in the source.

<table>
  <thead>
    <tr><th>Fruit</th><th>Stock</th></tr>
  </thead>
  <tbody>
    <tr><td>Apples</td><td>12 pieces</td></tr>
    <tr><td>Pears</td><td>8 pieces</td></tr>
  </tbody>
</table>

Choose header cells and data cells

Use th when a value identifies a row or column; use td for the data associated with that heading. Do not choose th just to make text bold. A table can have both column headers and row headers. Include units, either beside each number or in an appropriate heading. A bare value such as “12” may not tell readers whether it means items or a price.

Once the basics are clear, you can add a caption to name the table, scope to clarify header relationships, and colspan or rowspan when a cell truly covers multiple positions. The MDN table reference describes these features.

Troubleshoot a misaligned table

Check that each ordinary row has the expected number of cells. Mismatched counts can place data under the wrong heading. If a wide table is clipped on a narrow screen, consider a horizontal scrolling area instead of squeezing the content until it is unreadable. Do not insert repeated spaces inside cells to imitate columns; alignment and borders belong in CSS, while the data relationships belong in HTML.

More in This Category
HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML gives web content structure and meaning. Learn how it differs from CSS and JavaScript, see how basic markup appears in a browser, and make your first HTML file.

HTML Tutorial / Syntax

HTML Tutorial / Syntax

Learn the essential rules of HTML syntax, including elements, tags, attributes, void elements, comments, nesting, and the structure of a complete HTML document.

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

Choose the right HTML list for ordered items, unordered items, and name–description pairs. Try a complete example, custom numbering, and correctly nested lists.

HTML Tutorial / When to Use div and span

HTML Tutorial / When to Use div and span

Understand the structural difference between div and span, style only the content you need, and choose semantic elements when they better describe the content.

HTML Tutorial / Group Images and Captions with figure and figcaption

HTML Tutorial / Group Images and Captions with figure and figcaption

Use figure and figcaption to keep an image and its visible explanation together, and distinguish captions from image alt text.

HTML Tutorial / Table Basics: table, tr, th, and td

HTML Tutorial / Table Basics: table, tr, th, and td

Build a basic HTML data table with rows and header or data cells, then organize its headings and body without using tables for page layout.

HTML Tutorial / Divide a Page with Semantic Elements

HTML Tutorial / Divide a Page with Semantic Elements

Learn when to use header, nav, main, article, section, aside, and footer, then build a complete page whose regions and in-page links work together.

HTML Tutorial / Images: img, Alt Text, and Dimensions

HTML Tutorial / Images: img, Alt Text, and Dimensions

Add an image with HTML img, write useful alternative text, and set width and height correctly. A downloadable example helps you test file paths and sizing.

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

Learn when to use HTML em, strong, q, blockquote, abbr, code, and kbd. Examples show how each element conveys meaning rather than just changing the appearance of text.

HTML Tutorial / Basic Structure of an HTML Document

HTML Tutorial / Basic Structure of an HTML Document

Learn the roles of doctype, html, head, and body, then save your first HTML file and open it in a browser. Includes a complete example, a diagram, and fixes for common filename, refresh, and encoding problems.