# Max Columns Grid

> A responsive auto-fill grid that wraps on small screens but never grows past a set number of columns.

- Category: [CSS Layouts](https://copy-css.com/category/layouts)
- Tags: responsive grid, auto-fill, column limit, minmax, cards
- Live preview: https://copy-css.com/snippets/max-columns-grid
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<div class="grid-max">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
```

## CSS

```css
.grid-max {
  --cols: 3;   /* never more than this */
  --min: 200px; /* never narrower than this */
  --gap: 16px;
  display: grid;
  grid-template-columns: repeat(
    auto-fill,
    minmax(min(100%, max(var(--min), (100% - (var(--cols) - 1) * var(--gap)) / var(--cols))), 1fr)
  );
  gap: var(--gap);
}
```

## Notes

- This CSS Max Columns Grid is 11 lines of pure CSS (0.3 KB) with no JavaScript and no dependencies. It uses CSS Grid and custom properties.
- Customize with: `--cols`, `--min`, `--gap`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
