Seven Things You Should Know About Flexbox.
1.10.2016
Flexbox is a great CSS module for making clean and flexible layouts. But flexbox is a bit odd, it can be tricky to get the results you're looking for when you're just getting started. Here are some things you should know about flexbox:
-
display: flex;
goes on a parent element. Then all children of that element become flex items and will accept other flex properties. - Don't use
float
orbox-sizing: border-box;
with flex elements. It won't work they way that you'd expect from display or inline elements. -
flex-direction
defaults torow
to lay flex elements out horizontally. Sometimes, you wantcolumn
to lay elements out vertically instead. -
margin-[side]: auto;
makes the margin use up all the available space on that side. If multiple margins (in the same dimension) areauto
, they each take an even share of the available space -
flex-grow:
defaults to0;
where elements won't grow beyond the content's width. You probably wantflex-grow: 1
. Among other things, this handy for making all input elements stretch to use up the remaining space: -
flex-wrap:
defaults tonowrap
, where all elements will be stuffed on a single line (or a single column, forflex-direction: column;
). Alternately, you may wantflex-wrap: wrap;
to allow the elements to flow to multiple rows. - Flexbox is well-supported in all recent versions of Firefox, Chrome, Safari, Chrome, Edge, mobile browsers, and even passably-well supported in IE. See http://caniuse.com/#feat=flexbox.
For more about flexbox
, see:
- https://github.com/philipwalton/flexbugs
- http://flexboxin5.com/ (Backup mirror: https://github.com/cvan/flexboxin5)
- http://tympanus.net/codrops/css_reference/flexbox/
- http://flexboxfroggy.com/