Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🎯 Introduction: Layout
Alignment Made Easy
Alignment used to be one of the most painful parts of CSS.
Centering a div vertically? Creating equal spacing between items? Those were
once complex tasks that required floats, margin hacks, or table
displays.
Enter Flexbox—a CSS3 layout model that brings powerful,
intuitive control over the alignment and distribution of elements along a
container's main and cross axes.
In this chapter, you'll learn:
Let’s get started by understanding the two main axes that
drive alignment in Flexbox.
🔄 Main Axis vs Cross Axis
(Recap)
Understanding these axes is key to knowing how each
alignment property behaves.
🔧 Property 1:
justify-content — Aligning Along the Main Axis
This property controls how space is distributed between
flex items along the main axis (horizontal by default).
🔹 Syntax:
css
.container
{
display: flex;
justify-content: [value];
}
🔹 Values & Behaviors:
Value |
Description |
Visual Behavior |
flex-start |
Items align to start |
← items |
flex-end |
Items align
to end |
items → |
center |
Items align center |
← items → |
space-between |
Items spread
with space between, no gaps at ends |
item — item —
item |
space-around |
Equal space around
items |
— item — item — |
space-evenly |
Equal space between
all items |
— item — item
— item — |
🔸 Example:
html
<div
class="container">
<div
class="box">A</div>
<div
class="box">B</div>
<div
class="box">C</div>
</div>
css
.container
{
display: flex;
justify-content: space-between;
}
Result: Three boxes are spaced out evenly, aligned
horizontally.
🔧 Property 2: align-items
— Aligning Along the Cross Axis
This property aligns flex items vertically when
flex-direction: row (or horizontally when flex-direction: column).
🔹 Syntax:
css
.container
{
display: flex;
align-items: [value];
}
🔹 Values:
Value |
Description |
stretch |
Default — stretches items
to fill container |
flex-start |
Aligns items
to the start of the cross axis |
flex-end |
Aligns items to the
end of the cross axis |
center |
Aligns items
to the center of the cross axis |
baseline |
Aligns items based on
their text baseline |
🔸 Example:
css
.container
{
display: flex;
align-items: center;
}
✅ Useful when you want to vertically
center elements inside a container.
🧠 Combining
justify-content + align-items
Want to center content horizontally and vertically?
css
.container
{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Result: The child element will be perfectly centered
both ways.
🔧 Live Example: Centered
Login Box
html
<div
class="center-box">
<form>Login Form</form>
</div>
css
.center-box
{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
🔧 Property 3:
align-content — Aligning Wrapped Rows
While align-items aligns individual items, align-content is
used only when items wrap onto multiple lines (with flex-wrap:
wrap).
🔹 Syntax:
css
.container
{
display: flex;
flex-wrap: wrap;
align-content: [value];
}
🔹 Values:
Value |
Description |
flex-start |
Lines packed at the
start |
flex-end |
Lines packed
at the end |
center |
Lines packed in the
center |
space-between |
Even spacing
between rows |
space-around |
Space around rows |
stretch |
Rows stretch
to fill space |
🔸 Example:
css
.container
{
display: flex;
flex-wrap: wrap;
align-content: center;
height: 400px;
}
🧠 Use this only when
wrapping is enabled and multiple rows exist.
🧪 Visual Comparison Table
Property |
Applies To |
Axis |
When to Use |
justify-content |
All flex containers |
Main Axis |
Horizontal alignment
by default |
align-items |
Single-line
containers |
Cross Axis |
Vertical
alignment by default |
align-content |
Multi-line (wrapped)
containers |
Cross Axis |
Vertical spacing
between rows |
🧠 Real-World Scenarios
🔹 1. Navigation Menu
Alignment
css
nav
{
display: flex;
justify-content: space-between;
align-items: center;
}
🔹 2. Card Layout with
Vertical Centering
css
.card
{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
🔹 3. Responsive Image +
Text Grid
css
.grid
{
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-content: flex-start;
}
✅ Best Practices
⚠️ Common Mistakes
Mistake |
Problem |
Fix |
Forgetting height |
Vertical alignment
doesn’t work |
Add height to
container |
Using align-content on single-line |
No effect |
Use align-items
instead |
Expecting
justify-content to affect vertical space |
Wrong axis |
Use align-items or
align-content |
🧾 Summary Table:
Alignment Properties Recap
Property |
Function |
Applies When |
justify-content |
Align items on main
axis |
Always |
align-items |
Align items
on cross axis |
Always |
align-content |
Align multiple rows |
Only when flex-wrap:
wrap is used |
📍 Chapter Recap: What
You’ve Learned
You're now ready to move on to flex-grow, shrink, and
basis to build fluid and responsive components.
A: Flexbox (Flexible Box Layout) is a CSS layout
model designed to distribute space and align items in a container—even when
their size is unknown or dynamic. It's perfect for creating responsive,
one-dimensional layouts (row or column).
A: Flexbox is ideal for one-dimensional layouts
(either horizontal OR vertical), while CSS Grid handles two-dimensional
layouts (both rows AND columns). Flexbox is great for components; Grid is
better for full-page layouts.
A: Not always! Flexbox naturally adapts content for
different screen sizes. But combining Flexbox with media queries gives you
finer control for breakpoints, especially in complex responsive designs.
A: It turns a container into a Flex Container,
activating Flexbox for all its direct children (Flex Items). Once
enabled, you can use properties like justify-content, align-items, and
flex-wrap.
A: Absolutely! One of Flexbox’s most popular features is its ability to center elements both vertically and horizontally using just two properties:
display: flex;
justify-content:
center;
align-items:
center;
A: Use the flex-wrap property:
flex-wrap: wrap;
This allows Flex Items to move onto multiple lines if they
don’t fit in one row.
A: flex is shorthand for:
flex:
[flex-grow] [flex-shrink] [flex-basis];
For example
flex: 1 1 200px;
Means: grow if needed, shrink if needed, and start at 200px
width.
A: Yes! The order property lets you rearrange
elements without changing your HTML structure. Lower numbers appear first.
A: Yes. Flexbox is widely supported in all modern
browsers, including Chrome, Firefox, Edge, Safari, and even IE11 (with minor
limitations). It’s safe to use in production.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)