Table

Headers

Use table headers to tell users what the rows and columns represent. Use the scope attribute to help users of assistive technology distinguish between row and column headers. How tables work

Accessibility

Follow WebAIM's guidance for tables and

  • give tables captions
  • use the scope attribute to associate the data cells with the appropriate headers
  • let the browser window determine the width of the table whenever possible, to reduce horizontal scrolling

Captions

Use the <caption> element to describe a table in the same way you would use a heading. A caption helps users find, navigate and understand tables.

Responsive design

Tables too wide to display in full display with a scroll bar. This can be seen in the scoped column and row variation.

When to use this component

Use the table component to let users compare information in rows and columns.

When not to use this component

Never use the table component to layout content on a page. Instead, use the grid system.

Variations

A minimal table, with a caption

open this example in its own window

Some people in a table
Name Age
Jane Doe 1
Janet Doughnut 3
John Doe 3
Jeff Donut 7
<div class="table-responsive">
  <table class="table">
    <caption>
      Some people in a table
    </caption>

    <thead>
      <tr>
        <th scope="col">Name</th>

        <th scope="col">Age</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Jane Doe</td>

        <td>1</td>
      </tr>

      <tr>
        <td>Janet Doughnut</td>

        <td>3</td>
      </tr>

      <tr>
        <td>John Doe</td>

        <td>3</td>
      </tr>

      <tr>
        <td>Jeff Donut</td>

        <td>7</td>
      </tr>
    </tbody>
  </table>
</div>

Options to the table() macro

head: Array<HeadRow|Array<Cell>>

The rows in the table head. Each Cell must have a headerScope.

body: Array<Row|Array<Cell>>

The rows in the table body.

caption: string|null = null

The table caption.

id: string|null = null

The ID of the component's root HTML element.

classes: Array<string> = []

The classes to add to the <table> element.

Options to Cell

body: CellBody
headerScope: string|null = null

The scope for this cell's <th> tag. Setting this renders the cell as a <th>. This is required for all table head cells.

classes: Array<string> = []

The classes to add to the <td> or <th> element.

Options to Row

cells: Array<Cell|CellBody>
classes: Array<string> = []

The classes to add to the <tr> element.

Options to HeadRow

cells: Array<Cell>
classes: Array<string> = []

The classes to add to the <thead> element.

{% from 'macros/component/table.html' import table %}

{{ table({
    'caption': 'Some people in a table',
    'head': [
        [
            {
                'body': 'Name',
                'headerScope': 'col'
            },
            {
                'body': 'Age',
                'headerScope': 'col'
            }
        ]
    ],
    'body': [
        [
            'Jane Doe',
            '1'
        ],
        [
            'Janet Doughnut',
            '3'
        ],
        [
            'John Doe',
            '3'
        ],
        [
            'Jeff Donut',
            '7'
        ]
    ]
}) }}

A table with a scoped column and row headers

open this example in its own window

Some people in a table
Name Age Birth Day Birth Month Siblings Cousins Colour Shape Animal Hair
Jane Doe 1 13 June 2 4 Blue Square Cat Blonde
Janet Doughnut 3 23 October 0 1 Green Triangle Fish Red
John Doe 3 5 January 1 12 Yellow Circle Bird Brown
Jeff Donut 7 15 May 3 6 Purple Rectangle Tortoise Black
<div class="table-responsive">
  <table class="table">
    <caption>
      Some people in a table
    </caption>

    <thead>
      <tr>
        <th scope="col">Name</th>

        <th scope="col">Age</th>

        <th scope="col">Birth Day</th>

        <th scope="col">Birth Month</th>

        <th scope="col">Siblings</th>

        <th scope="col">Cousins</th>

        <th scope="col">Colour</th>

        <th scope="col">Shape</th>

        <th scope="col">Animal</th>

        <th scope="col">Hair</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Jane Doe</th>

        <td>1</td>

        <td>13</td>

        <td>June</td>

        <td>2</td>

        <td>4</td>

        <td>Blue</td>

        <td>Square</td>

        <td>Cat</td>

        <td>Blonde</td>
      </tr>

      <tr>
        <th scope="row">Janet Doughnut</th>

        <td>3</td>

        <td>23</td>

        <td>October</td>

        <td>0</td>

        <td>1</td>

        <td>Green</td>

        <td>Triangle</td>

        <td>Fish</td>

        <td>Red</td>
      </tr>

      <tr>
        <th scope="row">John Doe</th>

        <td>3</td>

        <td>5</td>

        <td>January</td>

        <td>1</td>

        <td>12</td>

        <td>Yellow</td>

        <td>Circle</td>

        <td>Bird</td>

        <td>Brown</td>
      </tr>

      <tr>
        <th scope="row">Jeff Donut</th>

        <td>7</td>

        <td>15</td>

        <td>May</td>

        <td>3</td>

        <td>6</td>

        <td>Purple</td>

        <td>Rectangle</td>

        <td>Tortoise</td>

        <td>Black</td>
      </tr>
    </tbody>
  </table>
</div>

Options to the table() macro

head: Array<HeadRow|Array<Cell>>

The rows in the table head. Each Cell must have a headerScope.

body: Array<Row|Array<Cell>>

The rows in the table body.

caption: string|null = null

The table caption.

id: string|null = null

The ID of the component's root HTML element.

classes: Array<string> = []

The classes to add to the <table> element.

Options to Cell

body: CellBody
headerScope: string|null = null

The scope for this cell's <th> tag. Setting this renders the cell as a <th>. This is required for all table head cells.

classes: Array<string> = []

The classes to add to the <td> or <th> element.

Options to Row

cells: Array<Cell|CellBody>
classes: Array<string> = []

The classes to add to the <tr> element.

Options to HeadRow

cells: Array<Cell>
classes: Array<string> = []

The classes to add to the <thead> element.

{% from 'macros/component/table.html' import table %}

{{ table({
    'caption': 'Some people in a table',
    'head': [
        [
            {
                'body': 'Name',
                'headerScope': 'col'
            },
            {
                'body': 'Age',
                'headerScope': 'col'
            },
            {
                'body': 'Birth Day',
                'headerScope': 'col'
            },
            {
                'body': 'Birth Month',
                'headerScope': 'col'
            },
            {
                'body': 'Siblings',
                'headerScope': 'col'
            },
            {
                'body': 'Cousins',
                'headerScope': 'col'
            },
            {
                'body': 'Colour',
                'headerScope': 'col'
            },
            {
                'body': 'Shape',
                'headerScope': 'col'
            },
            {
                'body': 'Animal',
                'headerScope': 'col'
            },
            {
                'body': 'Hair',
                'headerScope': 'col'
            }
        ]
    ],
    'body': [
        [
            {
                'body': 'Jane Doe',
                'headerScope': 'row'
            },
            '1',
            '13',
            'June',
            '2',
            '4',
            'Blue',
            'Square',
            'Cat',
            'Blonde'
        ],
        [
            {
                'body': 'Janet Doughnut',
                'headerScope': 'row'
            },
            '3',
            '23',
            'October',
            '0',
            '1',
            'Green',
            'Triangle',
            'Fish',
            'Red'
        ],
        [
            {
                'body': 'John Doe',
                'headerScope': 'row'
            },
            '3',
            '5',
            'January',
            '1',
            '12',
            'Yellow',
            'Circle',
            'Bird',
            'Brown'
        ],
        [
            {
                'body': 'Jeff Donut',
                'headerScope': 'row'
            },
            '7',
            '15',
            'May',
            '3',
            '6',
            'Purple',
            'Rectangle',
            'Tortoise',
            'Black'
        ]
    ]
}) }}