# RWD Tables

Home Search

## Responsive tables

One of the biggest challenges in responsive web design (RWD) is presenting tabular data. Large tables with lots of columns don't fit on smaller screens and there isn't a simple way to re-format the table content with CSS and media queries for an acceptable presentation. To address this, the framework offers two different options for presenting tables responsively. Each has benefits and tradeoffs, the right choice will depend on the data being presented.

Reflow mode - Re-formats the table columns at narrow widths so each row of data is presented as a formatted block of label/data pairs. This is ideal for tables with product or contact information with more complex or lengthy data formatting that doesn't need comparison across rows of data.

Column toggle mode - Selectively hides columns at narrower widths as a sensible default but also offers a menu to let users manually control which columns they want to see. This mode is better for financial data tables that have compact values and need to maintain comparisons across columns and rows of data. It can also be used for building things like product comparison tables.

The responsive table feature is built with a core table plugin (`table.js`) that initializes when the `data-role="table"` attribute is added to the markup. This plugin is very lightweight and adds `ui-table` class, parses the table headers and generates information on the columns of data, and fires a `tablecreate` event. Both the table modes, reflow and column toggle, are written as extensions to the table widget that hook in via the `create` event to add the additional behaviors that make the tables responsive. Reflow is the default mode so if the extension is present, it will be applied automatically if the `data-role="table"` attribute is on the table.

If only one mode is used on a project, the download builder tool can be used to package only the table plugin and the single extension to save code weight.

## General table markup guidelines

Here is the basic table markup you should use for both table modes:

    <table data-role="table" id="my-table" data-mode="reflow">
      <thead>
        <tr>
          <th>Rank</th>
          <th>Movie Title</th>
          <th>Year</th>
          <th><abbr title="Rotten Tomato Rating">Rating</abbr></th>
          <th>Reviews</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>1</th>
          <td><a href="foo.com" data-rel="external">Citizen Kane</a></td>
          <td>1941</td>
          <td>100%</td>
          <td>74</td>
        </tr>
        </tbody>
    </table>

Both table modes start with standard HTML table markup but there are some specific guidelines you must follow for the responsive table to work correctly:

- Follow standard HTML table markup guidelines for proper semantics
- Do not use `rowspan` or `colspan` in your tables, these aren't supported except for grouped headers (see below)
- Adding `thead` and `tbody` elements are optional but provide improved semantics
- Assign a unique `ID` to the table (required for the column toggle mode)
- Add the `data-role="table"` to apply the responsive table plugin
- The default table mode is `reflow`, add `data-mode="columntoggle"` change modes
- The first row of the table must contain the table headers, be sure to use `TH` instead of `TD` tags
- To display longer table header text in the column chooser or reflow labels, wrap the text in the `TH` with a `abbr` element and set the `title`. This string will be used in place.

## Styling and theming tables

The responsive table plugin is as minimally styled as possible to give you a clean slate for your designs. The plugin focuses primarily on the difficult scripting elements: generating the labels for the reflow table and creating the button and column chooser popup. Out of the box, the table just has a few basic style rules to add a bit of padding and set the vertical alignment of cells to be top left for visual consistency.

The table will adapt to whatever content block it sits on, but there isn't an explicit theming attribute for this widget. We did this because it's simple enough to add theme classes like `ui-body-a` to individual cells if a more heavily themed look is wanted.

### Row strokes

To add horizontal lines between each row, add a custom style to your stylesheet that adds a border to the table headers and cells. In this example, RGBA is used to set a color (black) at an opacity of 5% so this will work on any background color:

    .table-stroke thead th {
      border-bottom: 1px solid rgba(0, 0, 0, .1);
    }
    .table-stroke tbody th,
    .table-stroke tbody td {
      border-bottom: 1px solid rgba(0, 0, 0, .05);
    }

This standard table stroke style can be also applied by adding `table-stroke` `class` to the `table` element. If you prefer a custom stroke style, use the example above as a starting point. Note that adding styles that set a color for the stroke will override a theme class so do not use these together.

### Row stripes

To add alternating row stripes between each row, add a custom style to your stylesheet that sets the color for each row using the `tr:nth-child(odd)` or `tr:nth-child(even)` selector.

    /* Add alternating row stripes */
    .table-stripe tbody tr:nth-child(odd) td,
    .table-stripe tbody tr:nth-child(odd) th {
      background-color: rgba(0,0,0,0.04);
    }
    /* Add stroke to the header and last item */
    .table-stripe thead th,
    .table-stripe tbody tr:last-child {
      border-bottom: 1px solid rgba(0, 0, 0, .1);
    }

This standard alternating row stripe style can be also applied by adding `table-stripe` `class` to the `table` element. If you prefer a custom stroke style, use the example above as a starting point. Note that adding styles that set a color for the stroke will override a theme class so do not use these together.

### In this section

- Tables
- Reflow mode
- Column toggle mode