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 example.

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.

Examples

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 class="">
        <th class="" scope="col">Name</th>

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

        <td class="">1</td>
      </tr>

      <tr class="">
        <td class="">Janet Doughnut</td>

        <td class="">3</td>
      </tr>

      <tr class="">
        <td class="">John Doe</td>

        <td class="">3</td>
      </tr>

      <tr class="">
        <td class="">Jeff Donut</td>

        <td class="">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 class="">
        <th class="" scope="col">Name</th>

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

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

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

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

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

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

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

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

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

        <td class="">1</td>

        <td class="">13</td>

        <td class="">June</td>

        <td class="">2</td>

        <td class="">4</td>

        <td class="">Blue</td>

        <td class="">Square</td>

        <td class="">Cat</td>

        <td class="">Blonde</td>
      </tr>

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

        <td class="">3</td>

        <td class="">23</td>

        <td class="">October</td>

        <td class="">0</td>

        <td class="">1</td>

        <td class="">Green</td>

        <td class="">Triangle</td>

        <td class="">Fish</td>

        <td class="">Red</td>
      </tr>

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

        <td class="">3</td>

        <td class="">5</td>

        <td class="">January</td>

        <td class="">1</td>

        <td class="">12</td>

        <td class="">Yellow</td>

        <td class="">Circle</td>

        <td class="">Bird</td>

        <td class="">Brown</td>
      </tr>

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

        <td class="">7</td>

        <td class="">15</td>

        <td class="">May</td>

        <td class="">3</td>

        <td class="">6</td>

        <td class="">Purple</td>

        <td class="">Rectangle</td>

        <td class="">Tortoise</td>

        <td class="">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'
        ]
    ]
}) }}