---
title: Table for Astro
description: "Semantic Astro table parts for records and comparisons."
seo:
    title: Table for Astro | Shadcn & Tailwind CSS
related:
    - /components/pagination
    - /components/card
sidebar:
    label: "Table"
---

```astro
---
import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
---

<Table class="w-full max-w-2xl">
  <TableCaption>Sample charges for completed studio visits.</TableCaption>
  <TableHeader>
    <TableRow>
      <TableHead>Invoice</TableHead>
      <TableHead>Status</TableHead>
      <TableHead>Method</TableHead>
      <TableHead class="text-right">Amount</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    <TableRow>
      <TableCell>INV-001</TableCell>
      <TableCell>Paid</TableCell>
      <TableCell>Credit Card</TableCell>
      <TableCell class="text-right">$250.00</TableCell>
    </TableRow>
    <TableRow>
      <TableCell>INV-002</TableCell>
      <TableCell>Pending</TableCell>
      <TableCell>PayPal</TableCell>
      <TableCell class="text-right">$150.00</TableCell>
    </TableRow>
    <TableRow>
      <TableCell>INV-003</TableCell>
      <TableCell>Unpaid</TableCell>
      <TableCell>Bank Transfer</TableCell>
      <TableCell class="text-right">$350.00</TableCell>
    </TableRow>
  </TableBody>
  <TableFooter>
    <TableRow>
      <TableCell colspan="3">Total</TableCell>
      <TableCell class="text-right">$750.00</TableCell>
    </TableRow>
  </TableFooter>
</Table>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/table
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/table/index.ts

```ts
export { default as Table } from "./table.astro"
export { default as TableHeader } from "./table-header.astro"
export { default as TableBody } from "./table-body.astro"
export { default as TableFooter } from "./table-footer.astro"
export { default as TableRow } from "./table-row.astro"
export { default as TableHead } from "./table-head.astro"
export { default as TableCell } from "./table-cell.astro"
export { default as TableCaption } from "./table-caption.astro"
```

#### src/components/ui/table/table-body.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"tbody">

const { class: className, ...props } = Astro.props
---

<tbody data-slot="table-body" class={cn("relative in-data-[variant=card]:rounded-xl in-data-[variant=card]:shadow-xs/5 before:pointer-events-none before:absolute before:inset-px not-in-data-[variant=card]:before:hidden before:rounded-[calc(var(--radius-xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] dark:before:shadow-[0_-1px_--theme(--color-white/8%)] [&_tr:last-child]:border-0 in-data-[variant=card]:*:[tr]:border-0 in-data-[variant=card]:*:[tr]:*:[td]:border-b in-data-[variant=card]:*:[tr]:*:[td]:bg-card in-data-[variant=card]:*:[tr]:first:*:[td]:first:rounded-ss-xl in-data-[variant=card]:*:[tr]:*:[td]:first:border-s in-data-[variant=card]:*:[tr]:first:*:[td]:border-t in-data-[variant=card]:*:[tr]:last:*:[td]:last:rounded-ee-xl in-data-[variant=card]:*:[tr]:*:[td]:last:border-e in-data-[variant=card]:*:[tr]:first:*:[td]:last:rounded-se-xl in-data-[variant=card]:*:[tr]:last:*:[td]:first:rounded-es-xl in-data-[variant=card]:*:[tr]:hover:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-black)_2%)] in-data-[variant=card]:*:[tr]:data-[state=selected]:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-black)_4%)] dark:in-data-[variant=card]:*:[tr]:data-[state=selected]:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-white)_4%)] dark:in-data-[variant=card]:*:[tr]:hover:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-white)_2%)]", className)} {...props}>
  <slot />
</tbody>
```

#### src/components/ui/table/table-caption.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"caption">

const { class: className, ...props } = Astro.props
---

<caption
  data-slot="table-caption"
  class={cn("in-data-[variant=card]:my-4 mt-4 text-muted-foreground text-sm", className)}
  {...props}
>
  <slot />
</caption>
```

#### src/components/ui/table/table-cell.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"td">

const { class: className, ...props } = Astro.props
---

<td data-slot="table-cell" class={cn("whitespace-nowrap bg-clip-padding p-2.5 in-data-[slot=table-footer]:py-3.5 align-middle leading-none in-data-[variant=card]:first:ps-[calc(--spacing(2.5)-1px)] in-data-[variant=card]:last:pe-[calc(--spacing(2.5)-1px)] has-[[role=checkbox]]:w-px last:has-[[role=checkbox]]:ps-0 first:has-[[role=checkbox]]:pe-0", className)} {...props}>
  <slot />
</td>
```

#### src/components/ui/table/table-footer.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"tfoot">

const { class: className, ...props } = Astro.props
---

<tfoot
  data-slot="table-footer"
  class={cn("border-t in-data-[variant=card]:border-none bg-transparent not-in-data-[variant=card]:bg-[color-mix(in_srgb,var(--card),var(--color-black)_2%)] font-medium dark:not-in-data-[variant=card]:bg-[color-mix(in_srgb,var(--card),var(--color-white)_2%)] [&>tr]:last:border-b-0", className)}
  {...props}
>
  <slot />
</tfoot>
```

#### src/components/ui/table/table-head.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"th">

const { class: className, ...props } = Astro.props
---

<th data-slot="table-head" class={cn("h-10 whitespace-nowrap px-2.5 text-left align-middle font-medium text-muted-foreground leading-none has-[[role=checkbox]]:w-px last:has-[[role=checkbox]]:ps-0 first:has-[[role=checkbox]]:pe-0", className)} {...props}>
  <slot />
</th>
```

#### src/components/ui/table/table-header.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"thead">

const { class: className, ...props } = Astro.props
---

<thead
  data-slot="table-header"
  class={cn("[&_tr]:border-b", className)}
  {...props}
>
  <slot />
</thead>
```

#### src/components/ui/table/table-row.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"tr">

const { class: className, ...props } = Astro.props
---

<tr data-slot="table-row" class={cn("relative border-b not-in-data-[variant=card]:hover:bg-[color-mix(in_srgb,var(--background),var(--color-black)_2%)] not-in-data-[variant=card]:data-[state=selected]:bg-[color-mix(in_srgb,var(--background),var(--color-black)_4%)] dark:not-in-data-[variant=card]:data-[state=selected]:bg-[color-mix(in_srgb,var(--background),var(--color-white)_4%)] dark:not-in-data-[variant=card]:hover:bg-[color-mix(in_srgb,var(--background),var(--color-white)_2%)]", className)} {...props}>
  <slot />
</tr>
```

#### src/components/ui/table/table.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"table"> & { variant?: "default" | "card" }

const { class: className, variant = "default", ...props } = Astro.props
---

<div
  data-slot="table-container"
  data-variant={variant}
  class="relative w-full overflow-x-auto"
>
  <table data-slot="table" class={cn("w-full caption-bottom in-data-[variant=card]:border-separate in-data-[variant=card]:border-spacing-0 text-sm", className)} {...props}>
    <slot />
  </table>
</div>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import {
    Table,
    TableBody,
    TableCaption,
    TableCell,
    TableFooter,
    TableHead,
    TableHeader,
    TableRow,
} from "@/components/ui/table"
```

```astro
<Table>
  <TableCaption>A list of your invoices.</TableCaption>
  <TableHeader>
    <TableRow>
      <TableHead>Invoice</TableHead>
      <TableHead>Status</TableHead>
      <TableHead>Amount</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    <TableRow>
      <TableCell>INV-001</TableCell>
      <TableCell>Paid</TableCell>
      <TableCell>$250.00</TableCell>
    </TableRow>
  </TableBody>
</Table>
```

## Composition

Arrange the parts as shown for a `Table`:

```text
Table
├── TableCaption
├── TableHeader
│   └── TableRow
│       ├── TableHead
│       ├── TableHead
│       └── TableHead
├── TableBody
│   └── TableRow
│       ├── TableCell
│       ├── TableCell
│       └── TableCell
└── TableFooter
```

## Notes

- Keep table structure semantic: headers in `TableHeader`, rows in `TableBody`,
  and summary data in `TableFooter` when needed.
- Right-align numeric columns so values are easier to scan.
- Wrap large tables in a container with horizontal overflow when the content may
  exceed the available width.

## Sizes and states

```astro
---
import {
  Table,
  TableHeader,
  TableBody,
  TableRow,
  TableHead,
  TableCell,
  TableCaption,
} from "@/components/ui/table"
---

<Table variant="card">
  <TableCaption>Content ready for handoff</TableCaption>
  <TableHeader>
    <TableRow>
      <TableHead scope="col">Page</TableHead>
      <TableHead scope="col">Review</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    <TableRow>
      <TableCell>Services</TableCell>
      <TableCell>Approved</TableCell>
    </TableRow>
    <TableRow data-state="selected">
      <TableCell>Contact</TableCell>
      <TableCell>Selected for review</TableCell>
    </TableRow>
  </TableBody>
</Table>
```

## API Reference

Read the [installed source](/r/table.json) for the complete prop types.

## When to use Table

Compose TableHeader, TableBody, rows, and cells from your data. Use header cells for column or row names and a caption when it helps identify the data. The wrapper allows horizontal overflow.

## Keyboard and behavior

Native table markup preserves the relationship between headers and cells. The component does not add sorting, filtering, row selection, or pagination. Implement those as labelled controls and keep their state clear.

## Theme and layout

Use `variant="card"` for the card table treatment or the default for a page table. Check long cells and narrow-screen overflow. Avoid hiding essential columns only to fit a small screen.
