Go beyond the basics of Flexbox to master real-world layout patterns including holy grail layout, sticky footers, perfect centering, and responsive card grids using flex-grow, flex-shrink, and flex-basis.
The flex shorthand combines three properties that control how flex items share space:
flex-basis sets the initial size of a flex item before growing or shrinking occurs. It behaves like width (in a row) or height (in a column), but is Flexbox-aware. A value of 0 means the item starts with no size and grows purely based on flex-grow. A value of auto means the item uses its content size or explicit width/height as its starting point.
flex-grow determines how much of the remaining space an item should absorb. If three items have flex-grow: 1, they split extra space equally. If one has flex-grow: 2, it gets twice as much extra space as the others.
flex-shrink determines how much an item should shrink when there is not enough space. A value of 0 prevents shrinking entirely; the item keeps its flex-basis size even if it causes overflow.
/* Equal-width items */
.item { flex: 1 1 0; }
/* Fixed sidebar + fluid main */
.sidebar { flex: 0 0 250px; }
.main { flex: 1 1 0; }<style>
/* Pattern 1: Sticky footer layout */
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-header { padding: 16px; background: #1e293b; }
.page-main { flex: 1; padding: 16px; } /* grows to fill */
.page-footer { padding: 16px; background: #1e293b; }
/* Pattern 2: Equal-height cards */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.card {
flex: 1 1 200px; /* grow, shrink, min 200px */
display: flex;
flex-direction: column;
}
.card-body { flex: 1; } /* pushes footer down */
.card-footer { margin-top: auto; }
/* Pattern 3: Centering anything */
.center-box {
display: flex;
justify-content: center;
align-items: center;
min-height: 200px;
}
</style>
<div class="card-grid">
<div class="card">
<div class="card-body">Short content</div>
<div class="card-footer">Footer</div>
</div>
<div class="card">
<div class="card-body">Much longer content that takes more space</div>
<div class="card-footer">Footer</div>
</div>
</div>The holy grail layout is a classic web design pattern with a header, footer, and three columns (left sidebar, main content, right sidebar). It was notoriously difficult to achieve with floats, but Flexbox makes it straightforward.
.holy-grail {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.middle {
display: flex;
flex: 1;
}
.left-sidebar { flex: 0 0 200px; }
.main-content { flex: 1; }
.right-sidebar { flex: 0 0 200px; }The outer container uses flex-direction: column to stack header, middle, and footer vertically. The middle section is itself a flex container (in the default row direction) with the main content set to flex: 1 so it fills all available horizontal space.
This pattern also naturally produces a sticky footer because flex: 1 on the middle section pushes the footer to the bottom of the viewport when content is short.
If three flex items have flex-grow values of 1, 2, and 1, how is 400px of extra space distributed?