Utilities
Tables
Create table utilities for border collapse, spacing, layout, and caption positioning with full type safety.
Overview
Table utilities help you control the layout and styling of HTML tables including border behavior, cell spacing, layout algorithm, and caption positioning.
Why Use Table Utilities?
Table utilities help you:
- Control border behavior: Choose between collapsed or separate borders
- Manage cell spacing: Set consistent spacing between table cells
- Optimize layout: Choose between auto or fixed table layout algorithms
- Position captions: Control where table captions appear
useBorderCollapseUtility
The useBorderCollapseUtility() function creates utility classes for controlling how table borders interact.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useBorderCollapseUtility } from "@styleframe/theme";
const s = styleframe();
useBorderCollapseUtility(s, {
collapse: 'collapse',
separate: 'separate',
});
export default s;
styleframe/index.css
._border-collapse\:collapse { border-collapse: collapse; }
._border-collapse\:separate { border-collapse: separate; }
<table class="_border-collapse:collapse">Merged borders</table>
<table class="_border-collapse:separate">Separate borders</table>
Border Collapse Values
| Value | Description |
|---|---|
collapse | Adjacent cell borders merge into a single border |
separate | Each cell has its own distinct borders |
useBorderSpacingUtility
The useBorderSpacingUtility() function creates utility classes for setting the spacing between table cell borders.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useBorderSpacingUtility, useBorderSpacingXUtility, useBorderSpacingYUtility } from "@styleframe/theme";
const s = styleframe();
useBorderSpacingUtility(s, {
'0': '0',
'1': '0.25rem',
'2': '0.5rem',
'4': '1rem',
});
useBorderSpacingXUtility(s, {
'0': '0',
'2': '0.5rem',
'4': '1rem',
});
useBorderSpacingYUtility(s, {
'0': '0',
'2': '0.5rem',
'4': '1rem',
});
export default s;
styleframe/index.css
._border-spacing\:0 { border-spacing: 0; }
._border-spacing\:1 { border-spacing: 0.25rem; }
._border-spacing\:2 { border-spacing: 0.5rem; }
._border-spacing\:4 { border-spacing: 1rem; }
._border-spacing-x\:0 { --tw-border-spacing-y: 0; border-spacing: 0 var(--tw-border-spacing-y); }
._border-spacing-x\:2 { --tw-border-spacing-y: 0; border-spacing: 0.5rem var(--tw-border-spacing-y); }
._border-spacing-x\:4 { --tw-border-spacing-y: 0; border-spacing: 1rem var(--tw-border-spacing-y); }
._border-spacing-y\:0 { --tw-border-spacing-x: 0; border-spacing: var(--tw-border-spacing-x) 0; }
._border-spacing-y\:2 { --tw-border-spacing-x: 0; border-spacing: var(--tw-border-spacing-x) 0.5rem; }
._border-spacing-y\:4 { --tw-border-spacing-x: 0; border-spacing: var(--tw-border-spacing-x) 1rem; }
<table class="_border-collapse:separate _border-spacing:2">
Spaced cells
</table>
Note: Border spacing only works when
border-collapse is set to separate.useTableLayoutUtility
The useTableLayoutUtility() function creates utility classes for controlling the table layout algorithm.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useTableLayoutUtility } from "@styleframe/theme";
const s = styleframe();
useTableLayoutUtility(s, {
auto: 'auto',
fixed: 'fixed',
});
export default s;
styleframe/index.css
._table-layout\:auto { table-layout: auto; }
._table-layout\:fixed { table-layout: fixed; }
<table class="_table-layout:fixed" style="width: 100%;">
Fixed column widths
</table>
Table Layout Values
| Value | Description |
|---|---|
auto | Column widths determined by content (default) |
fixed | Column widths determined by first row or explicit widths; faster rendering |
useCaptionSideUtility
The useCaptionSideUtility() function creates utility classes for positioning table captions.
styleframe.config.ts
import { styleframe } from "styleframe";
import { useCaptionSideUtility } from "@styleframe/theme";
const s = styleframe();
useCaptionSideUtility(s, {
top: 'top',
bottom: 'bottom',
});
export default s;
styleframe/index.css
._caption-side\:top { caption-side: top; }
._caption-side\:bottom { caption-side: bottom; }
<table class="_caption-side:top">
<caption>Table title at top</caption>
</table>
<table class="_caption-side:bottom">
<caption>Table title at bottom</caption>
</table>
Examples
Data Table with Collapsed Borders
styleframe.config.ts
import { styleframe } from "styleframe";
import { useBorderCollapseUtility, useTableLayoutUtility } from "@styleframe/theme";
const s = styleframe();
useBorderCollapseUtility(s, { collapse: 'collapse' });
useTableLayoutUtility(s, { fixed: 'fixed' });
export default s;
styleframe/index.css
._border-collapse\:collapse { border-collapse: collapse; }
._table-layout\:fixed { table-layout: fixed; }
Usage in HTML:
<table class="_border-collapse:collapse _table-layout:fixed" style="width: 100%;">
<thead>
<tr>
<th style="border: 1px solid #e5e7eb;">Name</th>
<th style="border: 1px solid #e5e7eb;">Status</th>
<th style="border: 1px solid #e5e7eb;">Role</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid #e5e7eb;">John Doe</td>
<td style="border: 1px solid #e5e7eb;">Active</td>
<td style="border: 1px solid #e5e7eb;">Admin</td>
</tr>
</tbody>
</table>
Spaced Table Cells
styleframe.config.ts
import { styleframe } from "styleframe";
import { useBorderCollapseUtility, useBorderSpacingUtility } from "@styleframe/theme";
const s = styleframe();
useBorderCollapseUtility(s, { separate: 'separate' });
useBorderSpacingUtility(s, { '2': '0.5rem' });
export default s;
styleframe/index.css
._border-collapse\:separate { border-collapse: separate; }
._border-spacing\:2 { border-spacing: 0.5rem; }
Usage in HTML:
<table class="_border-collapse:separate _border-spacing:2">
<tr>
<td style="background: #f3f4f6; padding: 1rem;">Cell 1</td>
<td style="background: #f3f4f6; padding: 1rem;">Cell 2</td>
</tr>
<tr>
<td style="background: #f3f4f6; padding: 1rem;">Cell 3</td>
<td style="background: #f3f4f6; padding: 1rem;">Cell 4</td>
</tr>
</table>
Best Practices
- Use table-layout: fixed for performance: Fixed layout renders faster and more predictably with large tables
- Choose border-collapse carefully: Collapsed borders are simpler but separate allows for spacing
- Use border-spacing with separate borders: Border spacing only works when borders are not collapsed
- Consider accessibility: Ensure tables have proper headers and captions for screen readers
- Responsive tables: For responsive designs, consider alternative layouts for mobile
FAQ
Use
fixed when you have tables with many rows and want faster rendering, or when you need predictable column widths. The browser doesn't need to scan the entire table to calculate column widths, so it renders faster.Border spacing only works when
border-collapse is set to separate. If you're using collapse, borders merge and there's no space between them. Set border-collapse: separate first.No, HTML tables should only be used for tabular data. For layout purposes, use CSS Grid or Flexbox. Tables have semantic meaning for screen readers and using them for layout can cause accessibility issues.
For simple responsive tables, allow horizontal scrolling with
overflow-x: auto on a wrapper. For more complex needs, consider restructuring data on small screens using CSS Grid or displaying as cards.