Chapter 20: Responsive Tables

Container Queries for Tables

Engineering component-driven responsive tables using CSS Container Queries (`container-type: inline-size` and `@container`), eliminating viewport coupling for sidebar and widget embeds.

LEARNING OBJECTIVES
  • Understand why global Viewport Media Queries (@media) fail when responsive table components are embedded in narrow containers or sidebars.
  • Implement modern CSS Container Queries using container-type: inline-size and container-name.
  • Construct modular, context-aware tabular components that reflow between multi-column matrices and card stacks based on parent width.
  • Apply Container Query Length Units (cqi, cqw) for fluid typography and dynamic cell padding.
🎬 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)

Imagine a modern enterprise dashboard on a 32-inch 4K widescreen monitor ($3840\text{px}$ wide). The dashboard features a large main canvas on the left ($2800\text{px}$) and a narrow collapsible analytics sidebar on the right ($320\text{px}$).

4K Monitor Viewport (3840px Wide):
+-------------------------------------------------------------+-----------------------+
| Main Workspace Canvas (2800px Wide)                         | Sidebar (320px Wide)  |
|                                                             |                       |
| [ Recent Invoices Table Component ]                         | [ Recent Invoices     |
| +---------------------------------------------------------+ |   Table Component ]   |
| | ID    | Customer   | Date       | Amount   | Status     | |                       |
| | #101  | Acme Corp  | 2026-08-20 | $1,200   | Paid       | | ??? WHAT HAPPENS    |
| +---------------------------------------------------------+ |     UNDER @media ???  |
+-------------------------------------------------------------+-----------------------+

If you design the "Recent Invoices Table" using traditional Viewport Media Queries (@media (max-width: 768px)), the browser checks the global screen width ($3840\text{px}$). Because $3840\text{px} > 768\text{px}$, the table inside the $320\text{px}$ sidebar is forced to render in full desktop mode (5 columns), causing catastrophic horizontal blowout inside the sidebar!

CSS Container Queries solve this architectural flaw. Instead of asking "How wide is the user's monitor?", the table component asks "How wide is the container I am currently sitting in?". If embedded in the main canvas ($2800\text{px}$), it renders as a wide data matrix; if embedded in the sidebar ($320\text{px}$), it automatically reflows into vertical cards—even on a 4K screen.


Technical Deep Dive & Specifications

Establishing a Containment Context

To enable container queries, an ancestor element must be declared as a query container using the container-type property (CSS Containment Module Level 3):

.table-widget-container {
  /* Declares this element as a container query context on its inline (horizontal) axis */
  container-type: inline-size;
  /* Optional: Names the container to avoid ambiguity with nested containers */
  container-name: table-widget;
}
+-------------------------------------------------------------------------------+
| Container Property | Value        | Technical Purpose                         |
+-------------------------------------------------------------------------------+
| `container-type`   | `inline-size`| Queries the container's horizontal width. |
| `container-type`   | `size`       | Queries both horizontal width AND height. |
| `container-type`   | `normal`     | Default: Element is not a query container.|
| `container-name`   | `<identifier>`| Gives the container an explicit scope.   |
+-------------------------------------------------------------------------------+

The @container Rule Syntax

Once a container context is established, child elements apply conditional styles based on the container's inline size:

/* Unnamed container query */
@container (max-width: 550px) {
  .responsive-table,
  .responsive-table tbody,
  .responsive-table tr,
  .responsive-table td {
    display: block;
  }
}

/* Explicitly named container query */
@container table-widget (max-width: 550px) {
  .responsive-table thead {
    display: none;
  }
  
  .responsive-table td::before {
    content: attr(data-label);
    font-weight: 700;
  }
}

Container Query Units (CQ Units)

Container queries also introduce Container Query Length Units, which scale relative to the container's dimensions rather than the global viewport (vw/vh):

CQ Unit Dimension Represented Equivalent Viewport Unit
1cqi $1%$ of the query container's inline size (width) 1vw
1cqb $1%$ of the query container's block size (height) 1vh
1cqw $1%$ of the query container's width 1vw
1cqh $1%$ of the query container's height 1vh
1cqmin Smaller value of cqi or cqb 1vmin
1cqmax Larger value of cqi or cqb 1vmax
.table-cell {
  /* Dynamically scale padding based on parent container width */
  padding: clamp(8px, 1.5cqi, 20px);
  font-size: clamp(0.75rem, 1cqi + 0.5rem, 1rem);
}

💻 Interactive Code Playground

Starter Code

Line-by-Line Code Breakdown

  • Lines 49–54: .cq-table-wrapper establishes a container query boundary using container-type: inline-size; container-name: table-card;.
  • Lines 82–120: @container table-card (max-width: 460px) evaluates the width of the .cq-table-wrapper element itself, completely ignoring the global browser window width.
  • Lines 131–190: The exact same HTML markup is placed in both the wide main panel and the narrow sidebar panel.
  • Render Output: On a desktop monitor, the main panel displays a full 4-column table matrix, while the sidebar panel displays compact key-value cards, simultaneously on the same screen without custom JavaScript.

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: Build a Container-Aware Currency Conversion Table

Instructions:

  1. Create a container query context on .currency-widget using container-type: inline-size.
  2. Write a container query that transforms the table into vertical stacked cards when the container width drops below 400px.
  3. Use the cqi unit to make cell padding dynamically scale smoothly between 6px and 16px.

🏁 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. Applying @container queries directly to the container element: A query container cannot query itself. @container queries apply to children of the element declared with container-type.
  2. Using container-type: size when height is unconstrained: container-type: size requires both width and height containment, which causes height to collapse to 0px if not explicitly defined. Always use container-type: inline-size for responsive tables.

💡 Pro Tips

  1. Component-Driven Design Systems: Modern design systems (like Tailwind v3.4+ and Web Components) standardize on container queries so tabular UI components remain fully responsive in modals, drawers, tabs, and nested dashboard grids.
  2. Browser Support: Container queries have 100% universal support across all modern evergreen browsers (Chrome 105+, Safari 16+, Firefox 110+, Edge 105+).

📌 Key Takeaways

  • Viewport media queries fail when table components are placed inside narrow containers on wide screens.
  • container-type: inline-size creates a horizontal query context on a parent element.
  • @container (max-width: ...) triggers styles based on the container's width, enabling true component-driven responsiveness.
  • Container query units (cqi, cqw) allow fluid typography and padding that scale relative to the container box.
  • --
⭐ LEARN: HTML 🌟 ⚔️ QUIZ BATTLE ARENA // ACTIVE
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 1 / 3

Why is container-type: inline-size preferred over container-type: size when designing responsive tables?

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

Can an element declared with container-type: inline-size query its own size inside an @container rule?

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

What does the container query unit 1cqi represent?

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