In-depth introduction to flexbox
Modern CSS is loaded with features; many things that in the past required JavaScript and tooling now can be done with plain CSS. If I were to recommend one feature that is worth learning in detail, it would be flexbox. With the flexbox, you can build most of the layouts that you can think of. Element positioning and sizing is much simpler. Even dreaded horizontal alignment is not a big deal. It really saves a lot of time. In cases where flexbox is not enough, there is also display: gird. The bonus of learning flexbox in depth is that alignment specification for flexbox is also used for the grid, so learning grid afterwards is a breeze. The article is long and may feel dry at moments but bear with me. You won't regret it. At the end of the article, I offer a few simple examples where flexbox makes life much easier.
Flexbox positions elements within flex container across one axis (called the main axis). By default, this axis goes horizontally from left to right. To create a flex container set display: flex
on the element. Every item inside the flex container becomes a flex item. The axis perpendicular to the main axis is called the cross axis.
You can change the direction of the main and cross axis by setting flex-direction property on the flex container. Possible values are: row
(default), column
, row-reversed
, column-reversed
. In most examples, I'll be working with row
. The reversed variants are sometimes useful but for the purpose of this article we will stick to the row
.
Flexbox, by default, will allow items to take as much space as they need, given, of course, the container is big enough. We change it by setting flex-basis
on the flex items. This property sets preferred size across the main axis. This is the size that will be set if there is no need to grow or shrink items.
Since we work with the default setting for the main axis (row), in our case flex-basis
is influencing the width of the items. For flex-direction: column
it will influence the height of the items.
If we want to get this nice responsive experience where items just fit inside a container no matter the size of the container, we have to instruct the flexbox how it can manipulate items. flex-grow
property set on flex item will allow it to grow if there is space available. See the following example.
The value of flex-grow
is a number. Flexbox will get flex-grow
values from all items and grow them proportionally to their values. In the example, the first item has flex-grow: 1
and the second item has flex-grow: 4
. The first item grew 55 pixels (from 120 set in flex-basis
) and the second item grew 220 pixels (4*55).
You don't have to set flex-grow for all items. A good example is a site header containing a menu on the left and a user avatar on the right. Instead of setting float on divs and horizontal alignment through elaborate margins and padding, you can use flexbox (I'll show you how to easily accomplish reliable horizontal alignment later ).
flex-grow
works only if there is spare space in the container. What if items flex-basis
add up to a bigger number than the space across the main axis? Flexbox will shrink all items in equal proportion to fit items in the container. We can manipulate proportions by setting flex-shrink
on the items.
As you can see, it works analogously to flex-grow
. The second element has been shrunken proportionally more than the first one. The best workflow is to start with the smallest acceptable container and work out behaviour for bigger containers using flex-grow and other properties.
There is another option for dealing with situations where there is not enough space on the main axis. We can allow flexbox to wrap content. Set flex-wrap: wrap
on the container.
Flexbox still works along one axis here it just wraps axis around to the next row.
Main axis alignment
So far in all examples, we were manipulating flex items sizes. Some times we want our times to have a defined size no matter how big the container is and we want to justify them inside the container. For that cases flexbox allows us to manipulate/distribute remaining space in different ways. For that purpose, we have justify-content
property that we can set on a flex container. Possible values for this property are: flex-start
, flex-end
, center
, space-between
, space-around
, space-evenly
. See the below examples.
justify-content
is good enough if our items have exactly the same height (for rows) or width (for columns). If items have different dimensions on the cross-axis the outline will be ragged.
If it is not what we want we can manipulate alignment on the cross-axis using align-items
property on the flex container. Possible values are: flex-start
, flex-end
, center
, baseline
, stretch
. For baseline
and stretch
to work you can't set cross-axis dimension on items. The dimensions must come from the content inside the item.
This is quite useful and powerful. Remember the example with the header containing the menu and avatar? If you use floats and margin/padding to align them; it is enough that someone changes menu height and you have to go and fix alignment on the avatar. Using flex you can position item horizontally and vertically no matter what size items have.
Cross axis alignment
There is one more area where we can run into alignment issues; it is when we allow flexbox to wrap content and container is bigger than what is needed on cross axis. To position items in multi row/column container, we use align-content
property set on the flex container. The possible values for this property are: flex-start
, flex-end
, center
, space-between
, space-around
, space-evenly
.
App layouts
Flexbox is perfect for building layouts. Say, you build an app where you need a header, footer, side panel and main area. The header and footer have constant height and take all horizontal space. The sidebar has a constant width and takes available vertical space. The main area takes the remaining horizontal and vertical space. Something like this:
You can decompose this layout into two parts. First flex column (header, sidebar with main area and footer are flex items here). Sidebar and main area form a flex row.
Positioning initials within avatar
The goal here is to center horizontally and vertically users initials withing avatar badge.
Center icon and multi-line text
For this example I’m omitting irrelevant styling.