Skip to content

Sortable Data Table

Tailwind CSS data table with clickable column headers that sort text, dates and numbers, with aria-sort, arrow indicators and a responsive scroll wrapper.

Also known as: sortable table, sortable data grid, table column sorting, sort table by column, clickable table headers

Tailwind CSS Tables Responsive #accessible #data #responsive #sort #table

Sortable Data Table live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A sortable data table built with Tailwind CSS and a dependency-free JavaScript sorter. Clicking a column header sorts the rows by text, date or number, a second click reverses the order, and arrow icons plus aria-sort show the current state. It suits order lists, admin dashboards and reports.

How it works

The root is .tst-table. The table sits inside a horizontally scrollable wrapper (overflow-x-auto with min-w-[640px] on the table), so on phones the table scrolls instead of squeezing columns. Each th carries data-tst-type (text, date or number) and an aria-sort attribute, and contains a full-width .tst-sort button with a two-part .tst-arrow SVG.

On DOMContentLoaded the script initializes every .tst-table. A click on a header button reads its current aria-sort and toggles between ascending and descending; an unsorted number column starts descending (largest first), other types start ascending. The sortBy() function reads each row's cell value, preferring a data-tst-value attribute over the visible text, so formatted values like $12,980.00 or Oct 3, 2026 sort correctly. Text is compared with Intl.Collator, and the original row position breaks ties so the sort stays stable. Rows are moved back into the tbody in one DocumentFragment, all other headers are reset to aria-sort="none", and a short tst-flash highlight plays on the rows. Any header that starts with aria-sort="ascending" or "descending" in the markup is applied as the initial sort.

style.css dims the inactive arrow half (.tst-up / .tst-down) and darkens the sorted header.

Customization

  • data-tst-type on a th: set number, date or text to control how that column is compared.
  • data-tst-value on a td: provide a raw sort value (ISO date, plain number) when the displayed text is formatted.
  • aria-sort="descending" on the Date header: move it to another th to change the initial sort, or set all to none to keep the source order.
  • Remove the .tst-sort button and the aria-sort attribute from a th (keep plain text) to make that column non-sortable.
  • bg-violet-600 on thead and bg-violet-50 on .tst-table: swap violet for another Tailwind color to retheme.
  • min-w-[640px] on the table: lower it if your columns are narrow, raise it for wide data.
  • The status.textContent line in sortBy(): change or translate the announcement text.

Use cases

  • Order lists: staff click Date or Amount headers to reorder recent orders, and arrows show the sort direction
  • Admin dashboards: sort records by text, date or number without a library, with aria-sort kept up to date
  • Reports and statistics: formatted values such as dollar amounts and dates sort correctly using raw data values
  • Mobile layouts: the table scrolls horizontally inside a wrapper instead of squeezing columns on phones

Accessibility

Headers use scope="col", and the sort controls are real buttons inside the th, so they are reachable with Tab and activated with Enter or Space. aria-sort is kept up to date on every header, which screen readers announce when entering a column. A role="status" region reports each change, for example "Sorted by Amount, descending.". The scroll wrapper is a focusable role="region" labelled by the table heading, so keyboard users can scroll it. Header buttons show a white inset focus outline, arrows are aria-hidden, and the highlight and arrow transitions are disabled under prefers-reduced-motion.

Usage notes

The sorter works on the rows in the DOM, so it is best for up to a few hundred rows. For paged or server-side data, call your API from the click handler instead.

Sortable Data Table at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile
Category
Tables
License
Free for personal and commercial use

Questions about this template

Can I use the Sortable Data Table code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Sortable Data Table – free Tailwind CSS table template
Sortable Data Table · free Tailwind CSS table template

More Tables

Table with Search Filter – free Bootstrap 5 table template

Table with Search Filter

Bootstrap 5 team directory table with live search, status filter, highlighted matches, result counter and empty state.

Editable Table with Inline Editing – free Bootstrap 5 table template

Editable Table with Inline Editing

Dark Bootstrap 5 inventory table with click-to-edit cells, validation, live totals, an unsaved changes counter and simulated save and discard.

Product Comparison Table Section for Shopify – free Shopify table template

Product Comparison Table Section for Shopify

Shopify section with a real comparison table for 2 to 4 products: live prices, yes/no icons, row values set in the theme editor, and a sticky first column when it scrolls on phones.

Table with Sticky Header – free Pure CSS table template

Table with Sticky Header

Dark striped data table in pure CSS with a sticky header row and sticky first column inside a scrollable, keyboard-focusable container.

Responsive Table with Actions – free Bootstrap 5 table template

Responsive Table with Actions

Responsive Bootstrap 5 invoice table with status badges, a status filter and row action dropdowns to mark as paid, remind or delete.