Skip to the main content

Sortable and Filterable Table

FreeUses XON Data Display

A real table whose headers sort, with a text filter, five rows a page and a status line. The rows are plain HTML. Written with XON's table part.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="sdt" aria-labelledby="sdt-title">
  <div class="sdt__head">
    <h2 class="sdt__title" id="sdt-title">Example trails</h2>
    <div class="sdt__search">
      <label class="sdt__label" for="sdt-filter">Filter trails</label>
      <input class="sdt__input" id="sdt-filter" type="search" placeholder="Filter trails, try &ldquo;ridge&rdquo;" autocomplete="off" data-table-filter="trails" data-wait="150">
    </div>
  </div>

  <div class="sdt__scroll">
    <table class="sdt__table" data-table="trails" data-page-size="4">
      <caption class="sdt__caption">Twelve sample trails. Example data.</caption>
      <thead>
        <tr>
          <th data-sort="trail">Trail</th>
          <th data-sort="km" data-type="number">Distance</th>
          <th data-sort="climb" data-type="number">Climb</th>
          <th data-sort="added" data-type="date">Added</th>
        </tr>
      </thead>
      <tbody>
          <tr><th scope="row">Ash Hollow loop</th><td>4.2 km</td><td>120 m</td><td data-value="2025-03-14">14 Mar 2025</td></tr>
          <tr><th scope="row">Birch Ridge</th><td>9.8 km</td><td>410 m</td><td data-value="2025-01-22">22 Jan 2025</td></tr>
          <tr><th scope="row">Cedar Brook path</th><td>3.1 km</td><td>45 m</td><td data-value="2025-06-02">2 Jun 2025</td></tr>
          <tr><th scope="row">Dune Crossing</th><td>6.5 km</td><td>80 m</td><td data-value="2024-11-09">9 Nov 2024</td></tr>
          <tr><th scope="row">Elm Gap climb</th><td>7.4 km</td><td>520 m</td><td data-value="2025-04-18">18 Apr 2025</td></tr>
          <tr><th scope="row">Fern Gully walk</th><td>2.6 km</td><td>30 m</td><td data-value="2025-08-27">27 Aug 2025</td></tr>
          <tr><th scope="row">Gorse Common</th><td>5.3 km</td><td>95 m</td><td data-value="2024-09-30">30 Sep 2024</td></tr>
          <tr><th scope="row">Heron Marsh</th><td>8.0 km</td><td>15 m</td><td data-value="2025-05-12">12 May 2025</td></tr>
          <tr><th scope="row">Ivy Quarry</th><td>11.2 km</td><td>360 m</td><td data-value="2025-02-05">5 Feb 2025</td></tr>
          <tr><th scope="row">Juniper Edge</th><td>6.9 km</td><td>240 m</td><td data-value="2025-07-19">19 Jul 2025</td></tr>
          <tr><th scope="row">Kestrel Point</th><td>10.4 km</td><td>300 m</td><td data-value="2025-09-03">3 Sep 2025</td></tr>
          <tr><th scope="row">Larch Wood circuit</th><td>5.9 km</td><td>150 m</td><td data-value="2024-12-14">14 Dec 2024</td></tr>
      </tbody>
    </table>
  </div>

  <div class="sdt__foot">
    <p class="sdt__status" data-table-status="trails"></p>
    <nav class="sdt__pager" data-table-pager="trails" aria-label="Trail pages"></nav>
  </div>
</section>

CSS

.sdt {
  --sdt-bg: #ffffff;
  --sdt-tint: #f3f4fb;
  --sdt-ink: #1c2033;
  --sdt-muted: #4b5266;
  --sdt-line: #d5d9e6;
  --sdt-accent: #4338ca;
  --sdt-on-accent: #ffffff;
  --sdt-focus: #1c2033;
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  max-width: 40rem;
  padding: 1.25rem;
  color: var(--sdt-ink);
  background: var(--sdt-bg);
  border: 1px solid var(--sdt-line);
  border-radius: 16px;
  box-shadow: 0 1px 2px rgba(28, 32, 51, 0.06), 0 10px 28px -14px rgba(28, 32, 51, 0.22);
}
.sdt *, .sdt *::before, .sdt *::after { box-sizing: inherit; }

.sdt__head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.6rem 1rem; margin-bottom: 0.9rem; }
.sdt__title { margin: 0; font-size: 1.35rem; line-height: 1.2; letter-spacing: -0.01em; }
.sdt__search { flex: 1 1 11rem; max-width: 16rem; }
.sdt__label { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.sdt__input {
  display: block; width: 100%; min-height: 2.5rem; padding: 0.4rem 0.75rem;
  font: inherit; font-size: 0.95rem; color: var(--sdt-ink); background: var(--sdt-bg);
  border: 1.5px solid var(--sdt-muted); border-radius: 10px;
}

.sdt__scroll { overflow-x: auto; border: 1px solid var(--sdt-line); border-radius: 12px; }
.sdt__table { width: 100%; border-collapse: collapse; font-size: 0.92rem; font-variant-numeric: tabular-nums; }
.sdt__caption { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.sdt__table thead th { padding: 0; text-align: left; background: var(--sdt-tint); border-bottom: 1px solid var(--sdt-line); }
.sdt__table th button {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; width: 100%;
  min-height: 2.75rem; padding: 0.4rem 0.75rem;
  font: inherit; font-size: 0.82rem; font-weight: 700; letter-spacing: 0.01em; text-align: left;
  color: var(--sdt-ink); background: none; border: 0; cursor: pointer;
}
.sdt__table th button:hover { color: var(--sdt-accent); }
.sdt__table th[aria-sort="ascending"] button, .sdt__table th[aria-sort="descending"] button { color: var(--sdt-accent); }
.sdt__table tbody th, .sdt__table tbody td { padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--sdt-line); text-align: left; }
.sdt__table tbody th { font-weight: 650; }
.sdt__table tbody tr:last-child th, .sdt__table tbody tr:last-child td { border-bottom: 0; }
.sdt__table tbody tr:hover { background: var(--sdt-tint); }
.sdt__table td:nth-child(2), .sdt__table td:nth-child(3), .sdt__table td:nth-child(4) { white-space: nowrap; }

/* Sort arrows: two small triangles, the active one is solid */
.sdt [data-table-arrow] { flex: none; font-size: 0; display: inline-flex; flex-direction: column; gap: 2px; }
.sdt [data-table-arrow]::before, .sdt [data-table-arrow]::after { content: ""; width: 0.6rem; height: 0.3rem; background: currentColor; opacity: 0.35; }
.sdt [data-table-arrow]::before { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.sdt [data-table-arrow]::after { clip-path: polygon(0 0, 100% 0, 50% 100%); }
.sdt th[aria-sort="ascending"] [data-table-arrow]::before, .sdt th[aria-sort="descending"] [data-table-arrow]::after { opacity: 1; }

.sdt__foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem 1rem; margin-top: 1rem; }
.sdt__status { margin: 0; color: var(--sdt-muted); font-size: 0.85rem; }
.sdt__pager { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.sdt__pager button {
  min-width: 2.75rem; min-height: 2.75rem; padding: 0 0.7rem;
  font: inherit; font-size: 0.9rem; font-weight: 600; font-variant-numeric: tabular-nums; cursor: pointer;
  color: var(--sdt-ink); background: var(--sdt-bg); border: 1px solid var(--sdt-line); border-radius: 10px;
}
.sdt__pager button:hover { background: var(--sdt-tint); border-color: var(--sdt-accent); color: var(--sdt-accent); }
.sdt__pager [aria-current="page"] { color: var(--sdt-on-accent); background: var(--sdt-accent); border-color: var(--sdt-accent); }
.sdt__pager [aria-current="page"]:hover { color: var(--sdt-on-accent); background: var(--sdt-accent); }
.sdt__pager [aria-disabled="true"] { color: var(--sdt-muted); cursor: default; background: var(--sdt-tint); }
.sdt__pager [aria-disabled="true"]:hover { color: var(--sdt-muted); border-color: var(--sdt-line); background: var(--sdt-tint); }

.sdt__input:focus-visible, .sdt button:focus-visible { outline: 3px solid var(--sdt-focus); outline-offset: 2px; }
.sdt__table th button:focus-visible { outline-offset: -3px; }

@media (max-width: 30rem) {
  .sdt { padding: 0.75rem; }
  .sdt__search { max-width: none; }
  .sdt__table { font-size: 0.8rem; }
  .sdt__table td:nth-child(4) { white-space: normal; }
  .sdt__table th button, .sdt__table tbody th, .sdt__table tbody td { padding-inline: 0.4rem; }
  .sdt__table th button { font-size: 0.75rem; gap: 0.25rem; }
  .sdt__pager button { min-width: 2.75rem; padding: 0 0.5rem; }
}
@media (prefers-color-scheme: dark) {
  .sdt {
    --sdt-bg: #1c2033;
    --sdt-tint: #272c45;
    --sdt-ink: #eceef8;
    --sdt-muted: #b3b9d0;
    --sdt-line: #3d4466;
    --sdt-accent: #b4b9ff;
    --sdt-on-accent: #14172a;
    --sdt-focus: #ffffff;
    box-shadow: none;
  }
}
.sdt__input::placeholder { color: var(--sdt-muted); opacity: 1; }

JavaScript

(function () {
  'use strict';

  // The table part sorts, filters and pages the rows that are already in the page.
  // These lines only put its messages into friendlier words.
  xon.part('table').then(function (table) {
    table.words({
      previous: 'Prev',
      status: 'Page {0} of {1}. {2} trails.',
      statusFiltered: 'Page {0} of {1}. {2} of {3} trails match.',
      emptyFiltered: 'No trail matches that filter.'
    });
  });
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • Sortable headers hold real buttons and the sorted one has aria-sort.
  • The table has a caption and every row starts with a row header.
  • The status line is a polite live region and the pager is a labelled nav.
Data Display
Free

Feature Icon

Beautiful icon containers for features

  • HTML
  • CSS
  • Tailwind
  • Font Awesome
Data Display