LEARNING OBJECTIVES โต
- Construct master-detail table architectures using paired parent and collapsible child
<tr>rows. - Connect interactive toggles to expandable containers using
aria-expandedandaria-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.
๐ 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:
- The inner table is completely isolated in the DOM hierarchy inside its host
<td>. - The nested table must define its own independent
<thead>,<th> scope="col", and<tbody>. - Screen readers seamlessly switch context when entering the nested table and announce its internal column headers accurately.
๐ป Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Lines 134โ144: The
<button>usesaria-expanded="false"and points toaria-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 whenaria-expanded="true". - Lines 224โ240: Event delegation captures toggle clicks, flips
aria-expanded, and toggles the nativedetailRow.hiddenproperty.
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".
๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Global Master Toggle ("Expand All" / "Collapse All")
Add a master toggle button in the table header or caption that:
- Expands all closed rows when clicked if any row is collapsed.
- Collapses all open rows if all rows are already expanded.
- Synchronizes all
aria-expandedandhiddenproperties across all buttons and detail rows.
Instructions:
- Insert a
<button id="global-toggle" type="button">Expand All</button>into the table caption. - Check if at least one row is collapsed (
aria-expanded="false"). - If yes, expand all rows and update the global button text to
"Collapse All". - If no, collapse all rows and update text to
"Expand All".
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Wrong
colspanCount: 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. - Nesting
<tr>inside<tr>: HTML strictly forbids placing<tr>inside another<tr>. Always structure master and detail as sibling rows in<tbody>. - Missing Nested Table Headers: Omitting
<th>andscope="col"inside nested sub-tables prevents screen reader users from understanding sub-table columns. - Forgetting
hiddenattribute reset: Without[hidden] { display: none !important; }, custom CSS rules ontrcan prevent collapsed detail rows from hiding.
๐ก Pro Tips
- 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.
- 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">usinggrid-template-rows: 0frto1frfor 60 FPS accordion animations. - Keyboard Arrow Key Traversal: Enable
ArrowRightto expand andArrowLeftto 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-expandedandaria-controls. - Use the native
hiddenattribute 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. - --