03 / 06

What is the purpose of <thead>, <tbody>, and <tfoot>?

Purpose of <thead>, <tbody>, and <tfoot> in HTML Tables

The <thead>, <tbody>, and <tfoot> elements in HTML are used to semantically group different parts of a table. This helps organize the table for both styling and accessibility purposes.

Key Points
  1. 1

    <thead>: Groups the header rows of a table, typically containing <th> elements. It helps browsers and screen readers understand which rows are headers and allows styling them separately.

  2. 2

    <tbody>: Groups the main body rows of the table, containing the majority of <tr> data rows. It improves semantics and enables easier scrolling or styling of the table body.

  3. 3

    <tfoot>: Groups the footer rows of a table, often used for totals or summary information. It allows browsers to render table footers consistently and can appear before the body in HTML for certain rendering or printing behaviors.

Example Usage

In short: Use <thead> for header rows, <tbody> for the main content, and <tfoot> for footer or summary rows. These groupings improve table semantics, accessibility, and styling options.

Difficulty: 2/10
Topics: semantic HTML, table structure, accessibility

Scenario Questions

0-2 years experience
  1. 1

    You're building a simple data table for a user dashboard. Walk me through how you'd structure the HTML — where would you put the column headers versus the data rows?

  2. 2

    A designer hands you a table mockup with a sticky header row and a summary footer row. What HTML elements would you use for each, and why?

  3. 3

    If you forgot to wrap your header row in <thead> and just used <tr> directly inside <table>, what would break — visually, for screen readers, or when printing?

2-5 years experience
  1. 1

    We have a sortable, paginated table where the header row stays fixed while the body scrolls. The current markup puts everything in a single <tbody>. What accessibility or styling problems does that cause, and how would you restructure it?

  2. 2

    A QA engineer reports that when printing a 50-row invoice table, the column headers only appear on the first page. The markup uses <thead> — why isn't it repeating, and what CSS might be interfering?

  3. 3

    You're adding row-grouping to a report table (e.g., subtotals per region). Would you use multiple <tbody> sections or a different approach? What tradeoffs does each have for screen readers and CSS?

5-8 years experience
  1. 1

    Our virtualized table renders only visible rows for performance, but we still want <thead> and <tfoot> to stay in the DOM for accessibility. How do you reconcile virtualization with semantic table structure without breaking screen reader navigation?

  2. 2

    A legacy codebase has hundreds of tables missing semantic sections. You're tasked with a codemod to add <thead>/<tbody>/<tfoot> automatically. What heuristics would you use to detect header vs body vs footer rows reliably, and what edge cases would you flag for manual review?

  3. 3

    In a server-rendered React app, hydration mismatches occur because the server renders a full <tbody> but the client initially renders an empty one for virtualization. How would you structure the HTML to avoid this while preserving semantic correctness?

8+ years experience
  1. 1

    You're defining the table component API for a design system used by 50+ teams. Do you enforce <thead>/<tbody>/<tfoot> as required slots, or allow flexible composition? How do you balance semantic guarantees against real-world markup variations teams will inevitably produce?

  2. 2

    During a platform migration, you discover that 30% of tables in the codebase use <div> wrappers inside <tbody> for layout hacks, breaking semantic structure. Propose a migration strategy that fixes accessibility without a full rewrite, and how you'd prevent regression.

  3. 3

    An analytics team wants to inject tracking pixels into every table footer across the product. They propose appending to <tfoot> via a global script. Why is this architecturally risky, and what alternative pattern would you design for cross-cutting table concerns?

Follow-up Questions

  • How would a screen reader announce a table that skips these elements?
  • Can you have multiple tbody sections in one table?
  • What happens to thead/tfoot when printing a long table across pages?