Tables show tabular data in columns and rows.
Table with multi headers and a caption
<Table>
Table is a container component that contains TableCaption, TableHead and TableBody as children,
as well as TableRow and TableCell for the table content. These components mostly follow ordinary HTML table semantics, but also include some additional
responsive features including horizontal scrolling and vertically stacked rows.
Scrollable Table
Code snippet
Stackable Table
Code snippet
Table Props
| Name | Type | Default | Description |
|---|---|---|---|
borderless | boolean | Applies the borderless variation of the table. | |
childrenrequired | React.ReactNode | The table contents, usually | |
className | string | Additional classes to be added to the root table element. | |
compact | boolean | Applies the compact variation of the table. | |
scrollable | boolean | Applies a horizontal scrollbar and scrollable notice on | |
scrollableNotice | React.ReactNode | <Alert className="ds-c-table__scroll-alert" role="status">
<p className="ds-c-alert__text">Scroll using arrow keys to see more</p>
</Alert> | Additional text or content to display when the horizontal scrollbar is visible to give the user notice of the scroll behavior.
This prop will only be used when the |
stackable | boolean | A stackable variation of the table.
When | |
stackableBreakpoint | 'sm' | 'md' | 'lg' | 'sm' | Applies responsive styles to vertically stacked rows at different viewport sizes. |
striped | boolean | A striped variation of the table. | |
warningDisabled | boolean | Disables the warning message on development console when a responsive stackable table cell does not contain an |
<TableCaption>
TableCaption renders the <caption> element.
Table Caption Props
| Name | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | The table caption contents. | |
className | string | Additional classes to be added to the caption element. |
<TableHead>
TableHead renders the <thead> element and will typically contain TableRow elements to define table columns.
Table Head Props
| Name | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | The table head contents, usually |
<TableBody>
TableBody renders the <tbody> element and will typically contain TableRow elements to define table data.
Table Body Props
| Name | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | The table body contents, usually |
<TableRow>
TableRow renders a <tr> element.
Table Row Props
| Name | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | The table row contents, usually |
<TableCell>
TableCell dynamically renders a <th> or <td> element based on the parent component or user specified component prop. By default TableCell will automatically render a <th> element if the parent component is TableHead, otherwise it will render a <td> element.
Table Cell Props
| Name | Type | Default | Description |
|---|---|---|---|
align | 'center' | 'left' | 'right' | 'left' | Set the text-align on the table cell content. Options: left, center, right. |
children | React.ReactNode | The table cell contents. | |
component | 'td' | 'th' | When provided, this will render the passed in component as the HTML element.
If this prop is undefined, it renders a | |
className | string | Additional classes to be added to the row element. | |
headers | string |
| |
id | string |
| |
scope | 'row' | 'col' | 'rowgroup' | 'colgroup' | If this prop is undefined, the component sets a scope attribute of | |
stackedClassName | string | Additional classes to be added to the stacked Title element. | |
stackedTitle | string | Table data cell's corresponding header title, this stacked title is displayed as the row header when a responsive table is vertically stacked. |
Guidance
When to use
- When you need tabular information, such as statistical data.
- When users need to compare sets of information.
When to consider alternatives
- Depending on the type of content, consider using other presentation formats such as definition lists or hierarchical lists.
- If you're writing out name / value pairs, there's often a more compact way compared to using a table.
Usage
- Right-align numerical data in tables. You can use the
alignprop on the TableCell to accomplish this. Right alignment makes it easier to scan and compare numerical values.
Accessibility
- Tables can have two levels of headers. Each header cell should have
scope="col"orscope="row". Learn more about the "scope" attribute. - Complex tables are tables with more than two levels of headers. Each header should be given a unique
idand each data cell should have aheadersattribute with each related header cell’sidlisted. - When adding a title to a table, include it in a
<caption>tag inside of the<table>element.
Customization
The following Sass variables can be overridden to customize Table components: