Flexbox is a layout model that makes aligning and distributing elements in a row or column straightforward. In the DevBlog you’ll use it everywhere: navigation, cards, header, footer.
Enabling Flexbox
.container {
display: flex;
}
That’s all it takes for child elements to line up in a row.
Container properties
flex-direction — axis direction
.container {
display: flex;
flex-direction: row; /* default: left to right */
/* flex-direction: row-reverse; right to left */
/* flex-direction: column; top to bottom */
/* flex-direction: column-reverse; bottom to top */
}
justify-content — alignment along the main axis
The main axis follows the flex-direction (horizontal by default).
.container {
justify-content: flex-start; /* default: push to the left */
/* justify-content: flex-end; push to the right */
/* justify-content: center; center horizontally */
/* justify-content: space-between; ends at edges, rest evenly spaced */
/* justify-content: space-around; equal space around each item */
/* justify-content: space-evenly; strictly equal gaps */
}
align-items — alignment along the cross axis
The cross axis is perpendicular to the main axis (vertical by default).
.container {
align-items: stretch; /* default: stretch to container height */
/* align-items: flex-start; align to top */
/* align-items: flex-end; align to bottom */
/* align-items: center; center vertically */
/* align-items: baseline; align text baselines */
}
gap — spacing between items
.container {
display: flex;
gap: 16px; /* same gap on both axes */
/* gap: 16px 24px; vertical | horizontal */
}
Replaces the old approach of adding margin to each child.
flex-wrap — wrap to the next line
.cards-grid {
display: flex;
flex-wrap: wrap; /* wrap items if they don't fit */
/* flex-wrap: nowrap; default: all on one line */
gap: 24px;
}
Item properties
flex — how an item grows and shrinks
Shorthand for flex-grow flex-shrink flex-basis.
.sidebar {
flex: 0 0 280px; /* don't grow, don't shrink, fixed 280px */
}
.main-content {
flex: 1; /* fill remaining space */
}
order — display order
.featured-post {
order: -1; /* show first regardless of HTML order */
}
align-self — alignment for a specific item
.cta-button {
align-self: flex-end; /* push to the bottom of the container */
}
DevBlog: navigation bar
<header class="site-header">
<a href="/" class="logo">DevBlog</a>
<nav class="site-nav">
<a href="/" class="nav-link">Home</a>
<a href="/articles/" class="nav-link">Articles</a>
<a href="/about/" class="nav-link">About</a>
</nav>
</header>
.site-header {
display: flex;
justify-content: space-between; /* logo left, nav right */
align-items: center;
padding: 0 24px;
height: 56px;
border-bottom: 1px solid #e5e7eb;
}
.logo {
font-weight: 700;
font-size: 1.25rem;
color: #111827;
text-decoration: none;
}
.site-nav {
display: flex;
gap: 24px;
}
.nav-link {
color: #374151;
text-decoration: none;
font-weight: 500;
}
DevBlog: wrapping card row
<section class="posts-grid">
<article class="post-card">...</article>
<article class="post-card">...</article>
<article class="post-card">...</article>
</section>
.posts-grid {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.post-card {
flex: 1 1 300px; /* minimum 300px wide, can grow */
max-width: 400px;
padding: 24px;
border: 1px solid #e5e7eb;
border-radius: 8px;
}
On a wide screen — 3 cards per row. On a narrow screen — each card takes the full width.
DevBlog: two-column article layout
<div class="article-layout">
<main class="article-content">...</main>
<aside class="article-sidebar">...</aside>
</div>
.article-layout {
display: flex;
gap: 48px;
align-items: flex-start; /* don't stretch columns to full height */
max-width: 1100px;
margin: 0 auto;
padding: 40px 24px;
}
.article-content {
flex: 1; /* fill remaining space */
min-width: 0; /* allow shrinking — important in flex! */
}
.article-sidebar {
flex: 0 0 260px; /* fixed 260px width */
}
Quick reference
| Property | On container | Values |
|---|---|---|
flex-direction |
Yes | row, column, row-reverse, column-reverse |
justify-content |
Yes | flex-start, center, space-between, space-evenly |
align-items |
Yes | stretch, center, flex-start, flex-end |
gap |
Yes | 16px, 16px 24px |
flex-wrap |
Yes | nowrap, wrap |
flex |
On item | 1, 0 0 300px, 1 1 auto |
align-self |
On item | same as align-items |
order |
On item | number |
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!