

See the Pen Smashing Flexbox Series 2: center an item by Rachel Andrew ( on CodePen. Alignment became as simple as two lines of CSS: So the ability to properly align items and groups of items was for many of us the most exciting thing about Flexbox when it first started to show up in browsers.

If you have ever been confused about when to align and when to justify, I hope this article will make things clearer! History Of Flexbox Alignmentįor the entire history of CSS Layout, being able to properly align things on both axes seemed like it might truly be the hardest problem in web design. This time we will take a look at the alignment properties, and how these work with Flexbox. In the first article of this series, I explained what happens when you declare display: flex on an element. As flex-shrink and flex-basis work as poorly as they do, the general recommendation from here is to refrain from using this variable.In this article, we take a look at the alignment properties in Flexbox while discovering some basic rules to help remember how alignment on both the main and cross axis works. The syntax is "flex: flex-grow flex-shrink flex-basis" where the default is "flex: 0 1 auto". In stead of using flex-grow, flex-shrink and flex-basis, you can use a collective variable called flex. The relative sizes (percents) and auto don't work properly, and there is no advantage in using this parameter, compared to the use of width. The absolute sizes (px, cm, etc.) works fine, but here you might as well use width. With this parameter, you can specify a default size for an item, from which the sizes of the other items are calculated. Yet another parameter that is supposed to work, but doesn't work well in practice, is flex-basis. Unfortunately the width of item 2 does not change, no matter what value you specify for flex-shrink, so you cannot in any way control the width. Item 2 has become narrower, as it was supposed to. When you got the arrangement of the items in place, using rows or columns, you can start working with the alignment. The code takes up less lines, but the trade off is readability of the code if you are untrained. If you are not used to work with flexbox, it is, like any other combination parameter, something I recommend you don't using. you can use the variables from these parameter, separated by space. There is a parameter called flex-flow, which is a combination of flex-direction and flex-wrap, i.e. Column: from top to bottom.Īrranged in multiple lines/columnds, depending on the available space. Item 1 Item 2 Item 3 Item 4 Item 5 Item 6įor the parameter flex-wrap you have the following options:Īrranged in multiple lines/columnds, depending on the available space.
