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: The Power
of Flexibility in Responsive Design
One of the greatest strengths of Flexbox is its ability to
control how flex items grow, shrink, and occupy space inside their
container. This is especially useful when building layouts that must adjust
seamlessly to different screen sizes and content variations.
In this chapter, you’ll learn:
By the end, you’ll be able to build fluid, adaptive
layouts with just a few lines of CSS.
🧩 Flex Item Core
Properties
Every direct child of a flex container becomes a flex
item and can be controlled using these properties:
Property |
Purpose |
flex-grow |
Controls how much an
item can grow to fill extra space |
flex-shrink |
Controls how
much an item can shrink when space is tight |
flex-basis |
Sets the initial size
of the item before space is distributed |
All three can be combined using the shorthand:
css
flex:
[grow] [shrink] [basis];
🔧 1. flex-grow: Expanding
Items
The flex-grow property defines how much a flex item should grow
relative to other items in the same container when there’s extra space
available.
🔹 Syntax:
css
.item
{
flex-grow: 1;
}
🔹 Example:
html
<div
class="container">
<div class="item
one">1</div>
<div class="item
two">2</div>
</div>
css
.container
{
display: flex;
}
.one
{
flex-grow: 1;
}
.two
{
flex-grow: 2;
}
📌 .two will take twice
as much space as .one when there's room to grow.
📊 Comparison Table –
flex-grow
Item |
flex-grow |
Relative Size |
A |
1 |
1 part |
B |
2 |
2 parts |
C |
3 |
3 parts |
Total: 6 parts → Each item gets a percentage of the
remaining space based on its share.
🔧 2. flex-shrink:
Compressing Items
The flex-shrink property controls how much a flex item will shrink
relative to its siblings when the container is too small.
🔹 Syntax:
css
.item
{
flex-shrink: 1;
}
🔹 Example:
css
.item
{
flex-basis: 200px;
}
.item:first-child
{
flex-shrink: 2;
}
.item:last-child
{
flex-shrink: 1;
}
🧠 The first item
will shrink twice as fast as the second when needed.
📊 Comparison Table –
flex-shrink
Item |
flex-shrink |
Shrink Priority |
A |
2 |
Shrinks more |
B |
1 |
Shrinks less |
🔧 3. flex-basis: Initial
Size
The flex-basis property sets the initial size of an
item before flex-grow or flex-shrink calculations begin.
🔹 Syntax:
css
.item
{
flex-basis: 150px;
}
🔹 Example:
css
.item
{
flex-basis: 30%;
}
🧠 Think of flex-basis as
a starting width (or height for column direction).
📊 flex-basis vs width
Property |
Priority |
Behavior |
width |
Low |
Used if flex-basis is
auto |
flex-basis |
High |
Overrides
width in a flex context |
✅ Use flex-basis for more
consistent sizing within flex containers.
⚙️ The flex Shorthand
Instead of writing all three properties separately, use the
flex shorthand:
css
flex:
[flex-grow] [flex-shrink] [flex-basis];
🔹 Example:
css
.item
{
flex: 1 1 200px;
}
This means:
💡 Common Shorthand
Values:
Shorthand |
Equivalent |
flex: 1 |
flex-grow: 1;
flex-shrink: 1; flex-basis: 0 |
flex: auto |
flex-grow: 1;
flex-shrink: 1; flex-basis: auto |
flex: none |
flex-grow: 0;
flex-shrink: 0; flex-basis: auto |
flex: 0 0 100px |
Fixed size of
100px, no grow/shrink |
🧠 Real-World Scenarios
🔸 1. Equal-Width Columns
css
.column
{
flex: 1;
}
🔸 2. Sidebar + Content
Layout
css
.sidebar
{
flex: 0 0 250px;
}
.content
{
flex: 1;
}
🔸 3. Gallery Layout with
Auto Wrapping
css
.item
{
flex: 1 1 250px;
}
🧱 Responsive Card Grid
Example
html
<div
class="grid">
<div class="card">Card
1</div>
<div class="card">Card
2</div>
<div class="card">Card
3</div>
</div>
css
.grid
{
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card
{
flex: 1 1 300px;
}
Result: Cards will wrap and resize fluidly based on screen
size.
✅ Best Practices
❌ Common Mistakes
Mistake |
Cause |
Solution |
Items overflowing
container |
No shrink behavior |
Add flex-shrink: 1 |
Items not growing |
Missing
flex-grow |
Add flex: 1
or flex-grow: 1 |
Width not being
respected |
Using width instead of
flex-basis |
Use flex-basis |
📋 Summary Table – Flexbox
Sizing
Property |
Purpose |
Default |
flex-grow |
How much item can grow |
0 |
flex-shrink |
How much item
can shrink |
1 |
flex-basis |
Initial size |
auto |
flex |
Shorthand for
all three |
0 1 auto |
📍 Recap: What You’ve
Learned
Next, we’ll dive into wrapping, ordering, and responsive
reordering of Flexbox items.
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)