๐Ÿ“Š Chapter 19: Advanced Table Techniques

Tables with Expandable Rows

Master-Detail Accordion Rows, `aria-expanded`, `aria-controls`, and Nested Sub-Tables

LEARNING OBJECTIVES โŒต
  • Construct master-detail table architectures using paired parent and collapsible child <tr> rows.
  • Connect interactive toggles to expandable containers using aria-expanded and aria-controls.
  • Span detail rows across full grid widths using <td colspan="100%"> (or precise column counting).
  • Embed nested sub-tables and metadata drawers without corrupting the parent table's accessibility tree.
๐ŸŽฌ INTERACTIVE VISUAL PIPELINE Core Architecture Simulation
๐ŸŒ
1. Input
Directives & Tags
โš™๏ธ
2. Parse
Tokenizer & AST
๐ŸŒณ
3. Layout
Box Model & Flow
๐ŸŽจ
4. Render
GPU Paint & Composite
PHASE 1: INPUT & DIRECTIVES
Browser receives declarative markup stream, parsing tag tokens and initializing component state.

๐Ÿ“– The Mental Model & Story (Intuitive Foundation)

Think of an executive summary binder. The summary sheet lists high-level project metrics: Project Name, Budget, and Status. Tucked behind each summary page is a tabbed pocket folder containing detailed receipts, team rosters, and itemized invoices.

When reviewing the binder, you don't want all 500 pages of receipts spilling out simultaneously. You want to inspect the summary line, pull open the tabbed folder for a specific project, review its receipts, and tuck it back in when finished.

+-----------------------------------------------------------------------------------------+
| [โ–ถ] Order #1042  |  Acme Corp      |  3 Items  |  $1,420.00  |  Shipped                 |  <-- Master Row
+-----------------------------------------------------------------------------------------+
|  โ””โ”€โ”€ [โ–ผ] (Expanded Detail Drawer - <td colspan="5">)                                     |
|      +-------------------------------------------------------------------------------+  |
|      | Item Description                 | SKU       | Qty | Unit Price | Line Total  |  |  <-- Nested Table
|      |----------------------------------+-----------+-----+------------+-------------|  |
|      | 4K Ultra-HD Monitor              | MON-4K-01 |  2  | $600.00    | $1,200.00   |  |
|      | Ergonomic Monitor Arm Mount      | ARM-DS-02 |  2  | $110.00    |   $220.00   |  |
|      +-------------------------------------------------------------------------------+  |
+-----------------------------------------------------------------------------------------+
| [โ–ถ] Order #1043  |  Globex Corp    |  1 Item   |    $89.00   |  Processing              |  <-- Master Row
+-----------------------------------------------------------------------------------------+

In web engineering, this is the Master-Detail Accordion Pattern. It keeps the primary grid readable while giving users instant, non-destructive access to relational sub-records.


Technical Deep Dive & Specifications

2.1 The Two-Row Pairing Pattern

In pure semantic HTML, a table row (<tr>) cannot directly contain another <tr>. To represent parent-child relational data, we pair two sequential sibling rows in the <tbody>:

<tbody>
  <!-- 1. Master Row -->
  <tr class="master-row">
    <td>
      <button 
        type="button" 
        class="toggle-btn"
        aria-expanded="false" 
        aria-controls="order-details-1042"
        id="toggle-btn-1042">
        <span class="chevron" aria-hidden="true">โ–ถ</span>
        <span class="sr-only">Toggle order 1042 details</span>
      </button>
    </td>
    <td>#1042</td>
    <td>Acme Corp</td>
    <td>$1,420.00</td>
  </tr>

  <!-- 2. Detail Row -->
  <tr id="order-details-1042" class="detail-row" hidden>
    <td colspan="4">
      <div class="drawer-content">
        <!-- Sub-table or detailed metadata here -->
      </div>
    </td>
  </tr>
</tbody>

2.2 The colspan="100%" / Dynamic Colspan Rule

To make the detail drawer span the entire width of the table, the child <td> must span all columns.

  • In HTML standards, specifying <td colspan="4"> (matching the exact number of header columns) is the most robust approach.
  • In modern browsers, <td colspan="100%"> or <td colspan="99"> automatically expands to the maximum number of available columns without throwing parsing errors.

2.3 ARIA Accessibility Wireframe

Assistive technologies rely on explicit programmatic relationships to understand expandable UI:

[ <button id="btn-1" aria-expanded="false" aria-controls="drawer-1"> ]
                              โ”‚
                              โ”‚ (aria-controls references ID)
                              โ–ผ
[ <tr id="drawer-1" hidden aria-labelledby="btn-1"> ]
Semantic Attribute Location Functional Impact
aria-expanded="false" Toggle <button> Announces "collapsed" or "expanded" state to screen readers upon focus and activation.
aria-controls="ID" Toggle <button> Programmatically links the button to the DOM element it controls.
id="ID" Detail <tr> Unique target identifier referenced by aria-controls.
hidden Detail <tr> Completely removes the collapsed drawer from the Accessibility Tree until expanded.

2.4 Nested Tables & Accessibility Trees

When embedding a nested <table> inside the detail drawer:

  1. The inner table is completely isolated in the DOM hierarchy inside its host <td>.
  2. The nested table must define its own independent <thead>, <th> scope="col", and <tbody>.
  3. Screen readers seamlessly switch context when entering the nested table and announce its internal column headers accurately.

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL example.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45ยฐC
INSPECTING DOM: VALID
TAGS: SCANNING...

๐Ÿ’ป Interactive Code Playground

Starter Code

Line-by-Line Code Breakdown

  • Lines 134โ€“144: The <button> uses aria-expanded="false" and points to aria-controls="detail-ord-101", providing explicit programmatic linkage.
  • Lines 149โ€“181: The detail row (<tr id="detail-ord-101" class="detail-row" hidden>) spans all 5 columns with <td colspan="5"> and encloses a fully semantic nested <table class="sub-table">.
  • Lines 76โ€“84: Smooth CSS transform (transform: rotate(90deg)) rotates the chevron icon when aria-expanded="true".
  • Lines 224โ€“240: Event delegation captures toggle clicks, flips aria-expanded, and toggles the native detailRow.hidden property.

Expected Browser Render Output

  • A customer fulfillment table with Orders ORD-101 and ORD-102.
  • Clicking the โ–ถ button on ORD-101 rotates the chevron to โ–ผ and smoothly expands the blue-accented drawer showing a nested 2-row itemized invoice table.
  • Screen readers announce: "Toggle details for order ORD-101, button, expanded".

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL playground.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45ยฐC
INSPECTING DOM: VALID
TAGS: SCANNING...

๐Ÿ‹๏ธ Hands-On Exercise

๐ŸŽฏ The Challenge: Global Master Toggle ("Expand All" / "Collapse All")

Add a master toggle button in the table header or caption that:

  1. Expands all closed rows when clicked if any row is collapsed.
  2. Collapses all open rows if all rows are already expanded.
  3. Synchronizes all aria-expanded and hidden properties across all buttons and detail rows.

Instructions:

  1. Insert a <button id="global-toggle" type="button">Expand All</button> into the table caption.
  2. Check if at least one row is collapsed (aria-expanded="false").
  3. If yes, expand all rows and update the global button text to "Collapse All".
  4. If no, collapse all rows and update text to "Expand All".

๐Ÿ Starter Code Sandbox

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
STARTER CODE SANDBOX exercise.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45ยฐC
INSPECTING DOM: VALID
TAGS: SCANNING...

โš ๏ธ Common Pitfalls

  1. Wrong colspan Count: If your master table has 5 columns and your detail row sets <td colspan="3">, the detail drawer will leave an ugly empty gap on the right and corrupt layout grids.
  2. Nesting <tr> inside <tr>: HTML strictly forbids placing <tr> inside another <tr>. Always structure master and detail as sibling rows in <tbody>.
  3. Missing Nested Table Headers: Omitting <th> and scope="col" inside nested sub-tables prevents screen reader users from understanding sub-table columns.
  4. Forgetting hidden attribute reset: Without [hidden] { display: none !important; }, custom CSS rules on tr can prevent collapsed detail rows from hiding.

๐Ÿ’ก Pro Tips

  1. Lazy-Loaded Sub-Tables (AJAX on Expand): If detail data is expensive, fetch the sub-record JSON on the first toggle click, render the HTML template dynamically, and cache it on the DOM node.
  2. Smooth Height Transitions with CSS Grid: HTML <tr> elements do not support CSS height transitions. Wrap inner drawer contents in a <div class="drawer-grid"> using grid-template-rows: 0fr to 1fr for 60 FPS accordion animations.
  3. Keyboard Arrow Key Traversal: Enable ArrowRight to expand and ArrowLeft to collapse when focused on the master toggle button.

๐Ÿ“Œ Key Takeaways

  • Implement master-detail tables using sibling <tr> pairs inside <tbody> (<tr class="master"> followed by <tr class="detail">).
  • Span detail rows across all columns using <td colspan="N"> where $N$ matches total column count.
  • Connect buttons to collapsible rows using aria-expanded and aria-controls.
  • Use the native hidden attribute to remove collapsed detail content from visual layout and the Accessibility Tree.
  • Structure nested sub-tables with independent <thead> and <th> elements to preserve accessibility.
  • --
โญ LEARN: HTML ๐ŸŒŸ โš”๏ธ QUIZ BATTLE ARENA // ACTIVE
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 1 / 3

Why cannot a child detail row be nested directly inside the parent <tr> element (e.g. <tr><tr>...</tr></tr>)?

Question 1 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 2 / 3

What is the role of aria-controls on the accordion toggle button?

Question 2 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 3 / 3

How should a detail row cell (<td>) be configured to span across a 6-column data table?

Question 3 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP