CSS FlexBox
CSS FlexBox
October 2022
1 WHY FLEXBOX
2 FLEX MODEL
For a long time, the only reliable cross-browser compatible tools available for creating CSS layouts were features
like floats and positioning. These work, but in some ways they're also limiting and frustrating.
The following simple layout designs are either difficult or impossible to achieve with such tools in any kind of
convenient, flexible way:
• Making all the children of a container take up an equal amount of the available width/height, regardless of how
much width/height is available.
• Making all columns in a multiple-column layout adopt the same height even if they contain a different amount of
content.
As you'll see in subsequent sections, flexbox makes a lot of layout tasks much easier. Let's dig in!
When elements are laid out as flex items, they are laid
out along two axes
• The main axis is the axis running in the direction the
flex items are laid out in (for example, as rows across
the page, or columns down the page.) The start and
end of this axis are called the main start and main
end.
• The cross axis is the axis running perpendicular to the
direction the flex items are laid out in. The start and
end of this axis are called the cross start and cross
end.
• The parent element that has display: flex set on it is
called the flex container.
• The items laid out as flexible boxes inside the flex
container are called flex items.
By default, flex items will all try to fit onto one line. You can change
that and allow the items to wrap as needed with this property.
• wrap: flex items will wrap onto multiple lines, from top to bottom.
• wrap-reverse: flex items will wrap onto multiple lines from bottom
to top.
This defines the alignment along the main axis. It helps distribute extra
free space leftover when either all the flex items on a line are inflexible,
or are flexible but have reached their maximum size. It also exerts
some control over the alignment of items when they overflow the line.
The gap property explicitly controls the space between flex items. It
applies that spacing only between items not on the outer edges.
FLEX-BASIS
FLEX-SHRINK
This defines the default size of an element before the
remaining space is distributed. It can be a length (e.g. 20%, This defines the ability for a flex item to shrink if
5rem, etc.) or a keyword. The auto keyword means “look at necessary.
my width or height property” (which was temporarily done by
the main-size keyword until deprecated).
The content keyword means “size it based on the item’s
content” – this keyword isn’t well supported yet, so it’s hard
to test and harder to know what its brethren max-
content, min-content, and fit-content do.
If set to 0, the extra space around content isn’t factored in. If set
to auto, the extra space is distributed based on its flex-grow value.
This is the shorthand for flex-grow, flex-shrink and flex-basis combined. The second and third
parameters (flex-shrink and flex-basis) are optional. The default is 0 1 auto, but if you set it with a
single number value, like flex: 5;, that changes the flex-basis to 0%, so it’s like setting flex-grow: 5;
flex-shrink: 1; flex-basis: 0%;.
It is recommended that you use this shorthand property rather than set the individual properties.
The shorthand sets the other values intelligently.