Flex Properties
Flexbox is a foundational layout model that gives you precise control over the arrangement of items in modern web design. In this section, we’ll explore the core properties that define how flex containers and items interact. These properties work together to create responsive, flexible interfaces while maintaining clean, predictable behavior. Let’s dive in!
justify-content
The justify-content property controls the alignment of flex items along the main axis (the direction items are laid out). This is your primary tool for distributing space horizontally within a flex container. It’s especially powerful for creating balanced layouts where items naturally flow and share available space.
Here’s how it works in practice:
| Value | Behavior |
|---|---|
flex-start |
Items align at the start of the container (default) |
flex-end |
Items align at the end of the container |
center |
Items centered within the container |
space-between |
Items spaced evenly with the first at the start and last at the end |
space-around |
Items spaced evenly with equal padding around each item |
space-evenly |
Items spaced evenly with equal spacing between items (including ends) |
Real-world example: Imagine a navigation bar with buttons that need equal spacing. Using space-around ensures consistent padding on all sides:
<code class="language-html"><div class="nav-container"> <p> <button>Home</button></p> <p> <button>About</button></p> <p> <button>Contact</button></p> <p></div></code>
<code class="language-css">.nav-container {
<p> display: flex;</p>
<p> justify-content: space-around;</p>
<p> padding: 10px;</p>
<p>}</code>
Key insight: This property only affects horizontal alignment. For vertical alignment, we use align-items (covered next). Remember: justify-content is your go-to for horizontal spacing control—especially when creating responsive designs where items need to shift dynamically.
align-items
The align-items property governs the alignment of flex items along the cross axis (the direction perpendicular to the main axis). This is your primary tool for vertical alignment within a flex container. It ensures items stack neatly and share vertical space consistently.
Here’s how it works in practice:
| Value | Behavior |
|---|---|
flex-start |
Items align at the top of the container (default) |
flex-end |
Items align at the bottom of the container |
center |
Items centered vertically within the container |
baseline |
Items align based on their baseline (text lines) |
stretch |
Items stretch to fill the cross axis (default for flex items) |
Real-world example: A card grid where items need consistent vertical spacing:
<code class="language-html"><div class="card-grid"> <p> <div class="card">Item 1</div></p> <p> <div class="card">Item 2</div></p> <p> <div class="card">Item 3</div></p> <p></div></code>
<code class="language-css">.card-grid {
<p> display: flex;</p>
<p> flex-direction: column;</p>
<p> align-items: stretch; /<em> Stretch cards to fill height </em>/</p>
<p> gap: 10px; /<em> We'll cover gap next </em>/</p>
<p>}</p>
<p>.card {</p>
<p> padding: 10px;</p>
<p> background: #f0f0f0;</p>
<p> min-height: 50px;</p>
<p>}</code>
Key insight: align-items is crucial for creating uniform vertical spacing. When combined with justify-content, it gives you complete control over both horizontal and vertical alignment. For complex layouts, pair it with flex-wrap to handle multi-line arrangements.
align-content
The align-content property controls the alignment of flex lines (the horizontal lines in multi-line flex containers) along the cross axis. This property only matters when your flex container wraps items into multiple lines (i.e., when flex-wrap is set to wrap or wrap-reverse). It’s your tool for fine-tuning vertical spacing between lines.
Here’s how it works in practice:
| Value | Behavior |
|---|---|
flex-start |
Lines align at the top of the container |
flex-end |
Lines align at the bottom of the container |
center |
Lines centered within the container |
space-between |
Lines spaced evenly with first line at top and last at bottom |
space-around |
Lines spaced evenly with equal padding around each line |
stretch |
Lines stretch to fill the cross axis (default for flex lines) |
Real-world example: A multi-line grid where lines need even spacing:
<code class="language-html"><div class="multi-line-grid"> <p> <div class="item">1</div></p> <p> <div class="item">2</div></p> <p> <div class="item">3</div></p> <p> <div class="item">4</div></p> <p> <div class="item">5</div></p> <p> <div class="item">6</div></p> <p></div></code>
<code class="language-css">.multi-line-grid {
<p> display: flex;</p>
<p> flex-direction: row;</p>
<p> flex-wrap: wrap; /<em> Triggers multi-line layout </em>/</p>
<p> align-content: space-between; /<em> Distributes lines evenly </em>/</p>
<p> gap: 10px;</p>
<p>}</p>
<p>.item {</p>
<p> min-width: 100px;</p>
<p> padding: 5px;</p>
<p> background: #e0e0e0;</p>
<p>}</code>
Key insight: This property is often misunderstood because it only applies to multi-line containers. If you don’t have flex-wrap: wrap, align-content has no effect. Use it when you need to control the vertical spacing between lines—especially in responsive grids.
flex-wrap
The flex-wrap property determines when flex items move to the next line within the container. This is essential for creating responsive layouts that adapt to varying screen sizes without breaking.
Here’s how it works in practice:
| Value | Behavior |
|---|---|
nowrap |
Items stay on a single line (default) |
wrap |
Items wrap to new lines when space is insufficient |
wrap-reverse |
Items wrap to new lines but in reverse order (mirrored) |
Real-world example: A responsive product grid that wraps on small screens:
<code class="language-html"><div class="product-grid"> <p> <div class="product">Product 1</div></p> <p> <div class="product">Product 2</div></p> <p> <div class="product">Product 3</div></p> <p> <div class="product">Product 4</div></p> <p></div></code>
<code class="language-css">.product-grid {
<p> display: flex;</p>
<p> flex-wrap: wrap; /<em> Items wrap to new lines </em>/</p>
<p> gap: 15px;</p>
<p>}</p>
<p>.product {</p>
<p> min-width: 200px;</p>
<p> padding: 10px;</p>
<p> background: #f0f0f0;</p>
<p>}</code>
Key insight: flex-wrap is your gateway to responsive layouts. Without it, items will overflow the container (causing layout breaks). Pair it with flex-direction (e.g., column for vertical wrapping) to create complex responsive patterns.
gap
The gap property sets spacing between flex items in both the main and cross axes. It’s a modern, powerful alternative to margin and padding for controlling item spacing. This property works with both flex and grid layouts (though it’s most relevant here for flex).
Here’s how it works in practice:
- Single value: Sets equal spacing in both directions (e.g.,
gap: 10px= 10px between items horizontally and vertically) - Two values: Sets different spacings (e.g.,
gap: 10px 5px= 10px horizontally, 5px vertically)
Real-world example: A clean, consistent spacing system for a dashboard:
<code class="language-html"><div class="dashboard"> <p> <div class="dashboard-item">Chart 1</div></p> <p> <div class="dashboard-item">Chart 2</div></p> <p> <div class="dashboard-item">Chart 3</div></p> <p></div></code>
<code class="language-css">.dashboard {
<p> display: flex;</p>
<p> flex-wrap: wrap;</p>
<p> gap: 12px 8px; /<em> 12px horizontal, 8px vertical </em>/</p>
<p>}</p>
<p>.dashboard-item {</p>
<p> padding: 10px;</p>
<p> background: #f0f0f0;</p>
<p> min-width: 150px;</p>
<p>}</code>
Key insight: gap simplifies spacing management by replacing multiple margin/padding rules with a single property. It’s especially valuable for mobile-first designs where consistent spacing is critical.
Summary
These five properties form the backbone of flexible web layouts:
justify-content→ Horizontal alignmentalign-items→ Vertical alignmentalign-content→ Multi-line vertical alignmentflex-wrap→ Line wrapping for responsivenessgap→ Consistent item spacing
Mastering them lets you build layouts that adapt seamlessly across devices while maintaining visual harmony. Remember: start simple (e.g., flex-wrap: wrap + gap), then refine with justify-content and align-items for precise control. Flexbox is your most versatile layout tool—use these properties to create interfaces that feel intentional and responsive.
💡 Pro tip: Always pair
gapwithflex-wrapfor clean, mobile-friendly grids. This combination handles spacing and wrapping without relying on margins or padding hacks.