Home

Awesome

grid-kiss: Keep CSS Grids simple, stupid

This is a PostCSS plugin aiming to replace the 24 new properties brought by CSS Grids with a single one that you immediately understand when you see it.

<p align="center"> <a href="https://www.npmjs.com/package/postcss-grid-kiss"><img src="https://img.shields.io/npm/dt/postcss-grid-kiss.svg" alt="Downloads"></a> <a href="https://www.npmjs.com/package/postcss-grid-kiss"><img src="https://img.shields.io/npm/v/postcss-grid-kiss.svg" alt="Version"></a> <a href="https://travis-ci.org/sylvainpolletvillard/postcss-grid-kiss"><img src="https://travis-ci.org/sylvainpolletvillard/postcss-grid-kiss.svg?branch=master" alt="Build Status"></a> <a href="https://www.npmjs.com/package/postcss-grid-kiss"><img src="https://img.shields.io/npm/l/postcss-grid-kiss.svg" alt="License"></a> <a href="https://gitter.im/sylvainpolletvillard/postcss-grid-kiss?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://badges.gitter.im/sylvainpolletvillard/postcss-grid-kiss.svg" alt="Discuss about this project on gitter"></a> </p>

Table of contents

Examples

Try it online

Try the different examples and play with the plugin on the playground. Edit the CSS and HTML on the left and the grid will be updated instantly.

Basic website layout

body {
	grid-kiss:
		"+-------------------------------+      "
		"|           header ↑            | 120px"
		"+-------------------------------+      "
		"                                       "
		"+-- 30% ---+  +--- auto --------+      "
		"| .sidebar |  |       main      | auto "
		"+----------+  +-----------------+      "
		"                                       "
		"+-------------------------------+      "
		"|              ↓                | 60px "
		"|         → footer ←            |      "
		"+-------------------------------+      "
}

is converted to:

body > header {
	grid-area: header;
	align-self: start;
}

body > .sidebar {
	grid-area: sidebar;
}

body > main {
	grid-area: main;
}

body > footer {
	grid-area: footer;
	justify-self: center;
	align-self: end;
}

body {
	display: grid;
	align-content: space-between;
	grid-template-rows: 120px 1fr 60px;
	grid-template-columns: 30% 1fr;
	grid-template-areas:
		"header  header"
		"sidebar main  "
		"footer  footer";
}

which displays this kind of grid layout:

example-result

Responsive layouts

Use different grid-kiss declarations in media queries to easily get responsive layouts. It is recommended to start by the grid on small screens, then use media queries to progressively enhance your layouts on wider screens.

responsive-layout

Installation

npm install postcss-grid-kiss --save-dev
yarn add postcss-grid-kiss --dev

Usage

If you never used PostCSS before, read PostCSS usage documentation first.

postcss src/your.css --output dist/compiled.css --use postcss-grid-kiss
const postcss  = require('postcss'),
      gridkiss = require('postcss-grid-kiss');

postcss([ gridkiss ])
    .process(css, { from: 'src/your.css', to: 'compiled.css' })
    .then(function (result) {
        fs.writeFileSync('compiled.css', result.css);
        if( result.map ) fs.writeFileSync('compiled.css.map', result.map);
    });

Read PostCSS documentation to make it work with Webpack, Gulp or your other build system.

Fallback for older browsers

CSS Grid Layout is a W3C Candidate Recommandation supported in all the evergreen browsers. It is available in Chrome 57, Firefox 52, Safari 10.1, Edge 16 and Opera 44. It is also supported on mobile iOS Safari and Chrome for Android. See Can I Use for more information on browser support.

For browsers not supporting CSS Grid Layout, Grid-kiss proposes a fallback that use absolute positionning and calc() operator. It uses a @supports query to only apply on non-supported browsers, and does not involve JavaScript.

With this fallback, Grid-kiss layouts will work on any browser supporting calc(), which is like 95% of browsers. But you should note that a fallback based on absolute positionning has some limitations:

Internet Explorer does not support @supports 🙄 , so Grid-kiss needs to add another media query hack that is known to run only on IE: @media screen and (min-width:0\0). This extends support from IE9 to IE11 at the cost of a bigger output size. If you don't care about Internet Explorer support and want to reduce the output size, you should omit IE in your browserslist.

By default, Grid-kiss is looking in your browserslist config for the list of supported browsers and automatically deduce what fallbacks are needed for your project by using Can I Use data. You can override this automatic detection with the fallback option explained below.

Options

Grid-kiss comes with a few options:

postcss([ gridkiss({ ...options }) ])

fallback : add fallback for browsers not supporting CSS Grid Layout

Note: it is recommended to use automatic detection through browserslist instead of using this option. See Fallback section.

If this option is provided, it overrides automatic detection and tells explicitely whether to add or not the fallback styles to the output.

postcss([ gridkiss({ fallback: true }) ]) // always add all fallbacks
postcss([ gridkiss({ fallback: false }) ]) // never add any fallback

optimize - reduce output size

This option (enabled by default) reduces the size of the output while keeping it readable. It does so by merging grid properties and renaming zone identifiers. For complete minification, use it with cssnano.

Set this option to false if you prefer a more verbose and descriptive output. Try to toggle the option in the playground to compare the outputs.

postcss([ gridkiss({ optimize: false }) ])

selectorParser - apply custom transforms to zone selectors

This option receives a function that is applied on the selectors you wrote in the zones. This is useful to add your own transforms or selector syntax, for example to use component names in a component-based framework like Vue or React.

postcss([
  gridkiss({
    selectorParser: function (selector) {
      if (/[A-Z]/.test(selector[0])) {
        return `[data-component-name='${selector}']`
      }
      return selector
    }
  })
])

Alternative styles

These alternative styles for zone syntax are also supported :

div {
	grid-kiss:
	"┌──────┐  ┌──────┐         "
	"│      │  │  ↑   │         "
	"│      │  │ bar →│  200px  "
	"│  ↓   │  └──────┘         "
	"│ baz  │              -    "
	"│  ↑   │  ┌──────┐         "
	"│      │  │  ↑   │  200px  "
	"└──────┘  │      │         "
	"          │ foo  │    -    "
	"┌──────┐  │      │         "
	"│ qux  │  │  ↓   │  200px  "
	"│  ↓   │  │      │         "
	"└─20em─┘  └──────┘         "
}
main {
	grid-kiss:
	"╔═══════╗  ╔════════════════╗      "
	"║       ║  ║    .article    ║ auto "
	"║   ↑   ║  ╚════════════════╝      "
	"║  nav  ║  ╔════╗  ╔════════╗      "
	"║       ║  ║    ║  ║ aside →║ 240px"
	"╚═ 25% ═╝  ╚════╝  ╚═ 80em ═╝      "
}

Documentation

How to draw a grid

Values accepted for selectors

Inside each zone, you can write a selector to associate a zone to a DOM element. It can be a tag name, a .class, an #id, or any.other[valid]#selector.

Since v1.2.0, selectors in zones may use some shortened notations specific to grid-kiss, although using a class is still the recommended method.

Since v1.4.0, you can also apply custom transforms and make your own syntax with the selectorParser option

Dimensions of rows

Declare the size of a row by writing the dimension just after the last column of the grid

+------+  +------+ ---
|  ^   |  | .bar | 40em
|      |  +------+ ---
| .baz |
|      |  +------+ ---
|  v   |  |  ^   | 38em
+------+  |      | ---
          | .foo |
+------+  |      | ---
| .qux |  |  v   | 40em
+------+  +------+ ---

The - separators between dimensions are not mandatory, they are only here to make the grid more readable.

Dimensions of columns

Declare the size of a column by writing the dimension inside the top or bottom border of a zone:

+-- 640px --+      +----------+
|  selector |  or  | selector |
+-----------+      +---30%----+

You cannot set the width of a zone occupying more than one column. This would imply some calculations that may or may not have a solution. As an alternative, you can declare the size of a column just after the last row of the grid:

+-------------+ +-----+        +-------------+ +-20%-+
|  .bigzone   | |     |        |  .bigzone   | |     |
+-------------+ +-----+        +-------------+ +-----+
+-----+ +-------------+   or   +-----+ +-------------+
|     | |  .bigzone2  |        |     | |  .bigzone2  |
+-----+ +-------------+        +-20%-+ +-------------+
| 20% | | 60% | | 20% |                | 60% |

The | separators between dimensions are not mandatory, they are only here to make the grid more readable.

Dimensions of gaps

You can also declare the dimension of spacing between zones the same way you do with rows and columns. These spaces are called gaps and act like empty zones. The example below defines gaps of 50px.

+-----+      +-----+      +-----+  ----
| .nw |      | .n  |      | .ne | 100px
+-----+      +-----+      +-----+  ----
                                   50px
+-----+      +-----+      +-----+  ----
| .w  |      |     |      | .e  | 100px
+-----+      +-----+      +-----+  ----
                                   50px
+-----+      +-----+      +-----+  ----
| .sw |      | .s  |      | .se | 100px
+-----+      +-----+      +-----+  ----
|100px| 50px |100px| 50px |100px|

Values accepted for dimensions

Dimensions can be any of the specified values:

When no value is specified, row and column sizes are set as auto.

Since v2.5.0, you can also apply custom transforms and make your own syntax with the dimensionParser option

Horizontal alignment of the grid

Specifies how all the zones are aligned horizontally inside the grid container. Irrelevant if one of the zones fits all the remaining free space.

"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"

grid-justify-content-stretch

"+---+ +---+ +---+    "
"| a | | b | | c |    "
"+---+ +---+ +---+    "
"+---+ +---+ +---+    "
"| d | | e | | f |    "
"+---+ +---+ +---+    "
"+---+ +---+ +---+    "
"| g | | h | | i |    "
"+---+ +---+ +---+    "

grid-justify-content-start

when there are two consecutive spaces or more at the beginning of the rows

"    +---+ +---+ +---+"
"    | a | | b | | c |"
"    +---+ +---+ +---+"
"    +---+ +---+ +---+"
"    | d | | e | | f |"
"    +---+ +---+ +---+"
"    +---+ +---+ +---+"
"    | g | | h | | i |"
"    +---+ +---+ +---+"

grid-justify-content-end

"    +---+ +---+ +---+    "
"    | a | | b | | c |    "
"    +---+ +---+ +---+    "
"    +---+ +---+ +---+    "
"    | d | | e | | f |    "
"    +---+ +---+ +---+    "
"    +---+ +---+ +---+    "
"    | g | | h | | i |    "
"    +---+ +---+ +---+    "

grid-justify-content-center

"+---+    +---+    +---+"
"| a |    | b |    | c |"
"+---+    +---+    +---+"
"+---+    +---+    +---+"
"| d |    | e |    | f |"
"+---+    +---+    +---+"
"+---+    +---+    +---+"
"| g |    | h |    | i |"
"+---+    +---+    +---+"

grid-justify-content-space-between

"    +---+  +---+  +---+    "
"    | a |  | b |  | c |    "
"    +---+  +---+  +---+    "
"    +---+  +---+  +---+    "
"    | d |  | e |  | f |    "
"    +---+  +---+  +---+    "
"    +---+  +---+  +---+    "
"    | g |  | h |  | i |    "
"    +---+  +---+  +---+    "

grid-justify-content-space-evenly

"  +---+    +---+    +---+  "
"  | a |    | b |    | c |  "
"  +---+    +---+    +---+  "
"  +---+    +---+    +---+  "
"  | d |    | e |    | f |  "
"  +---+    +---+    +---+  "
"  +---+    +---+    +---+  "
"  | g |    | h |    | i |  "
"  +---+    +---+    +---+  "

grid-justify-content-space-around

Vertical alignment of the grid

Specifies how all the zones are aligned vertically inside the grid container. Irrelevant if one of the zones fits all the remaining free space.

"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"

grid-align-content-stretch

"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"
"                 "
"                 "

grid-align-content-start

"                 "
"                 "
"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"

grid-align-content-end

"                 "
"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"
"                 "

grid-align-content-center

"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"                 "
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"                 "
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"

grid-align-content-space-between

"                 "
"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"                 "
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"                 "
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"
"                 "

grid-align-content-space-evenly

"                 "
"+---+ +---+ +---+"
"| a | | b | | c |"
"+---+ +---+ +---+"
"                 "
"                 "
"+---+ +---+ +---+"
"| d | | e | | f |"
"+---+ +---+ +---+"
"                 "
"                 "
"+---+ +---+ +---+"
"| g | | h | | i |"
"+---+ +---+ +---+"
"                 "

grid-align-content-space-around

Horizontal alignment inside a zone

Each zone can specify an alignment indicator. When no indicators are specified, defaults are stretch horizontally and vertically.

+-------------+    +-------------+
| .item-a  <  | or | .item-a  ←  |
+-------------+    +-------------+

grid-justify-self-start

+-------------+    +-------------+
|  >  .item-a | or |  →  .item-a |
+-------------+    +-------------+

grid-justify-self-end

+--------------+    +--------------+
| < .item-a  > | or | ← .item-a  → |
+--------------+    +--------------+

grid-justify-self-stretch

+--------------+    +--------------+
| > .item-a  < | or | → .item-a  ← |
+--------------+    +--------------+

grid-justify-self-center

Vertical alignment inside a zone

+-------------+    +-------------+
|   .item-a   | or |   .item-a   |
|      ^      |    |      ↑      |
+-------------+    +-------------+

grid-align-self-start

+-------------+    +-------------+
|      v      | or |      ↓      |
|   .item-a   |    |   .item-a   |
+-------------+    +-------------+

grid-align-self-end

+-------------+    +-------------+
|      ^      |    |      ↑      |
|   .item-a   | or |   .item-a   |
|      v      |    |      ↓      |
+-------------+    +-------------+

grid-align-self-stretch

+-------------+    +-------------+
|      v      |    |      ↓      |
|   .item-a   | or |   .item-a   |
|      ^      |    |      ↑      |
+-------------+    +-------------+

grid-align-self-center

New lines and position of alignement characters do not matter. Just make it visually understandable.

Usage with Prettier formatter

People using Prettier code formatter may have issues with the automatic formating of grid-kiss declarations. In that case, write grid-template-kiss instead of grid-kiss. For some reason, Prettier correctly formats the declarations starting with grid-template-***.


Sketchnote


Credits for images : CSS Tricks, @aneveux