A Long Time Coming#
Flexbox—short for flexible boxes, which folks often just shorten down to flex—is a
-
CSS Flexbox Layout Guide – CSS Tricks
This page is a classic! It probably bought Chris Coyier a house. -
Basic Concepts of Flexbox – MDN
Can’t go wrong. -
Flexbox Froggy
A little game.
Flex was created in the mid-2010s (with slow adoption) to facilitate and allow CSS layouts that the box model (despite its floatposition )display
And let us tell you—being a web designer was a whole lot harder before flex came on the front-end scene. (Hence the “Finally.”) Notice, for instance, that we haven’t talked about any vertical centering at all yet—you don’t want to know! And you don’t have to worry about it.
Flex encapsulates a lot of practical, helpful design paradigms in its system. It ushered in our era of modern web layout!
I was told that the “flexbox” model would solve the problems, but there hasn’t been any progress on that front these past few years…
Flex Concepts#
Main and Cross Axes#
Flexbox is a one-dimensional layout system—meaning it is (…usually) focused on arranging items either horizontally in rows, or vertically in columns.
These are called the axes. The one running in the direction of your flex items is your main axis; perpendicular to this is your cross axis:
.some-container {
display: flex;
flex-direction: row; /* Default! */
}
.some-container {
display: flex;
flex-direction: column; /* Rotated! */
}
Start vs. End, Justify vs. Align#
Flex also lets us position elements along/within the axes, in both directions—in relation to the startend
For the main axis, you justify ;align :
For rows (the default): justifyalign
For columns (rotated): justifyalign
Shorthand?#
Like a lot of CSS, flex has shorthand properties.
But again, we would generally avoid them—the system can be hard enough to understand. This will be true when we get to grid
Container (Parent) Properties#
display: flex; #
display: flex;Unlike most (…all?) of the CSS we’ve been introduced to, flex is applied on a parent element—but actually adjusts the layout of the children. An element with display: flex;
There is also display: inline-flex;inlineflex .
flex-direction #
flex-directionAfter specifying an element as flex, we can set its main axis with the flex-directionflex-direction: row; —flex-direction: column; :
flex-direction
Always row
The first list is display: block;min-block-size ,start /end !
You can also combine these with a -reversestartend :
Keep in mind that all flex reordering is only visual—it obviously can’t change the order in your HTML. This means that keyboard navigation and screen readers still sequence through the items as they are in your code. So for good, logical accessibility, keep in mind the semantic reading order!
flex-wrap #
flex-wrapSince flexbox is one-dimensional, by default it will try to cram everything into one line—even when there is not enough room! But you can tell it to wrap onto additional lines by adding the flex-wrap: wrap;nowrap
flex-wrap
The only two-dimensional flex use.
Without the height restriction, the last one would just grow taller, by default.
There is also a -reverseendstart :
You could do some weird, unique layouts with these—but keep in mind the order is still only visual!
justify-content #
justify-contentSo most of what we’ve seen here is… somewhat possible using floatposition —
justify-content
How should we space items out?
But the justify-contentjustify-content
The start /end
When our main axis is vertical, with flex-direction: column; :
These only works with the block-size
align-items #
align-itemsAnd then perpendicular to justifyalign-items
align-items
For the cross/perpendicular axis.
And for the vertical:
align-content #
align-contentWhen we have a flex element with flex-wrapalign-contentjustify-content
align-content
When there is extra room.
These wouldn’t do anything without the block-sizeflex-wrap .
align-contentflex-direction: column;
gap / column-gap / row-gap #
gapcolumn-gaprow-gapWhile you could use margin to separate your flex children, it would apply to the items on the outer edges, too. (Hence our many :not(:first-child)margin
– MDNgap – MDNcolumn-gap – MDNrow-gap
These are common with CSS grid!
Flex added support for more intuitive gap
Note the last one, gap
If we did a Flexbox spec workshop, where we went over the whole spec in excruciating detail and sketched illustrations, who would come?
Item (Child) Properties#
Flexbox is usually applied on the parent/container. But once you’ve set display: flex;
order #
orderKind of like the -reverseorderorder: 0; )
order
Visual/display only!
Other order-based selectors (like :first-child )
flex-grow / flex-shrink #
flex-growflex-shrinkThese properties tell the flex items to… growshrink ,
– MDNflex-grow – MDNflex-shrink
Who takes up the extra space?
It takes a unitless proportional value, akin to fractions or a factor/multiplier. If you give one flexed child flex-grow: 1;flex-grow: 2;
And flex-shrinkflex-shrink: 0; ,
flex-basis #
flex-basisThe flex-basisinline-sizeblock-size —
flex-basis
How much of the space.
This defaults to auto ,inline-sizeblock-size —flex-basis%px
You are usually fine just specifying inline-size /block-size .
align-self #
align-selfFinally, we have an individual override for an align-itemsalign-self
align-self
Overrides this child! Rebels.
This is a lot of stuff! Flex can sometimes be tough to wrap one’s head around, but it is so much better than floatinline-sizemargin
Much of what you look at on the web is laid out in flexbox (and its followup which we keep mentioning, CSS Grid).
Flexbox is a new layout model for CSS.
The contents of a flex container can be laid out in any direction, can be reordered, can be aligned and justified within their container, and can “flex”their sizes and positions to respond to the available space.
We expect this model to be particularly useful for UI layouts.