What Are CSS Flexbox Properties?
CSS Flexbox provides a collection of properties that control how elements are arranged, aligned, sized, wrapped, and distributed inside a flexible layout. Understanding these properties is essential for building navigation bars, responsive cards, toolbars, dashboards, forms, headers, and other modern web interfaces.
Flexbox properties are divided into two main groups: properties for the flex container and properties for the flex items. The container controls the overall layout, while individual items can control their own size, order, and alignment.
Flex Container Properties
displayflex-directionflex-wrapflex-flowjustify-contentalign-itemsalign-contentgapFlex Item Properties
orderflex-growflex-shrinkflex-basisflexalign-selfWhat Is display: flex?
The first step in using Flexbox is making an element a flex container.
You do this with display: flex.
.container {
display: flex;
}
Once an element becomes a flex container, its direct children become flex items. Flexbox then controls how those items are positioned inside the container.
<div class="container">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
What Does flex-direction Do?
The flex-direction property determines the direction
of the main axis and therefore controls how flex items are laid out.
| Value | Purpose |
|---|---|
| row | Places items along the row direction. |
| row-reverse | Places items in the reverse row direction. |
| column | Places items vertically. |
| column-reverse | Places items vertically in reverse order. |
</pre>
<p class="mt-5">
Remember that Flexbox has a <strong class="text-white">main axis</strong> and a
<strong class="text-white">cross axis</strong>. Changing the direction changes how
properties such as <code class="text-blue-300">justify-content</code> and
<code class="text-blue-300">align-items</code> behave.
</p>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Does flex-wrap Do?
</h2>
<p class="mb-4">
By default, flex items try to remain on a single line. The
<code class="text-blue-300">flex-wrap</code> property allows items to move onto additional
lines when the available space is not sufficient.
</p>
<div class="grid md:grid-cols-3 gap-4">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">nowrap</h3>
<p class="text-sm mt-2 text-slate-400">
Keeps the items on one flex line.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">wrap</h3>
<p class="text-sm mt-2 text-slate-400">
Allows items to create additional flex lines.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">wrap-reverse</h3>
<p class="text-sm mt-2 text-slate-400">
Allows wrapping in the reverse cross-axis direction.
</p>
</div>
</div>
<pre class="mt-6 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code>.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
What Is the flex-flow Property?
flex-flow is a shorthand property that combines
flex-direction and
flex-wrap.
</pre>
<p class="mt-5">
The example above is equivalent to:
</p>
<pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
How Does justify-content Work?
The justify-content property controls how flex items
are distributed along the main axis.
| Value | Typical Result |
|---|---|
| flex-start | Items are grouped toward the start. |
| flex-end | Items are grouped toward the end. |
| center | Items are grouped in the center. |
| space-between | Extra space is distributed between items. |
| space-around | Space is distributed around items. |
| space-evenly | Available space is distributed evenly. |
</pre>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Does align-items Work?
</h2>
<p class="mb-4">
<code class="text-blue-300">align-items</code> controls the alignment of flex items on
the cross axis.
</p>
<div class="grid md:grid-cols-2 gap-4">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">stretch</h3>
<p class="text-sm mt-2 text-slate-400">
Items can stretch across the cross axis when their cross size is not explicitly constrained.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">flex-start</h3>
<p class="text-sm mt-2 text-slate-400">
Aligns items toward the cross-axis start.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">flex-end</h3>
<p class="text-sm mt-2 text-slate-400">
Aligns items toward the cross-axis end.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
<h3 class="font-semibold text-white">center</h3>
<p class="text-sm mt-2 text-slate-400">
Centers items on the cross axis.
</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5 md:col-span-2">
<h3 class="font-semibold text-white">baseline</h3>
<p class="text-sm mt-2 text-slate-400">
Aligns items according to their text or inline content baselines.
</p>
</div>
</div>
<pre class="mt-6 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.header {
display: flex;
align-items: center;
}
How Does align-content Work?
align-content controls how multiple flex lines are
distributed along the cross axis when the container has extra cross-axis space.
This property matters primarily when wrapping creates multiple flex lines. It does not
perform the same job as align-items, which controls
the items within a flex line.
</pre>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-5 mt-6">
<div class="space-y-3 text-sm">
<div class="rounded-lg border border-slate-600 p-3">
<strong class="text-white">align-items:</strong>
aligns items within a flex line.
</div>
<div class="rounded-lg border border-slate-600 p-3">
<strong class="text-white">align-content:</strong>
distributes multiple flex lines within the container.
</div>
</div>
</div>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Does gap Work in Flexbox?
</h2>
<p class="mb-4">
The <code class="text-blue-300">gap</code> property adds spacing between flex items.
It is especially useful when you need consistent spacing without assigning individual
margins to every child.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.toolbar {
display: flex;
gap: 1rem;
}
You can also control the two dimensions separately:
</pre>
<p class="mt-5">
For most component layouts, <code class="text-blue-300">gap</code> makes the relationship
between sibling elements much easier to understand and maintain.
</p>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Does the order Property Do?
</h2>
<p class="mb-4">
The <code class="text-blue-300">order</code> property controls the visual order of flex items.
Items are laid out according to their order values, with lower values appearing first.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.first {
order: 2;
}
.second {
order: 1;
}
Be careful when reordering content. Visual order can differ from source order, so important content should still have a sensible semantic order in the HTML.
What Does flex-grow Do?
flex-grow controls how a flex item participates in
distributing positive free space along the main axis.
</pre>
<p class="mt-5">
Here the main region is allowed to consume available free space while the sidebar does not
grow.
</p>
<div class="mt-6 rounded-xl border border-slate-700 bg-slate-900 p-5">
<div class="flex gap-3">
<div class="w-24 rounded-lg border border-slate-600 p-3 text-center text-sm">
Fixed
</div>
<div class="flex-1 rounded-lg border border-slate-600 p-3 text-center text-sm">
flex-grow: 1
</div>
</div>
</div>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Does flex-shrink Do?
</h2>
<p class="mb-4">
<code class="text-blue-300">flex-shrink</code> controls how a flex item participates in
shrinking when the items would otherwise require more space than is available.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.logo {
flex-shrink: 0;
}
This can be useful for preventing a particular flex item from shrinking while other items handle the available-space pressure.
What Does flex-basis Do?
flex-basis defines the initial main-axis size used for
a flex item before the available space is distributed.
</pre>
<p class="mt-5">
It is particularly useful when you want each item to begin from a preferred size while
still allowing Flexbox to adjust the final size.
</p>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Is the flex Shorthand Property?
</h2>
<p class="mb-4">
The <code class="text-blue-300">flex</code> property is shorthand for the main flexible
sizing properties:
<code class="text-blue-300">flex-grow</code>,
<code class="text-blue-300">flex-shrink</code>, and
<code class="text-blue-300">flex-basis</code>.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.content {
flex: 1;
}
You can also explicitly provide the components:
</pre>
<p class="mt-5">
The last example is a common responsive pattern. The item has a preferred basis of
<code class="text-blue-300">240px</code>, can grow when more space is available, and can
shrink when the available space becomes smaller.
</p>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
How Does align-self Work?
</h2>
<p class="mb-4">
<code class="text-blue-300">align-self</code> lets an individual flex item override the
container's <code class="text-blue-300">align-items</code> setting.
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.container {
display: flex;
align-items: center;
}
.special-item {
align-self: flex-end;
}
This is useful when a component contains mostly consistent items but one particular element requires a different cross-axis alignment.
How Do Flexbox Properties Work Together?
Flexbox becomes much easier to use when you stop thinking about each property independently. Most real layouts combine several properties.
</pre>
<div class="grid md:grid-cols-4 gap-4 mt-6">
<div class="rounded-xl border border-slate-700 bg-slate-900 p-4">
<div class="text-blue-300 font-mono text-sm">display</div>
<p class="text-sm mt-2">Creates the flex container.</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-4">
<div class="text-blue-300 font-mono text-sm">flex-wrap</div>
<p class="text-sm mt-2">Allows multiple lines.</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-4">
<div class="text-blue-300 font-mono text-sm">gap</div>
<p class="text-sm mt-2">Adds space between items.</p>
</div>
<div class="rounded-xl border border-slate-700 bg-slate-900 p-4">
<div class="text-blue-300 font-mono text-sm">flex</div>
<p class="text-sm mt-2">Controls flexible item sizing.</p>
</div>
</div>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
Which Flexbox Properties Are Most Important?
</h2>
<div class="overflow-x-auto rounded-xl border border-slate-700">
<table class="w-full text-left text-sm">
<thead class="bg-slate-800 text-white">
<tr>
<th class="px-4 py-3">Property</th>
<th class="px-4 py-3">Applied To</th>
<th class="px-4 py-3">Main Purpose</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-700 bg-slate-900">
<tr>
<td class="px-4 py-3 font-mono text-blue-300">flex-direction</td>
<td class="px-4 py-3">Container</td>
<td class="px-4 py-3">Sets the main-axis direction.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">flex-wrap</td>
<td class="px-4 py-3">Container</td>
<td class="px-4 py-3">Controls wrapping onto multiple lines.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">justify-content</td>
<td class="px-4 py-3">Container</td>
<td class="px-4 py-3">Distributes space on the main axis.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">align-items</td>
<td class="px-4 py-3">Container</td>
<td class="px-4 py-3">Aligns items on the cross axis.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">align-content</td>
<td class="px-4 py-3">Container</td>
<td class="px-4 py-3">Distributes multiple flex lines.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">gap</td>
<td class="px-4 py-3">Container</td>
<td class="px-4 py-3">Creates space between items.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">flex</td>
<td class="px-4 py-3">Item</td>
<td class="px-4 py-3">Controls grow, shrink, and basis.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">align-self</td>
<td class="px-4 py-3">Item</td>
<td class="px-4 py-3">Overrides cross-axis alignment.</td>
</tr>
<tr>
<td class="px-4 py-3 font-mono text-blue-300">order</td>
<td class="px-4 py-3">Item</td>
<td class="px-4 py-3">Changes visual item order.</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Are Common Mistakes with Flexbox Properties?
</h2>
<div class="space-y-5">
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Confusing justify-content and align-items
</h3>
<p>
Developers often memorize that one property is horizontal and the other is vertical.
That breaks when <code class="text-blue-300">flex-direction</code> changes.
Always think in terms of main and cross axes.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Using align-content when there is only one flex line
</h3>
<p>
<code class="text-blue-300">align-content</code> is intended for distributing multiple
flex lines. For ordinary single-line alignment, <code class="text-blue-300">align-items</code>
is usually the property you need.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Forgetting flex-wrap
</h3>
<p>
Large groups of items can create unwanted overflow when everything is forced onto one
line. Wrapping can make the layout much more resilient.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Overusing fixed widths
</h3>
<p>
Flexbox is designed to distribute available space. Too many rigid dimensions can make
the layout harder to adapt to different screen sizes.
</p>
</div>
<div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
<h3 class="text-lg font-semibold text-white mb-2">
Reordering important content with order
</h3>
<p>
The visual order of flex items can differ from the source order. Keep the HTML structure
meaningful and use CSS ordering carefully.
</p>
</div>
</div>
</section>
<section>
<h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
What Is a Practical Flexbox Example?
</h2>
<p class="mb-5">
The following example combines several Flexbox properties to create a responsive layout:
</p>
<pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code><div class="dashboard">
<aside class="sidebar">
Sidebar
</aside>
<main class="content">
<header class="header">
<h1>Dashboard</h1>
<button>Profile</button>
</header>
<section class="cards">
<article class="card">Users</article>
<article class="card">Projects</article>
<article class="card">Revenue</article>
</section>
</main>
</div>
.dashboard {
display: flex;
min-height: 100vh;
}
.sidebar {
flex: 0 0 240px;
}
.content {
flex: 1;
min-width: 0;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 220px;
}
This example demonstrates an important Flexbox pattern: use the container properties to establish the layout and use flexible sizing on the children to let the interface respond to available space.
How Should You Learn Flexbox Properties?
A good way to learn Flexbox is to understand the layout model before memorizing individual property names.
1. Start with the container
Use display: flex and choose the appropriate
flex-direction.
2. Decide how items should align
Use justify-content for the main axis and
align-items for the cross axis.
3. Add spacing
Use gap when the spacing belongs between sibling items.
4. Make items flexible
Use flex,
flex-grow,
flex-shrink, and
flex-basis when sizing needs to respond to available space.
Key Takeaways
- display: flex creates a Flexbox container.
- flex-direction determines the main-axis direction.
- flex-wrap controls whether items can create additional lines.
- justify-content distributes items on the main axis.
- align-items aligns items on the cross axis.
- align-content distributes multiple flex lines.
- gap creates consistent spacing between flex items.
- flex-grow, flex-shrink, and flex-basis control flexible sizing.
- flex combines the main flexible sizing properties.
- align-self lets one item override cross-axis alignment.
- order changes visual ordering and should be used carefully.