|
| 1 | +# CSS Grid Tailwind Plugin |
| 2 | + |
| 3 | +Isolated from [https://github.com/tailwindcss/plugin-examples](https://github.com/tailwindcss/plugin-examples) |
| 4 | + |
| 5 | +[View demo](https://tailwindcss.github.io/plugin-examples/#css-grid) · [View source](https://github.com/tailwindcss/plugin-examples/blob/master/plugins/css-grid/index.js) |
| 6 | + |
| 7 | +In `plugins/css-grid/index.js` you'll find an example of a plugin that adds new utilities for using [CSS Grid Layout](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout). |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +It exposes three configuration options: |
| 12 | + |
| 13 | +- `grids`, for specifying all of the grid sizes you'd like to generate |
| 14 | +- `gaps`, for specifying the gap sizes you'd like to generate |
| 15 | +- `variants`, for specifying which variants to generate |
| 16 | + |
| 17 | +```js |
| 18 | +module.exports = { |
| 19 | + // ... |
| 20 | + |
| 21 | + plugins: [ |
| 22 | + // ... |
| 23 | + require('./plugins/css-grid')({ |
| 24 | + grids: [2, 3, 5, 6, 8, 10, 12], |
| 25 | + gaps: { |
| 26 | + 0: '0', |
| 27 | + 4: '1rem', |
| 28 | + 8: '2rem', |
| 29 | + }, |
| 30 | + variants: ['responsive'], |
| 31 | + }), |
| 32 | + ], |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +With zero configuration, it will generate grids from 1 to 12 columns in size, no gap sizes, and `responsive` variants for each new utility. |
| 37 | + |
| 38 | +The plugin generates the following sets of classes: |
| 39 | + |
| 40 | +- `.grid`, for setting `display: grid` on an element |
| 41 | +- `.grid-columns-{size}`, for specifying the number of columns in the grid |
| 42 | +- `.grid-gap-{size}`, for specifying the size of the gap between columns/rows |
| 43 | +- `.col-span-{columns}`, for specifying how wide a column should be |
| 44 | +- `.col-start-{line}` and `.col-end-{line}`, for specifying a column's start and end points explicitly (useful for reordering columns or leaving gaps) |
| 45 | + |
| 46 | +It's not really practical to expose all of the power of CSS Grid through utilities, but this plugin is a good example of using CSS Grid to replace a column-only float or Flexbox grid. |
0 commit comments