Flexbox Layout Patterns — Common UI Layouts with CSS Flexbox
CSS Flexbox solves common UI layout problems: sticky footers, equal-height columns, and horizontal navigation. Here's how to implement the most common Flexbox layout patterns...
CSS Flexbox excels at one-dimensional layouts — navbars, footers, card rows, and column alignment. Here are the most common layout patterns built with Flexbox.
Use the Flexbox Generator to generate Flexbox CSS visually.
Sticky footer layout
A footer that sticks to the bottom even when content is short:
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
.content {
flex: 1; /* grows to fill all available space */
}
.footer {
/* stays at bottom naturally */
}
<body>
<header>Navigation</header>
<main class="content">Page content</main>
<footer>Footer</footer>
</body>
flex: 1 on the content makes it take all remaining space, pushing the footer down.
Horizontal navigation bar
.navbar {
display: flex;
align-items: center;
padding: 0 1.5rem;
height: 64px;
background: #1e293b;
}
.navbar-brand {
font-size: 1.25rem;
font-weight: bold;
color: white;
}
.navbar-links {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}
.navbar-end {
margin-left: auto; /* pushes to the right */
display: flex;
gap: 0.75rem;
align-items: center;
}
<nav class="navbar">
<a class="navbar-brand" href="/">Logo</a>
<ul class="navbar-links">
<li><a href="/docs">Docs</a></li>
<li><a href="/pricing">Pricing</a></li>
</ul>
<div class="navbar-end">
<button>Sign In</button>
<button>Get Started</button>
</div>
</nav>
Sidebar + main content layout
.layout {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 0 0 260px; /* fixed width, don't grow or shrink */
background: #f8fafc;
padding: 1.5rem;
}
.main {
flex: 1; /* take all remaining width */
padding: 2rem;
overflow: auto;
}
Equal height columns
.columns {
display: flex;
gap: 2rem;
}
.column {
flex: 1;
/* By default, align-items: stretch makes all columns equal height */
}
All columns automatically have the same height as the tallest column (default Flexbox behavior with align-items: stretch).
Card row with equal sizing
.card-row {
display: flex;
gap: 1.5rem;
flex-wrap: wrap;
}
.card {
flex: 1 1 280px; /* grow, shrink, min-width 280px */
max-width: 400px;
}
flex-grow: 1— cards fill available space equallyflex-basis: 280px— minimum before wrappingmax-width: 400px— cap individual card width
Centering a modal/dialog
.modal-overlay {
display: flex;
justify-content: center;
align-items: center;
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
}
.modal {
background: white;
border-radius: 12px;
padding: 2rem;
max-width: 500px;
width: 90%;
}
Pricing cards with aligned features
.pricing-grid {
display: flex;
gap: 2rem;
align-items: stretch; /* cards all same height */
}
.plan {
flex: 1;
display: flex;
flex-direction: column;
padding: 2rem;
border: 1px solid #e2e8f0;
border-radius: 12px;
}
.plan-features {
flex: 1; /* pushes button to bottom */
}
.plan-button {
margin-top: auto;
}
Cards have equal height; the CTA button is always at the bottom regardless of feature list length.
Media object pattern
.media {
display: flex;
gap: 1rem;
align-items: flex-start;
}
.media-image {
flex: 0 0 64px; /* fixed-size image */
}
.media-body {
flex: 1; /* text takes remaining space */
}
Used for comment threads, notifications, user profile rows.
Inline form with input and button
.search-form {
display: flex;
gap: 0.5rem;
max-width: 480px;
}
.search-input {
flex: 1; /* input grows to fill available width */
min-width: 0; /* prevents overflow in Safari */
}
.search-button {
flex: 0 0 auto; /* button stays at its natural width */
white-space: nowrap;
}
min-width: 0 on the input is important — flex items default to min-width: auto which can cause overflow with text inputs.
Footer columns
.footer {
display: flex;
flex-wrap: wrap;
gap: 3rem;
padding: 3rem;
background: #0f172a;
}
.footer-column {
flex: 1 1 200px; /* min 200px, then grow equally */
}
.footer-links {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
Related tools
- Flexbox Generator — generate Flexbox CSS visually
- Flexbox Cheatsheet — all Flexbox properties
- Flexbox Centering — centering techniques
Related posts
- CSS Flexbox Cheatsheet — Every Property with Visual Examples — CSS Flexbox has 12 container properties and 6 item properties. Here's a complete…
- CSS flex-order Property — Reorder Flex Items Without Changing HTML — The CSS order property reorders flex items visually without changing the DOM. Le…
- CSS Flexbox — Complete Guide to flex-direction, justify-content, and align-items — Flexbox aligns items in one dimension — a row or a column. Here's how flex-direc…
- CSS Grid vs Flexbox — When to Use Each Layout System — CSS Grid and Flexbox solve different layout problems. Grid is for two-dimensiona…
- Flexbox Centering — Center Elements Vertically and Horizontally — Flexbox makes centering easy with just 3 CSS properties. Here's how to center co…
Related tool
Visual CSS Flexbox playground. Live preview + copyable CSS. Control direction, wrap, justify, align, gap. Example child layouts.
Written by Mian Ali Khalid. Part of the Frontend & Design pillar.