Skip to content

Instantly share code, notes, and snippets.

@nash403
Last active June 9, 2025 21:20
Show Gist options
  • Save nash403/a160b66af0380576fa02eb9db6bee29e to your computer and use it in GitHub Desktop.
Save nash403/a160b66af0380576fa02eb9db6bee29e to your computer and use it in GitHub Desktop.
Tailwind CSS v4+ utility to create a responsive column layout
/*
* Create a fully responsive column layout while defining the max width of the columns.
* On small screens, columns width will be the min between 100% and the defined width.
*
* Add `--col-size` and / or `--col-size-*` variables to your tailwind theme for customization
* Ex:
* ```
* --col-size: 320px;
* --col-size-xl: 448px;
* --col-size-wathever: 1024px;
*/
@utility auto-fit-cols-* {
grid-template-columns: repeat(auto-fit, minmax(min(100%, --value([dimension])), 1fr));
grid-template-columns: repeat(auto-fit, minmax(min(100%, --value(percentage)), 1fr));
grid-template-columns: repeat(auto-fit, minmax(min(100%, --value([percentage])), 1fr));
grid-template-columns: repeat(auto-fit, minmax(min(100%, calc(--value(number) * --spacing(1))), 1fr));
grid-template-columns: repeat(auto-fit, minmax(min(100%, --value(--col-size-*)), 1fr));
}
@utility auto-fit-cols {
grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--col-size, 320px)), 1fr)); /* defaults to 320px column width if no `--col-size` variable is defined */
}
/* Use it like this: `grid auto-fit-cols-40` (40 * 4px = 160px column width if using default spacing) or `grid gap-2 auto-fit-cols-[10rem]` for example */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment