Flexbox Deep Dive
Master one-dimensional layouts with flex containers, axes, alignment, sizing, wrapping, ordering, and real-world patterns.
Flex Container and Main Axis
Flexbox is a one-dimensional layout system. A flex container distributes its direct children along a main axis and cross axis.
display: flex creates the flex formatting context. flex-direction determines the main axis, while justify-content primarily operates along the main axis and align-items operates along the cross axis.
.toolbar {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 1rem;
}- Flexbox is primarily one-dimensional.
- The main axis depends on
flex-direction.
With `flex-direction: row`, which direction is the main axis?
Alignment with justify-content and align-items
justify-content distributes free space along the main axis. align-items aligns flex items on the cross axis. align-self overrides the cross-axis alignment for an individual item.
The exact physical direction depends on writing mode and flex-direction, so thinking in terms of main and cross axes is more robust than memorizing left/right behavior.
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
}justify-contentworks primarily on the main axis.align-itemsworks primarily on the cross axis.
In a default row flex container, which property primarily controls horizontal free-space distribution?
flex-grow, flex-shrink and flex-basis
The flex shorthand combines flex-grow, flex-shrink, and flex-basis.
flex-grow determines how an item can receive positive free space. flex-shrink determines how it can shrink when insufficient space exists. flex-basis establishes the initial main-size contribution before free-space distribution.
Understanding these three values is critical for avoiding mysterious width behavior.
.sidebar {
flex: 0 0 240px;
}
.content {
flex: 1 1 auto;
min-width: 0;
}min-width: 0 allows long content to shrink instead of forcing overflow.flex-basiscontrols the initial main-size contribution.flex: 1is a common way to distribute available space.min-width: 0is frequently important inside flex layouts.
Which flex property controls an item's initial main-size contribution?
Wrapping and Gap
By default, flex items remain on one line. flex-wrap: wrap allows them to form multiple lines when necessary. The gap property provides consistent spacing between flex items without requiring margins.
Flexbox wrapping is useful for toolbars, tag lists, button groups, navigation items, and card rows where the number of items can change.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 280px;
}flex-wrapallows items to move onto additional lines.gapis cleaner than manually managing margins between items.
What does `flex-wrap: wrap` allow?
Real-World Flexbox Patterns
Flexbox is excellent for navigation bars, media objects, vertically centered components, button groups, toolbars, and layouts where content needs to distribute along one primary axis.
A common pattern is margin-inline-start: auto, which pushes an item toward the end of the main axis without requiring a hardcoded spacer.
.nav {
display: flex;
align-items: center;
gap: 1rem;
}
.nav__actions {
margin-inline-start: auto;
}- Flexbox is ideal for one-dimensional component layouts.
- Auto margins can be powerful alignment tools.
What effect can `margin-inline-start: auto` have inside a horizontal flex container?