A lightweight responsive flexbox grid with cross browser support, an inline-block fallback and no polyfills
These are the docs for version 1 which I have maintained as a reference for people still using it. The last release under version 1 was 1.5.0. However, version 2 is more reliable and robust so I recommend using that instead.
Reflex gives you the ability to easily take advantage of flexbox for laying out a grid while having a reliable inline-block fallback for older browsers.
At the time of writing, global flexbox support is at 94.1% (excluding browsers with outdated or partial support) so I propose that it's time to start taking advantage of flexbox and accept that the inline-block fallback won't support everything.
Where flexbox isn't supported, your basic grid structure will remain intact and most of the layout classes still work. Try this page out in a non-flexbox browser to see for yourself!
ul
as a grid<div class="grid"> <div class="grid__col-6"> <content> </div> <div class="grid__col-6"> <content> </div> </div>
With reference to BEM syntax, this is the anamtomy of a reflex grid:
example: grid
This is the top level element and is similar to a row
in bootstrap. It sets the following properties:
display: flex
flex-wrap: wrap
example: grid__col-xs-6
This is a child element of .grid
and is similar to a col-x
in bootstrap. It sets the following properties:
display: flex
flex-direction: column
example: grid--align-end
Modifier classes can be applied to both the grid
and grid__element
. Further examples include:
grid--no-wrap
grid--align-end
grid--justify-center
Reflex falls back to an inline-block grid for browsers that do not support flexbox. Reflex uses some css hacks to help the inline-block grid work on older browsers. It's important to note that some of the features of flexbox do not have an equivalent in the inline-block fallback so not everything will work in the fallback, but your grid will remain intact.
If you want to use the grid with breakpoints in ie8 and below you will need to use respond.js which enables media query support in those browsers via JavaScript and html5Shiv if you wish to use html5 elements such as section
It is possible for this framework to support ie6. You can make a tiny change to enable support but it greatly increases the filesize of the css. Look in _mixins.scss (Sass) or mixins.less (LESS) for the commented line.
You don't have to use div
for your grid and ideally you should use the most semantically appropriate markup for your use case with no visible difference.
<div class="grid"> <div class="grid__col-6"> <content> </div> </div>
<ul class="grid"> <li class="grid__col-6"> <content> </li> </ul>
<section class="grid"> <article class="grid__col-6"> <content> </article> </section>
By default reflex is a 12 column grid but this can be easily changed. Regardless of how many columns you need, you will likely require extra control over the breakpoints at which your grid columns revert to 100% width. To do this you will need to use these modifier classes which determine grid columns at various viewport sizes. A grid built using numbered columns in this way will remain fully intact in the inline-block fallback.
The reflex grid breakpoints can be easily re-defined but are set to these values by default:
Class prefix | grid__col- | grid__col-xs- | grid__col-sm- | grid__col-md- | grid__col-lg- | grid__col-xlg- |
---|---|---|---|---|---|---|
Collapses at | does not collapse | < 480px | < 768px | < 992px | < 1200px | < 1600px |
You can add any number of auto sizing grid__col-auto
columns to a grid
and therefore avoid using numbered columns. You can also use the regular breakpoints to determine when your columns revert to 100% width. A grid built using auto columns will not remain fully intact in the inline-block fallback and will not work properly in ie10 due to it's limited flexbox support but if this is a concern, you could use JavaScript for that.
To understand how this grid works, you'll need to first understand how flexbox works, especially in relation to the cross axis and main axis. There's a pretty good introduction to flexbox here but here's a quick demo of the main points: flex direction, main axis, and cross axis.
↔ main axis
↕ cross axis
↕ main axis
↔ cross axis
By default any grid
element is set to flex-direction: row
and the main axis therefore follows the horizontal direction.
By default any grid__col-x
element is set to flex-direction: column
and the main axis therefore follows the vertical direction.
This will become important when you are using the modifier classes which typically act upon either the main or cross axis
By default, the flex-wrap
property of a display: flex
element is set to nowrap
but because this is a grid framework we generally need our grid to wrap so we explcitly set flex-wrap: wrap
on our grid
element
grid__col-x
elements do not wrap by default, but you can make them do so by using the modifier grid--wrap
.
In the inline-block fallback your grid columns will always wrap when they add up to 100% width (12 column by default).
When applied to grid
sets flex-wrap: nowrap
and gives you this:
these
columns
do not collapse
When applied to grid
sets flex-wrap: wrap-reverse
and gives you a grid that wraps in reverse:
I am first in the markup
I am second in the markup
I am third in the markup
These modifiers can be added to your grid
element but also to the grid__col-x
elements inside the grid.
In browsers that support flexbox we have equal height columns by default because the default value of align-items
is stretch
First appear years night there the in them rule.
Over after behold was living together tree is very sixth let bring fish. Forth behold they're fly deep.
Be can't winged good for also saying first.
When applied to grid
or grid__col-x
sets align-items: flex-start
and gives you this:
First appear years night there the in them rule.
Over after behold was living together tree is very sixth let bring fish. Forth behold they're fly deep.
Be can't winged good for also saying first.
When applied to grid
or grid__col-x
sets align-items: flex-end
First appear years night there the in them rule.
Over after behold was living together tree is very sixth let bring fish. Forth behold they're fly deep.
Be can't winged good for also saying first.
When applied to grid
or grid__col-x
sets align-items: center
First appear years night there the in them rule.
Over after behold was living together tree is very sixth let bring fish. Forth behold they're fly deep.
Be can't winged good for also saying first.
When applied to grid
or grid__col-x
sets align-items: baseline
.
By default, different text elements in adjacent columns will be aligned by their size and margin:
Paragraph
With grid--align-baseline
applied to the grid
element the text lines up:
Paragraph
You can override the cross axis alignment of a single grid__col-x
element by using these modifers.
When applied to grid__col-x
sets align-self: stretch
First appear years night there the in them rule that bring fly gathered there is place good.
stretch
First appear years night there the in them rule that bring fly gathered there is place good Created rule image night firmament.
When applied to grid__col-x
sets align-self: flex-start
First appear years night there the in them rule that bring fly gathered there is place good.
start
First appear years night there the in them rule that bring fly gathered there is place good Created rule image night firmament.
When applied to grid__col-x
sets align-self: flex-end
First appear years night there the in them rule that bring fly gathered there is place good.
end
First appear years night there the in them rule that bring fly gathered there is place good Created rule image night firmament.
When applied to grid__col-x
sets align-self: center
First appear years night there the in them rule that bring fly gathered there is place good.
center
First appear years night there the in them rule that bring fly gathered there is place good Created rule image night firmament.
These modifers set the value of align-content
which determine the position of your grid elements when there is extra space available in the cross-axis (usually because you've set a height on the grid that is larger than the contents). These modifiers can be added to your grid
element but also to the grid__col-x
elements inside the grid.
This modifier has no effect when there is only one line of grid__col-x
elements in the grid so cannot be used when grid--no-wrap
is present on the grid or when you only have a single row of columns
In browsers that support flexbox we have equal height columns that stretch to fill the available space by default because the default value of align-content
is stretch
First appear
Great blessed
Years night
Firmament you
To lights
Place good
When applied to grid
or grid__col-x
sets align-content: flex-start
First appear
Great blessed
Years night
Firmament you
To lights
Place good
When applied to grid
or grid__col-x
sets align-content: flex-end
First appear
Great blessed
Years night
Firmament you
To lights
Place good
When applied to grid
or grid__col-x
sets align-content: center
First appear
Great blessed
Years night
Firmament you
To lights
Place good
When applied to grid
or grid__col-x
sets align-content: space-between
First appear
Great blessed
Years night
Firmament you
To lights
Place good
When applied to grid
or grid__col-x
sets align-content: space-around
First appear
Great blessed
Years night
Firmament you
To lights
Place good
These modifers set the value of justify-content
which determine the position of your grid elements when there is extra space available in the main-axis (usually because your columns do not make up the full width of a grid). These modifiers can be added to your grid
element but also to the grid__col-x
elements inside the grid.
Grid elements are aligned to the left by default because the default value of justify-content
is flex-start
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
When applied to grid
or grid__col-x
sets justify-content: flex-end
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
When applied to grid
or grid__col-x
sets justify-content: center
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
When applied to grid
or grid__col-x
sets justify-content: space-between
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
First appear years night there the in them rule.
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
When applied to grid
or grid__col-x
sets justify-content: space-around
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
First appear years night there the in them rule.
First appear years night there the in them rule.
Be can't winged good for also saying first. Shall, fourth Greater cattle.
These modifers set the value of flex-direction
. These modifiers can be added to your grid
element but also to the grid__col-x
elements inside the grid.
grid
elements are in rows by default because the default value of flex-direction
is row
. grid__col-x
elements are columns by default but can be set to behave as rows by using grid--direction-row
I am first in the markup
I am second in the markup
I am third in the markup
I am fourth in the markup
I am fifth in the markup
When applied to grid
or grid__col-x
sets flex-direction: row-reverse
I am first in the markup
I am second in the markup
I am third in the markup
I am fourth in the markup
I am fifth in the markup
When applied to grid
or grid__col-x
sets flex-direction: column
I am first in the markup
I am second in the markup
I am third in the markup
When applied to grid
or grid__col-x
sets flex-direction: column-reverse
I am first in the markup
I am second in the markup
I am third in the markup
These helper classes allow you to easily set order
to your grid elements. This is useful because it allows you to have your markup in the correct semantic order for accessiblity purposes but in a different order visually.
By default there are 12 ordering helpers to match the default 12 columns. Changing the number of columns by editing the variable will also produce a matching number of order helper classes
When applied to grid__col-x
sets order: n
I am first in the markup but ordered second
I am second in the markup but ordered third
I am third in the markup but ordered first
There are responsive ordering classes that correspond to both the number of columns and the breakpoints so you can choose to change the order of grid__col-x
elements only above certain viewport sizes.
For example: .grid--order-2-md
When applied to grid__col-x
sets order: n
when viewport size is above the selected breakpoint.
I am first in the markup but ordered second when viewport size is above 992px
I am second in the markup but ordered third when viewport size is above 992px
I am third in the markup but ordered first when viewport size is above 992px
There are some nuances with flexbox that may make your life difficult if you're not expecting them. There is a wealth of information available on this subject in the flexbox working draft.
This is part of flexbox, and is entirely expected. However, flexbox layout is not always appropriate so it's possible to add a grid__cell
child element to your grid__col-x
columns which defines an area of your column as display: block
allowing you to lay out the contents of your grid elements like you would normally.
auto
absorb extra space and can be used for alignmentfloat
, clear
and vertical-align
have no effect on a flex item<div class="grid"> <div class="grid__col-6"> <content> </div> <div class="grid__col-6"> <content> </div> </div>
float: right
float: left
The top and bottom margins on these paragraphs do not collapse together as you would expect
The top and bottom margins on these paragraphs do not collapse together as you would expect
<div class="grid"> <div class="grid__col-6"> <div class="grid__cell"> <content> </div> </div> <div class="grid__col-6"> <div class="grid__cell"> <content> </div> </div> </div>
float: right
float: left
The top and bottom margins on these paragraphs collapse together as you would expect
The top and bottom margins on these paragraphs collapse together as you would expect
If you're using a grid__cell
as above, you can simply use your regular responsive image solution such as the solution used in bootstrap.
If you're not using a grid__cell
, flex images need to be handled differently to make them responsive and you should use grid__cell-img
.
<div class="grid"> <div class="grid__col-sm-4"> <img class="grid__cell-img" src=""> </div> </div>
It's possible to set up a footer section in a grid__col-x
element that is always aligned to the bottom by using grid__cell-footer
. This footer element is set up as a flex box so all the usual grid modifers work on it.
Consectetur adipiscing elit, sed do eiusmod.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc et sagittis dui. Maecenas rhoncus justo aliquam urna finibus tincidunt et ut mauris.
By default, grid__col-x
elements have padding to space the grid out. It's possible to override this in all grid__col-x
child elements in a grid by applying the modifer grid--bleed
to your grid
element.
Consectetur adipiscing elit, sed do eiusmod.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc et sagittis dui. Maecenas rhoncus justo aliquam urna finibus tincidunt et ut mauris.
You can override the padding on individual grid__col-x
elements by using grid__col--bleed
.
Default
grid__col--bleed
applied
You can override the left and right padding on individual grid__col-x
elements by using grid__col--bleed-x
.
Default
grid__col--bleed-x
applied
You can override the top and bottom padding on individual grid__col-x
elements by using grid__col--bleed-y
.
Default
grid__col--bleed-y
applied
You can add a background image to grid__col-x
elements by using grid__col--bg
.
You can add a background image to grid__col-x
elements by using grid__col--bg
.
You can apply padding to individual grid__cell
elements by using grid__cell--padding-x
.
padding-sm
padding-md
padding-lg
You can use some generic visibility helpers that can be applied to any element, not just grid elements. resize the viewport to see them in action. The breakpoints can be changed if you're building the css via less or sass/SCSS.
I would like to acknowledge the following resources which all helped in some way while I was building this: