Tables

Spruce ships with some basic table styling targeted only with classes to opt-in.

Default Style

Use the table class on an HTML <table> element to use the default styling.

A generic introduction to the table.
IDNameBlock of Text
1John DoeNullam quis risus eget urna mollis ornare vel eu leo.
2Jane DoeVestibulum id ligula porta felis euismod semper. Donec ullamcorper nulla non metus auctor fringilla.
<table class="table">
    <caption>A generic introduction to the table.</caption>
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Block of Text</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="row">1</th>
            <td>John Doe</td>
            <td>Nullam quis risus eget urna mollis ornare vel eu leo.</td>
        </tr>
    </tbody>
</table>

Use scope attribute to define which header relates with which table cells.

Striped and Hover Styles

Use the table--striped class on a <table> to display a zebra-striped styling. To add hover style to the table rows, use the table--hover class.

You control the stripe pattern with the config('stripe', $table) variable (odd, even).

IDNameNumber
1John Doe900
2Jane Doe1200
3Jim Doe1200
<table class="table table--striped table--hover">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Number</th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

Clear Style

Use the table--clear-border class on a <table> to remove the default border style.

IDNameNumber
1John Doe900
2Jane Doe1200
<table class="table table--clear-border">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Number</th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

Small Style

Use the table--sm class on a <table> to make it smaller.

IDNameNumber
1John Doe900
2Jane Doe1200
<table class="table table--sm">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Number</th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

Rounded Style

Use the table--rounded class on a <table> to make rounded background. For the best result use it with table--striped and table--clear-border.

IDNameNumber
1John Doe900
2Jane Doe1200
3Jack Doe200
4Jill Doe1400
<table class="table table--rounded table--striped table--clear-border">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Number</th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

In Line Style

Use the table--in-line class on a <table> to remove the inline padding from the first (on start) and last (on end) <th> and <td> element.

IDNameNumber
1John Doe900
2Jane Doe1200
3Jack Doe200
4Jill Doe1400
<table class="table table--in-line">
    <thead>
        <tr>
            <th scope="col">ID</th>
            <th scope="col">Name</th>
            <th scope="col">Number</th>
        </tr>
    </thead>
    <tbody>
        ...
    </tbody>
</table>

Bordered Style

New in v3. Use the table--bordered class to draw a border around every cell.

PlanSeatsDescription
Starter1For side projects.
Team10For growing teams that ship every week, with priority support and advanced analytics.
EnterpriseUnlimitedFor organizations.
<table class="table table--bordered">
    ...
</table>

Vertically Centered

New in v3. Browsers center the table cells vertically by default. Many designs align them to the top for long content (set 'vertical-align': top in the $table map); in that case, use table--middle to center the cells of a specific table.

PlanSeatsDescription
Starter1For side projects.
Team10For growing teams that ship every week, with priority support and advanced analytics.
EnterpriseUnlimitedFor organizations.
<table class="table table--middle">
    ...
</table>

New in v3. Use the table--sticky-header class inside a scrolling container to keep the header row visible. The header gets the heading-background table color (or the page background).

NameValueNote
Row 110Scroll the table to see the sticky header.
Row 220Scroll the table to see the sticky header.
Row 330Scroll the table to see the sticky header.
Row 440Scroll the table to see the sticky header.
Row 550Scroll the table to see the sticky header.
Row 660Scroll the table to see the sticky header.
Row 770Scroll the table to see the sticky header.
Row 880Scroll the table to see the sticky header.
Row 990Scroll the table to see the sticky header.
Row 10100Scroll the table to see the sticky header.
<div style="max-block-size: 20rem; overflow: auto;">
    <table class="table table--sticky-header">
        ...
    </table>
</div>

Responsive, Scrollable Table

Adding the <div class="table-responsive"></div> as a wrapper element you can make the tables horizontally scrollable.

You can control the width of the scrollable table with the config('responsive-inline-size', $table) globally and locally with the --inline-size custom property.

<div class="table-responsive">
    <table class="table table--striped table--hover">
        <thead>
            ...
        </thead>
        <tbody>
            ...
        </tbody>
    </table>
</div>
<div class="table-responsive" style="--inline-size: 20rem;">
    <table class="table">
        ...
    </table>
</div>

Customization

You can configure the table’s look through the $table map and the table colors:

@use 'sprucecss/scss/spruce' with (
  $colors: (
    'table': (
      'heading-background': hsl(220deg 20% 96%),
    ),
  ),
  $table: (
    'border-width': 1px,
    'caption-text-align': start,
    'heading-font-size': 0.75rem,
    'heading-font-weight': 700,
    'heading-letter-spacing': 0.05em,
    'heading-text-transform': uppercase,
    'vertical-align': middle,
  ),
);

Generate Your Own

Use the generate-table mixin to apply the table styles to another selector, like every table in your content:

@include generate-table('.post-content table', $has-variations: false, $has-responsive-table: false);

Documentation

Learn about Spruce CSS through our extensive documentation.

Components

Explore our UI library of accessible components built with Spruce CSS.

Blocks and Pages

Full-width page blocks and complete page templates, ready to copy.

Find us on GitHub

Did you find a bug? Have an idea or a question? Please open an issue to help us develop the project.