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 / Use textarea, select, option, and button

HTML Tutorial / Use textarea, select, option, and button

Create multi-line fields, selection lists, and form buttons while understanding initial values, submitted values, and reset behavior.

HTML Tutorial / Form Structure and GET vs. POST

HTML Tutorial / Form Structure and GET vs. POST

Understand how form action, method, and control names determine where form values go, and when GET or POST is appropriate.

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 / Form Validation with required, Length, and Range Limits

HTML Tutorial / Form Validation with required, Length, and Range Limits

Apply built-in required, length, and numeric constraints, test error states, and understand why server-side validation remains necessary.

HTML Tutorial / Accessible Tables: caption, scope, and Spanning Cells

HTML Tutorial / Accessible Tables: caption, scope, and Spanning Cells

Make table headings and data relationships clear with caption, th, and scope, and learn when colspan or rowspan makes a table harder to read.

HTML Tutorial / Add Placeholder Text to Form Fields

HTML Tutorial / Add Placeholder Text to Form Fields

Learn how to add placeholder hints to HTML input and textarea fields without confusing them with labels or initial values. Includes a complete form example, required-field checks, and an explanation of empty values in submitted data.

HTML Tutorial / Responsive Images: srcset, sizes, and picture

HTML Tutorial / Responsive Images: srcset, sizes, and picture

Offer image resolutions with srcset and sizes, and switch crops with picture. Downloadable files let you test the examples in a browser.

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 / 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 / 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.