LEARNING OBJECTIVES ⌵
- Understand the fundamental mechanics of cell padding on
<th>and<td>elements in the CSS Box Model. - Master two-dimensional
border-spacing: <horizontal> <vertical>in the separated border model. - Architect a flexible, enterprise-grade UI Density System (Compact, Standard, Comfortable modes) using CSS Custom Properties.
- Ensure touch-target accessibility compliance (WCAG 2.5.5 / 2.5.8 Target Size) on mobile and tablet data grids.
📖 The Mental Model & Story (Intuitive Foundation)
Imagine sitting in the cabin of an airplane.
COMPACT DENSITY STANDARD DENSITY COMFORTABLE DENSITY
(Economy Class Seat) (Business Class Seat) (First Class Suite)
+-------------------------------+ +---------------------------------+ +-------------------------------------+
| [Row 34] 28" Pitch / Tight | | [Row 12] 38" Pitch / Balanced | | [Row 1] 60" Pitch / Spacious |
| - High data capacity per sq ft| | - Balanced readability & space | | - High breathing room, relaxed view |
| - Used for dense operations | | - Default for standard portals | | - Best for high-touch consumer apps |
+-------------------------------+ +---------------------------------+ +-------------------------------------+
In data presentation, UI Density is the exact same trade-off:
- A Wall Street stock trader monitoring 500 stock tickers at 9:30 AM demands Compact Density: small fonts, tight 4px padding, and maximum rows per screen fold.
- A consumer reviewing their monthly electricity bill on an iPad needs Comfortable Density: large 16px padding, generous touch targets, and relaxed visual rhythm.
Managing cell padding and spacing is how frontend engineers tailor data grids precisely to the user's operational context.
Technical Deep Dive & Specifications
The Table Cell Box Model Mechanics
Table cells (<th> and <td>) adhere to specialized CSS formatting rules that differ from standard <div> elements:
+-------------------------------------------------------------+
| Table Margin (Only applies to <table>, NOT cells!) |
| +-------------------------------------------------------+ |
| | Table Border | |
| | +-------------------------------------------------+ | |
| | | Cell Border | | |
| | | +-------------------------------------------+ | | |
| | | | Cell Padding (padding: Top Right Bottom Left) | | |
| | | | +-------------------------------------+ | | | |
| | | | | Cell Content (Text, Badges, etc.) | | | | |
| | | | +-------------------------------------+ | | | |
| | | +-------------------------------------------+ | | |
| | +-------------------------------------------------+ | |
| +-------------------------------------------------------+ |
+-------------------------------------------------------------+
padding: Applied directly to<th>and<td>. It controls the internal breathing room between the cell's outer border and the internal text/content.marginon cells: Has NO effect. You cannot applymargin: 10pxto a<td>.border-spacing: Applied to the parent<table>whenborder-collapse: separate. It accepts either 1 value (all directions) or 2 values (horizontal vertical):table { border-collapse: separate; border-spacing: 12px 6px; /* 12px horizontal gap between columns, 6px vertical gap between rows */ }
Designing Enterprise UI Density Presets
Modern enterprise design systems (like Salesforce Lightning, Ant Design, Material Design, and Shopify Polaris) provide three standardized density tiers:
| Density Tier | Recommended Padding | Row Height | Typical Use Case |
|---|---|---|---|
Compact (--density-compact) |
6px 8px |
$\approx 32\text{px}$ | Financial terminals, server monitoring logs, bulk warehouse inventory. |
Standard (--density-standard) |
12px 16px |
$\approx 48\text{px}$ | SaaS administrative tables, CRM contacts, analytics dashboards. |
Comfortable (--density-comfortable) |
18px 24px |
$\approx 64\text{px}$ | Consumer billing receipts, mobile touch screens, executive summaries. |
/* CSS Custom Properties Density Architecture */
:root {
--table-cell-padding-compact: 6px 8px;
--table-cell-padding-standard: 12px 16px;
--table-cell-padding-comfortable: 18px 24px;
}
/* Default standard density */
.data-table th,
.data-table td {
padding: var(--table-cell-padding-standard);
}
/* Modifier classes */
.data-table--compact th,
.data-table--compact td {
padding: var(--table-cell-padding-compact);
font-size: 0.8125rem;
}
.data-table--comfortable th,
.data-table--comfortable td {
padding: var(--table-cell-padding-comfortable);
font-size: 1rem;
}
Touch Target Guidelines (WCAG 2.5.5 / 2.5.8)
On touch-screen devices, interactive elements (like checkboxes, sorting buttons, or row action menus) inside table cells must have a minimum target size:
- WCAG 2.1 Level AAA (2.5.5): $44 \times 44\text{ pixels}$.
- WCAG 2.2 Level AA (2.5.8): $24 \times 24\text{ pixels}$ with sufficient perimeter spacing.
If your table is in Compact Density on desktop, use CSS Media Queries to switch to Comfortable Density on touch screens (@media (pointer: coarse)).
💻 Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 51–64 (
.density-compact,.density-standard,.density-comfortable): Defines the three distinct padding and font-size levels. - Line 72–76 (
<div class="controls">): Renders interactive buttons to switch the table's visual density at runtime. - Line 115–125 (
<script>...setDensity...): Toggles the active CSS class on the table element dynamically, demonstrating instant layout adaptation without re-rendering the DOM structure.
Expected Browser Render Output
Cloud Storage Buckets
[Compact (32px)] [Standard (48px) *Active*] [Comfortable (64px)]
BUCKET NAME REGION OBJECTS STORAGE SIZE
--------------------------------------------------------------
prod-assets-media us-east-1 1,420,891 482.5 GB
analytics-raw-logs eu-west-1 9,850,210 2.4 TB
backup-archive-glacier us-west-2 412,000 18.9 TB
(Clicking Compact tightens row height to 32px; clicking Comfortable expands row height to 64px)🏋️ Hands-On Exercise
🎯 The Challenge: Build a Touch-Adaptive Responsive Density Table
Scenario: Build an operations table for field engineers. When viewed on desktop mice pointers, the table should default to a dense compact view (padding: 6px 10px). When viewed on a touchscreen mobile/tablet device (@media (pointer: coarse)), it must automatically expand to comfortable padding (padding: 16px 20px) so that row selection checkboxes can be easily tapped by human thumbs.
Instructions:
- Create a table with 4 columns: Selection Checkbox (
<input type="checkbox">),Device ID,Location, andBattery Level. - Style default desktop cells with compact padding (
6px 10px) andborder-collapse: collapse. - Add a media query for
@media (pointer: coarse)that expands cell padding to16px 20pxand increases checkbox dimensions to20px x 20px. - Ensure all header and row cells have semantic
scopeattributes.
🏁 Starter Code Sandbox
⚠️ Common Pitfalls
- Attempting to set
marginon<td>elements: As established by the CSS specification,marginis invalid ondisplay: table-cell. Always control spacing withpaddingorborder-spacing. - Neglecting Horizontal Cell Padding: Setting
padding: 12px 0;causes cell text in adjacent columns to collide with no horizontal separation. Always provide adequate horizontal padding (padding: 12px 16px;). - Fixed Row Heights with
height: 30pxon<tr>: Setting strict pixel heights on rows causes text to clip or overflow if cell text wraps on smaller screens. Usepaddingto control height naturally.
💡 Pro Tips
- Use
chUnits for Predictable Column Spacing: When styling fixed-character data (like currency or phone numbers), set min-widths using thechunit (e.g.,min-width: 12ch;) to ensure columns never wrap awkwardly. - Adaptive Padding with CSS
clamp(): For responsive data tables, create fluid cell padding withpadding: clamp(6px, 1.5vw, 16px) clamp(8px, 2vw, 20px);to scale smoothly across phone, tablet, and ultra-wide monitor viewports. - Implement Visual Density Switchers in User Preferences: In SaaS products with power users, save the user's preferred density (
compact,standard,comfortable) tolocalStorageor their user profile database so tables load in their preferred format across sessions.
📌 Key Takeaways
- Table cell breathing room is controlled via
paddingon<th>and<td>elements (CSSmarginhas no effect on cells). border-spacing: <horizontal> <vertical>allows independent X and Y spacing in the separated border model.- Enterprise design systems standardize on 3 UI density presets: Compact ($\approx 32\text{px}$ row height), Standard ($\approx 48\text{px}$), and Comfortable ($\approx 64\text{px}$).
- Use
@media (pointer: coarse)to automatically expand touch targets and cell padding on touchscreen devices for WCAG 2.2 compliance. - Never hardcode strict pixel
heighton<tr>; let cell padding drive row heights safely. - --