A responsive, declarative grid system powered by flexbox. Demo at flexgrid.axiomzen.co
Flexgrid.css is a responsive, declarative grid system powered by flexbox. It allows you to generate finely tuned responsive layouts at a lightning pace. The declarative nature lets you determine what size a grid cell should be at a certain screen width.
There are two ways to use flexgrid.css:
-
Use it as an
npm
module by running the following command:npm install flexgrid.css --save
-
Download the files directly:
- Uncompressed - flexgrid.css
- Compressed - flexgrid.min.css
Grids can de used on generic HTML elements such as div
, but resets are also in place to use them on ul
and ol
.
If you're using it out of the box, these are the default values that come shipped:
- columns: 12
- breakpoints:
xs
: 320pxsm
: 480pxmd
: 640pxlg
: 760pxxl
: 1020pxxx
: 1200px
- gutters:
xs
: 0.2remsm
: 0.32remmd
: 0.52remlg
: 0.84remxl
: 1.36remxx
: 2.2rem
These are all easily customisable if you build the file yourself.
In the most basic form, you can create a grid where all cells occupy an equal amount of space:
<div class="Grid">
<div class="Grid-cell"></div>
<div class="Grid-cell"></div>
...
</div>
If you want cells to occupy the amount of space that their content takes up, then use the --auto
modifier:
<div class="Grid">
<div class="Grid-cell--auto"></div>
<div class="Grid-cell--auto"></div>
<div class="Grid-cell--auto"></div>
<div class="Grid-cell--auto"></div>
...
</div>
In order to declaratively build a responsive grid in which cells occupy a certain percentage of width at a certain screen size, you can chain together size modifiers for the cells. When using this approach, the bare requirement is to declare the cell size at the smallest screen size, i.e. xs
. The other sizes are optional. Here are all screen size modifiers:
xs
sm
md
lg
xl
xx
The grid is based on a 12 column layout, so when thinking about your display for one row, think that the span number must add up to 12. Here's a basic example:
<ul class="Grid">
<li class="Grid-cell--xs-2"></li>
<li class="Grid-cell--xs-8"></li>
<li class="Grid-cell--xs-2"></li>
</ul>
This grid will display cells the same at any screen size, in a 2:8:2
ratio (compared to the number of columns, 12). Let's make these grid cells change at various breakpoints:
<div class="Grid">
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
<div class="Grid-cell--xs-4 Grid-cell--md-3 Grid-cell--lg-2"></div>
</div>
In the above example, the cells will be in a ratio of 4:4:4
per row, making 4 rows in total, by default and at the xs
screen size. At the md
screen size, they will occupy 3:3:3:3
per row, making 3 rows in total. Finally at the lg
screen size, they will occupy 2:2:2:2:2:2
per row, making 2 rows in total. If you're afraid about cell rows not always adding up to the number 12, fear not. Extra cells just overflow to a new line.
If at any point in time you want to offset cells, there's a simple way to do just that. In the following example, the first cell will span 12 columns at xs
screens, and 6 columns at md
screens. It will also be offset by 6
columns on md
screens. The second cell will conform to the flow of the first one, and sit below it because it can't fit in the first row:
<div class="Grid Grid--gutter-xl">
<div class="Grid-cell--xs-12 Grid-cell--md-6 Grid-cell--md-offset-6"></div>
<div class="Grid-cell--xs-12 Grid-cell--md-8"></div>
</div>
If at any point you want to reset the offset to 0, you can just use the offset-0
addition to the modifier:
<div class="Grid Grid--gutter-xl">
<div class="Grid-cell--xs-12 Grid-cell--md-6 Grid-cell--md-offset-6 Grid-cell--xl-offset-0"></div>
<div class="Grid-cell--xs-12 Grid-cell--md-8"></div>
</div>
It's important to be careful that your offsets + cell spans don't overflow outside the rows...a little bit of very basic math is required!
You can add gutters to the grid cells which will create an even, fixed horizontal and vertical spacing between cells, while collapsing the excess amounts. Gutter modifiers come in four sizes:
<div class="Grid Grid--gutter-sm">...</div>
<div class="Grid Grid--gutter-md">...</div>
<div class="Grid Grid--gutter-lg">...</div>
<div class="Grid Grid--gutter-xl">...</div>
To change the orientation to a columnar grid, add the following modifier:
<div class="Grid Grid--column">...</div>
To change the alignment of grid cells with respect to the flex line, use the following modifiers:
<!-- aligns to start of flex line -->
<div class="Grid Grid--alignStart">...</div>
<!-- aligns to end of flex line -->
<div class="Grid Grid--alignEnd">...</div>
<!-- aligns to center of flex line -->
<div class="Grid Grid--alignCenter">...</div>
To change the spatial distribution of grid cells along the main axis, use the following modifiers:
<!-- packs cells to center of main axis -->
<div class="Grid Grid--justifyCenter">...</div>
<!-- packs cells to end of main axis -->
<div class="Grid Grid--justifyEnd">...</div>
<!-- distributes extra space evenly between cells -->
<div class="Grid Grid--spaceBetween">...</div>
<!-- distributes extra space evenly around cells -->
<div class="Grid Grid--spaceAround">...</div>
This component has been tested in the following browsers:
Browser | Version |
---|---|
Chrome 46.0+ | |
Firefox 40.0+ | |
Safari 9.0+ | |
Opera 33.0+ |
Please check CONTRIBUTING.md.
This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.