Table
Tables organize data into columns and rows to make information easier to scan.
Playground
Qty | Price | Details |
---|---|---|
1 | $X.XX | No cats |
<Table><TableCaption><Typography component="h2" textSize={4}>My table caption</Typography></TableCaption><TableHead><TableRow><TableCell>Qty</TableCell><TableCell>Price</TableCell><TableCell>Details</TableCell></TableRow></TableHead><TableBody><TableRow><TableCell>1</TableCell><TableCell>$X.XX</TableCell><TableCell>No cats</TableCell></TableRow></TableBody></Table>
<table class="table-skin-simple"><caption class="text-left"><h3>Table caption</h3><!-- use the h* tag that fits your data structure --></caption><thead><tr><th>Qty</th><th>Price</th><th>Details</th></tr></thead><tbody><tr><td>1</td><td>$X.XX</td><td>No cats</td></tr></tbody></table>
Table caption
For accessibility, each table needs a caption, which should be the first child of the table. Typically, this caption holds the heading that labels the table. Use the Typography component to set the heading tag, text size, and text alignment as needed.
If your table somehow does not have a visible heading that goes with it, you will need to add a caption for accessibility support, and then make it visually hidden.
<TableCaption><Typography component="h2" textSize={4}>My table caption</Typography></TableCaption>
<table class="table-skin-simple"><caption class="text-left"><!-- without this class, the caption will be centered be default --><!-- use an h* tag and text size as appropriate for your data and design --><h3 class="text-size-4">Table caption</h3></caption></table>
Header cells
Per the HTML spec, the <td>
tag is for regular cells, and <th>
for header cells. This includes row headers (rather than column headers); for these, you should still use <th>
along with a property of scope="row"
.
As a convenience in React, TableCell
will intelligently render a <th>
vs a <td>
depending on whether it's rendered inside of a TableHead
vs a TableBody
. It can take the scope
prop.
Cell alignment
Cells may have a default alignment (left, center, or right), depending on which Table skin you're using. You can override this alignment for a given cell.
Default header | Header aligned left | Default header |
---|---|---|
Default value | Default value | Default value |
Cell aligned left | Cell aligned right | Cell aligned center |
<Table skin="simple"><TableCaption><Typography component="h2" textSize={4}>My table caption</Typography></TableCaption><TableHead><TableRow><TableCell>Default header</TableCell><TableCell textAlign="left">Header aligned left</TableCell><TableCell>Default header</TableCell></TableRow></TableHead><TableBody><TableRow><TableCell>Default value</TableCell><TableCell>Default value</TableCell><TableCell>Default value</TableCell></TableRow><TableRow><TableCell textAlign="left">Cell aligned left</TableCell><TableCell textAlign="right">Cell aligned right</TableCell><TableCell textAlign="center">Cell aligned center</TableCell></TableRow></TableBody></Table>
Cells with two lines
Cells can contain two lines, if they need secondary information. The "second line" sub-component will place that second line below the first, shrinking the font as it does so.
Qty | Regular Unit cost | Large Unit cost |
---|---|---|
1 | $X.XX $X.XX | $X.XX $X.XX |
2 | $X.XX $X.XX | $X.XX $X.XX |
3 | $X.XX $X.XX | $X.XX $X.XX |
<Table><TableCaption><Typography component="h2" textSize={4}>My table caption</Typography></TableCaption><TableHead><TableRow><TableCell>Qty</TableCell><TableCell>Regular<TableTextSecondLine>Unit cost</TableTextSecondLine></TableCell><TableCell>Large<TableTextSecondLine>Unit cost</TableTextSecondLine></TableCell></TableRow></TableHead><TableBody><TableRow><TableCell>1</TableCell><TableCell>$X.XX<TableTextSecondLine>$X.XX</TableTextSecondLine></TableCell><TableCell>$X.XX<TableTextSecondLine>$X.XX</TableTextSecondLine></TableCell></TableRow><TableRow><TableCell>2</TableCell><TableCell>$X.XX<TableTextSecondLine>$X.XX</TableTextSecondLine></TableCell><TableCell>$X.XX<TableTextSecondLine>$X.XX</TableTextSecondLine></TableCell></TableRow><TableRow><TableCell>3</TableCell><TableCell>$X.XX<TableTextSecondLine>$X.XX</TableTextSecondLine></TableCell><TableCell>$X.XX<TableTextSecondLine>$X.XX</TableTextSecondLine></TableCell></TableRow></TableBody></Table>
...<th>Small<span class="table-text-second-line">Unit cost</span></th>...
Horizontal Scrolling
If you want the table to be able to scroll horizontally when there's not enough room, you can wrap the whole thing in a "table scroll container".
<TableScrollContainer><Table>// etc</Table></TableScrollContainer>
<div class="table-scroll-container"><table class="table-skin-simple"><!-- etc --></table></div>
Skin
"Comparison" skin
This table is use to compare different product variations, and provide details about them.
Style | Tri-fold Ideal for larger menus and displaying additional information like images. | Long Ideal for smaller menus, food trucks, and restaurants with a more limited selection. |
---|---|---|
Size | A standard 8.5" x 11" sheet folded three times, offering six separate panels. | A flat sheet of 3.6" x 8.5" paper. Double-sided, full-color printing available. |
Paper stock | Standard glossy, Premium glossy | Standard glossy, Premium matte, Premium recycled |
<Table skin="comparison"><TableHead><TableRow><TableCell component="th" scope="row">//etc</TableCell></TableRow></TableHead></Table>
<table class="table-skin-comparison"><thead><tr><th scope="row"><!-- etc --></th></tr></thead></table>
"Dividers" skin
The "dividers" skin puts horizontal dividers between the rows.
Qty | Price | Details |
---|---|---|
1 | $X.XX | No rats |
2 | $X.XX | No bats |
3 | $X.XX | No hats |
<Table skin="dividers"><TableCaption><Typography component="h2" textSize={4}>My table caption</Typography></TableCaption><TableHead><TableRow><TableCell>Qty</TableCell><TableCell>Price</TableCell><TableCell>Details</TableCell></TableRow></TableHead><TableBody><TableRow><TableCell>1</TableCell><TableCell>$X.XX</TableCell><TableCell>No rats</TableCell></TableRow><TableRow><TableCell>2</TableCell><TableCell>$X.XX</TableCell><TableCell>No bats</TableCell></TableRow><TableRow><TableCell>3</TableCell><TableCell>$X.XX</TableCell><TableCell>No hats</TableCell></TableRow></TableBody></Table>
Components
Table
Prop | Type | Default | Description |
---|---|---|---|
skin | "simple" | "comparison" | "dividers" | simple | The skin to apply to the table |
All other props are forwarded to the element specified in thecomponent
prop(default: <table/>
)
TableHead
TableHead
has no props of its own
All props are forwarded to the element specified in thecomponent
prop(default: <thead/>
)
TableBody
TableBody
has no props of its own
All props are forwarded to the element specified in thecomponent
prop(default: <tbody/>
)
TableRow
TableRow
has no props of its own
All props are forwarded to the element specified in thecomponent
prop(default: <tr/>
)
TableCell
Prop | Type | Default | Description |
---|---|---|---|
textAlign | "left" | "center" | "right" | "standard" | "standard" | The alignment of the text |
All other props are forwarded to the element specified in thecomponent
prop(default: <div/>
)
TableScrollContainer
TableScrollContainer
has no props of its own
All props are forwarded to the element specified in thecomponent
prop(default: <div/>
)
Guidelines
Designer Guidelines
Tables do not display well on phones. Limit the number of columns.
Developer Guidelines
A table should only be used for tabular display of data, such as a pricing table. It should not be used to lay out page content into rows and columns; use the layout grid for that.
Accessibility Guidelines
A table should only be used for tabular display of data, such as a pricing table. It should not be used for page layout!
The table's title (e.g. a heading tag) should be inside a
TableCaption
that sits as the first child of the table. Use whatever heading tag is appropriate for the page structure.If a table header (
<th>
tag) is a row header rather than a column header, be sure to includescope="row"
. If your table has both row and column headers, the column headers will needscope="col"
.
SEO Considerations
Each major table should intregrate structured data, to let Google understand what the table is about. Use this JSON Script as a model. (Contact the Organic Search team with any questions.)
<script type="application/ld+json">{"@context": "http://schema.org","@type": "Table","about": "List of Square Business Card Prices by quantity"}</script>