# Table

## Usage

To apply this component, add the `.z-table` class to a `<table>` element.

```html
<table class="z-table">
  <caption></caption>
  <thead>
    <tr>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td></td>
    </tr>
  </tfoot>
</table>
```

```html
<table class="z-table">
  <caption>
    Table Caption
  </caption>
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Table Footer</td>
      <td>Table Footer</td>
      <td>Table Footer</td>
    </tr>
  </tfoot>
</table>
```

## Divider modifier

Add the `.z-table-divider` class to display a divider between table rows.

```html
<table class="z-table z-table-divider">
  <!-- ... -->
</table>
```

```html
<table class="z-table z-table-divider">
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
</table>
```

## Striped modifier

Add zebra-striping to a table by adding the `.z-table-striped` class.

```html
<table class="z-table z-table-striped">
  <!-- ... -->
</table>
```

```html
<table class="z-table z-table-striped">
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
</table>
```

## Hover modifier

Add the `.z-table-hover` class to display a hover state on table rows.

```html
<table class="z-table z-table-hover">
  <!-- ... -->
</table>
```

```html
<table class="z-table z-table-divider z-table-hover">
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
</table>
```

## Size modifiers

Add the `.z-table-small` or `.z-table-large` class to a table to make it smaller or larger.

```html
<table class="z-table z-table-small">
  <!-- ... -->
</table>
```

```html
<table class="z-table-sm z-table z-table-divider">
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
</table>

<table class="z-table-large z-table z-table-divider mt" style="--mt: 4">
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
</table>
```

## Justify modifier

To remove the outer padding of the first and last columns so that they are flush with the table, add the `.z-table-justify` class.

```html
<table class="z-table z-table-justify">
  <!-- ... -->
</table>
```

```html
<table class="z-table z-table-divider z-table-justify">
  <thead>
    <tr>
      <th class="w-40">Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua.
      </td>
      <td>
        <button class="z-button z-button-default" type="button">Button</button>
      </td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua.
      </td>
      <td>
        <button class="z-button z-button-default" type="button">Button</button>
      </td>
    </tr>
  </tbody>
</table>
```

## Alignment modifier

To vertically center table content, just add the `.z-table-middle` class. You can also apply the class to `<tr>` or `<td>` elements for a more specific selection.

```html
<table class="z-table z-table-middle">
  <!-- ... -->
</table>
```

```html
<table class="z-table z-table-middle z-table-divider">
  <thead>
    <tr>
      <th class="w-40">Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua.
      </td>
      <td>
        <button class="z-button z-button-default" type="button">Button</button>
      </td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua.
      </td>
      <td>
        <button class="z-button z-button-default" type="button">Button</button>
      </td>
    </tr>
  </tbody>
</table>
```

## Responsive table

If your table happens to be wider than its container element or would eventually get too big on a specific viewport width, just wrap it inside a `<div>` element and add the `.z-overflow-auto` class. This creates a container that provides a horizontal scrollbar whenever the elements inside it are wider than the container itself.

```html
<div class="z-overflow-auto">
  <table class="z-table">
    <!-- ... -->
  </table>
</div>
```

```html
<div class="z-overflow-auto">
  <table class="z-table-small z-table z-table-divider">
    <thead>
      <tr>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
        <th>Table Heading</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
      </tr>
      <tr>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
      </tr>
      <tr>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
        <td>Table Data</td>
      </tr>
    </tbody>
  </table>
</div>
```

### Stack on small viewports

If you want table columns to stack on small screens, add the `.z-table-responsive` class.

```html
<table class="z-table z-table-responsive">
  <!-- ... -->
</table>
```

```html
<table
  class="z-table z-table-divider z-table-responsive z-table-responsive-stack"
>
  <thead>
    <tr>
      <th>Table Heading</th>
      <th>Table Heading</th>
      <th>Table Heading</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Product Name">Table Data</td>
      <td data-label="Product Name">Table Data</td>
      <td data-label="Product Name">Table Data</td>
    </tr>
    <tr>
      <td data-label="Product Name">Table Data</td>
      <td data-label="Product Name">Table Data</td>
      <td data-label="Product Name">Table Data</td>
    </tr>
    <tr>
      <td data-label="Product Name">Table Data</td>
      <td data-label="Product Name">Table Data</td>
      <td data-label="Product Name">Table Data</td>
    </tr>
  </tbody>
</table>
```

Please resize the browser window to see the columns stack.

<span class="z-label z-label-primary">Note</span> To improve mobile readability, add `data-label="Your Label"` to table cells to show header info next to each data value. For full details, see [PR #95](https://github.com/franken-ui/ui/pull/95).

## Column width modifier

To modify the column width or content, you can use one of the following classes. You just need to add this class to one of the column cells. It's recommended on the header cell.

| Class               | Description                                                                                                                |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `.z-table-shrink ` | Add this class to a `<th>` or `<td>` element to reduce the column width to fit its content.                                |
| `.z-table-expand`  | Add this class to a `<th>` or `<td>` element to expand the column width to fill the remaining space and apply a min-width. |
| `.w-*`              | Add one of the width utility classes to a `<th>` or `<td>` element to modify the column width.           |

```html
<th class="z-table-shrink"></th>
```

```html
<table class="z-table z-table-divider">
  <thead>
    <tr>
      <th class="z-table-shrink">Shrink</th>
      <th class="z-table-expand">Expand</th>
      <th class="w-40">Width Small</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
    <tr>
      <td>Table Data</td>
      <td>Table Data</td>
      <td>Table Data</td>
    </tr>
  </tbody>
</table>
```

## Utilities

To optimize the display of table cells and their specific content, you can add one of the following classes.

| Class                 | Description                                                                                                                                                                                                                                   |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `.z-table-link`      | To link an entire cell, add this class to a `<th>`or `<td>` element and insert an `<a>` element. Add the `.z-link-reset`class from the [Link component](/docs/latest/kit/link) to reset the default link styling.                                   |
| `.z-preserve-width ` | Since images are responsive by default in UIkit, using an image inside a table cell with the `.z-table-shrink` class would reduce the image width to 0. To prevent this behavior, add the `.z-preserve-width` class to the `<img>` element. |
| `.z-text-truncate`   | When applying a fixed width to a table cell, you might want to add this class to the `<td>` element to truncate the text.                                                                                                                     |
| `.text-nowrap`        | Add this class to keep text from wrapping, for example when using the `.z-table-shrink` class.                                                                                                                         |

```html
<!-- Table link -->
<td class="z-table-link">
  <a class="z-link-reset" href=""></a>
</td>

<!-- Preserve width -->
<td>
  <img class="z-preserve-width" src="" width="" height="" alt="" />
</td>

<!-- Text truncate -->
<td class="z-text-truncate"></td>

<!-- Text nowrap -->
<td class="z-text-nowrap"></td>
```

```html
<div class="z-overflow-auto">
  <table class="z-table z-table-middle z-table-divider z-table-hover">
    <thead>
      <tr>
        <th class="z-table-shrink"></th>
        <th class="z-table-shrink">Preserve</th>
        <th class="z-table-expand">Expand + Link</th>
        <th class="w-40">Truncate</th>
        <th class="z-table-shrink text-nowrap">Shrink + Nowrap</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <input class="z-checkbox" type="checkbox" aria-label="Checkbox" />
        </td>
        <td>
          <img
            class="z-preserve-width rounded-full"
            src="/images/avatar.jpg"
            width="40"
            height="40"
            alt=""
          />
        </td>
        <td class="z-table-link">
          <a class="z-link-reset" href=""
            >Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
            eiusmod tempor.</a
          >
        </td>
        <td class="z-text-truncate">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          eiusmod tempor.
        </td>
        <td class="text-nowrap">Lorem ipsum dolor</td>
      </tr>
      <tr>
        <td>
          <input class="z-checkbox" type="checkbox" aria-label="Checkbox" />
        </td>
        <td>
          <img
            class="z-preserve-width rounded-full"
            src="/images/avatar.jpg"
            width="40"
            height="40"
            alt=""
          />
        </td>
        <td class="z-table-link">
          <a class="z-link-reset" href=""
            >Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
            eiusmod tempor.</a
          >
        </td>
        <td class="z-text-truncate">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          eiusmod tempor.
        </td>
        <td class="text-nowrap">Lorem ipsum dolor</td>
      </tr>
      <tr>
        <td>
          <input class="z-checkbox" type="checkbox" aria-label="Checkbox" />
        </td>
        <td>
          <img
            class="z-preserve-width rounded-full"
            src="/images/avatar.jpg"
            width="40"
            height="40"
            alt=""
          />
        </td>
        <td class="z-table-link">
          <a class="z-link-reset" href=""
            >Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
            eiusmod tempor.</a
          >
        </td>
        <td class="z-text-truncate">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          eiusmod tempor.
        </td>
        <td class="text-nowrap">Lorem ipsum dolor</td>
      </tr>
      <tr>
        <td>
          <input class="z-checkbox" type="checkbox" aria-label="Checkbox" />
        </td>
        <td>
          <img
            class="z-preserve-width rounded-full"
            src="/images/avatar.jpg"
            width="40"
            height="40"
            alt=""
          />
        </td>
        <td class="z-table-link">
          <a class="z-link-reset" href=""
            >Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
            eiusmod tempor.</a
          >
        </td>
        <td class="z-text-truncate">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          eiusmod tempor.
        </td>
        <td class="text-nowrap">Lorem ipsum dolor</td>
      </tr>
    </tbody>
  </table>
</div>
```