Guides
Column Size Guide
How to column width in Shadcn Table
Shadcn Table lets you easily change the default widths (sizes) of columns.
Example
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| defaultColumn | Partial<ST_ColumnDef<TData>> | Default column options to use for all column defs supplied to the table. This is useful for providing default cell/header/footer renderers, sorting/filtering/grouping options, etc. |
| defaultDisplayColumn | Partial<ST_DisplayColumnDef<TData>> | Default column options to use for all display column defs supplied to the table. This is useful for providing default cell/header/footer renderers, or turning on or off certain features for all display columns. |
| displayColumnDefOptions | { [key: string]: ST_DisplayColumnDef<TData> } | Customize and override the column definition options for the built-in display columns. (Select, Expand, Row Actions, etc.) |
| layoutMode | 'semantic' | 'grid' | 'grid-no-grow' | The width rule. 'grid' (the default) lets data columns share the spare width in the ratio of their sizes, while display columns and pinned columns keep their set width. 'grid-no-grow' keeps every column at its set width, 'semantic' renders a plain table layout. Virtualization always uses 'grid'. |
Relevant Column Options
| Column Option | Type | Description |
|---|---|---|
| grow | boolean | number | If `layoutMode` is `'grid'` (the default) or `'grid-no-grow'`, set the flex grow value for a column, or `false`/0 to keep it at its set width. In the default layout display columns and pinned columns do not grow; a data column grows in the ratio of its size. |
| maxSize | number | |
| minSize | number | |
| size | number |
Relevant State
| State Option | Type | Description |
|---|---|---|
| columnSizing | Record<string, number> | an object mapping column IDs to their current width in pixels |
Layout Modes
Shadcn Table has 3 layout modes that affect how column widths are applied. The layoutMode table option picks one; the default is "grid", and virtualization always uses "grid".
"grid"(default) - uses CSS Flexbox rows. Data columns share the spare width of the table in the ratio of theirsize, while display columns (row numbers, selection, row actions and the like) and pinned columns keep their set width. Once the columns are wider than the table, every column renders at its set width and the table scrolls sideways. Resizing does not change the mode."grid-no-grow"- uses the same Flexbox rows, but setsflex-grow: 0on every column and adds an empty "spacer" column to the end of the table to fill the remaining space. Every column keeps exactly its set width."semantic"- uses the default CSS styles that come with<table>,<tr>,<td>, etc. elements. Not available with virtualization.
If you want your columns to have an absolute width, you can use the "grid-no-grow" layout mode and set the size option on each column.
Column Size
You can change the width of any column by setting its size option on the column definition. There are minSize and maxSize column options available to enforce limits during resizing events.
The size, minSize, and maxSize do not take in any CSS width unit. They are only numbers that represent the width of the column in pixels.
const columns = [
{
accessorKey: 'id',
header: 'ID',
grow: false, //don't allow this column to grow to fill in remaining space
size: 50, //small column
},
{
accessorKey: 'username',
header: 'Username',
minSize: 100, //min size enforced during resizing
maxSize: 400, //max size enforced during resizing
size: 180, //medium column
},
{
accessorKey: 'email',
header: 'Email',
grow: true, //allow this column to grow to fill in remaining space
size: 300, //large column
},
];Column Grow
You can also set grow properties on the column definitions when using layoutMode "grid" or "grid-no-grow".
If the layout mode of your table is "grid" (the default), every data column grows in the ratio of its size. Set grow: false (or 0) on a column to give it a fixed size; it will not grow to fill in the remaining space of the table. Display columns have grow: false by default, and a pinned column never grows while it is pinned, so the pinned offsets stay exact.
If the layout mode of your table is "grid-no-grow" and you set grow: true on a column, that column will grow to fill in the remaining space of the table.
If the layout mode of your table is "grid-no-grow" and you set grow: 1 on one column, and grow: 2 on another column, the first column will grow to fill in 1/3 of the remaining space of the table, and the second column will grow to fill in 2/3 of the remaining space of the table.
If the layout mode of your table is "semantic", the grow column option will have no effect, but columns will size themselves to fit their own content better than the other layout modes.
But I Want My Columns to Have a Percentage Width
Shadcn Table is not designed to use percentage column widths well by default. This is because having horizontal scrollbars on tables is both a very common use case, and usually the best way to ensure that the actual content of the cells in each column is visible and not cutoff or wrapped excessively.
If you disagree with that, that's fair, and you're not out of luck. Shadcn Table exposes all st*Props, so if you really want to, you can add whatever CSS you want to achieve your own custom column widths behavior. This is not recommended by the maintainer, but some developers have found it useful to set all column sizes and minSizes to a very small number like 1, 1.5, 2, etc., and then use either layoutMode: "semantic" or layoutMode: "grid". This makes the columns act more like they are using percentage widths, up until a certain point. If you really need to never have a horizontal scrollbar, you can edit the stTableContainerProps className or style to set overflow-x: hidden. Be very careful with this though, as this could cause your table to be unusable on smaller screens.
If the above still does not get you to where you need to be, stTableHeadCellProps, stTableBodyCellProps, and stTableFooterCellProps (className or style) and more are all exposed and available to you to override with whatever custom CSS that you need. Just be aware that non of the built-in TanStack Table size APIs will be accurate anymore.
Side Note: Just Make Your Scrollbars Prettier
Making your scrollbars look better (especially on Windows and Linux) can go a long way to not letting a horizontal scrollbar ruin the look of your table.
Here's what this docs site uses in its global CSS to make scrollbars to more like the default Mac OS scrollbars for everyone:
::-webkit-scrollbar-track {
background-color: transparent;
}
::-webkit-scrollbar {
right: 0;
width: 12px;
height: 12px;
}
::-webkit-scrollbar-thumb {
background-color: #999;
border-radius: 8px;
width: 12px;
height: 12px;
}Default Column
By default, columns will have the following size properties defined:
defaultColumn = { minSize: 40, maxSize: 1000, size: 180 }; //units are in pxYou can modify the default column widths by setting the defaultColumn table option on the table.
const table = useShadcnTable({
columns,
data,
defaultColumn: {
minSize: 20, //allow columns to get smaller than default
maxSize: 9001, //allow columns to get larger than default
size: 260, //make columns wider by default
},
});Change Sizes of Built-in Display Columns
As discussed further in the Display Columns Guide, you can customize the options of the built-in columns that get generated under the hood by ST by enabling certain features.
const table = useShadcnTable({
columns,
data,
displayColumnDefOptions: {
'st-row-select': {
size: 50, //adjust the size of the row select column
grow: false, //default is false for this column
},
'st-row-numbers': {
size: 40,
grow: false, //default is false for every display column
},
},
enableRowNumbers: true,
});
return <ShadcnTable table={table} />;View Extra Storybook Examples