</> ConvertAtlas Developer Tools
Level 3 · Advanced — Modern CSS Lesson 29 Advanced 14 min

Advanced Flexbox Patterns

Sticky footers, equal-height cards, wrapping toolbars and the centering recipes pros reuse.

Flexbox shines for component-level layout. These reusable patterns solve problems that used to take hacks — keep them in your toolkit and you will reach for them constantly.

A footer that sits at the bottom even when content is short. Make the page a flex column at least as tall as the viewport, and let the main area grow.

HTML
<div class="app"><header>Header</header><main>Short content</main><footer>Always at the bottom</footer></div>
CSS
.app{display:flex;flex-direction:column;min-height:220px}
header,footer{background:#1e293b;color:#fff;padding:10px}
main{flex:1;background:#e2e8f0;padding:12px}
Result
main { flex: 1 } pushes the footer down.

Equal-height cards

Flex items in a row are equal height by default (<code>align-items: stretch</code>). Inside each card, make it a flex column and push the button down with <code>margin-top: auto</code> so footers align across cards.

HTML
<div class="row"><div class="card"><p>Short</p><a>Go</a></div><div class="card"><p>A longer paragraph that wraps</p><a>Go</a></div></div>
CSS
.row{display:flex;gap:10px}
.card{flex:1;display:flex;flex-direction:column;background:#fff;border:1px solid #e2e8f0;border-radius:8px;padding:14px}
.card a{margin-top:auto;color:#2563eb}
Result
Equal heights; buttons align via margin-top: auto.

Push-apart and wrapping toolbars

A single <code>margin-left: auto</code> on one item pushes it and everything after it to the far end — great for toolbars. Add <code>flex-wrap: wrap</code> so toolbars degrade gracefully on small screens.

HTML
<div class="bar"><b>Title</b><button>A</button><button class="end">Right</button></div>
CSS
.bar{display:flex;gap:8px;align-items:center;flex-wrap:wrap;background:#f1f5f9;padding:10px;border-radius:8px}
.end{margin-left:auto}
Result
margin-left: auto pushes an item to the right.

Reliable centering

The most reliable centering in CSS: a flex container with <code>justify-content: center</code> and <code>align-items: center</code>. Works for one item or many, horizontally and vertically.

Common mistake
Don't
.col { float: left; min-height: 300px; }
Do
.row { display: flex; } /* children are equal height */

Using margins and floats to fake equal-height columns. Flex items stretch to equal height automatically.

Best practice

Memorise four patterns: flex column + min-height for sticky footers, flex: 1 + margin-top: auto for card footers, margin-left: auto to push apart, and flex centering. They cover most component layouts.

Try it yourself

Build a card row where cards are equal height and their buttons align at the bottom. Then build a toolbar with a title on the left and a button pushed to the right with margin-left: auto.

Quick quiz

1. What pushes a sticky footer down?

2. How do flex items size their height by default?

3. What pushes one item to the far end?

Key takeaways
  • Sticky footer: flex column + min-height + flex: 1 on main.
  • Equal-height cards stretch by default; align footers with margin-top: auto.
  • margin-left: auto pushes an item (and the rest) to the end.
  • Flex centering (justify + align center) is the most reliable.