Introduction
CSS Flexbox is a powerful layout system used to design flexible and responsive layouts. It makes it easy to align items, distribute space, and create modern UI designs without using complex positioning or floats.
Flexbox works on a one-dimensional layout (row or column) and is widely used in real-world web development.
What Youβll Learn
- What is Flexbox
- Flex container and flex items
- Main axis and cross axis
- Flexbox properties
- Real-world usage
What is Flexbox?
Flexbox (Flexible Box Layout) is a one-dimensional layout system used to:
- Align items
- Distribute space
- Control layout direction
- Make responsive designs easier
It works on:
- Row (horizontal)
- Column (vertical)
Basic Structure of Flexbox
Flexbox always has:
- Flex Container (parent)
- Flex Items (children)
Example:
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
.container {
display: flex;
}
π Once you add display: flex;, the container becomes a flex container and all children become flex items.
πΉ Important Concept: Main Axis vs Cross Axis
Understanding axes is very important in Flexbox.
If flex-direction: row
- Main axis β horizontal
- Cross axis β vertical
If flex-direction: column
- Main axis β vertical
- Cross axis β horizontal
π All alignment properties depend on these axes.
Important Flexbox Properties
πΉ flex-direction
Controls direction of items.
.container {
display: flex;
flex-direction: row; /* default */
}
Values:
- row
- row-reverse
- column
- column-reverse
πΉ justify-content
Aligns items along the main axis.
.container {
justify-content: center;
}
Values:
- flex-start
- flex-end
- center
- space-between
- space-around
- space-evenly
π Used for horizontal alignment (in row direction).
πΉ align-items
Aligns items along the main axis.
.container {
align-items: center;
}
Values:
- stretch
- flex-start
- flex-end
- center
- baseline
π Used for vertical alignment (in row direction).
πΉ flex-wrap
Controls whether items move to the next line.
.container {
flex-wrap: wrap;
}
π Prevents overflow when items donβt fit in one line.
πΉ gap
Adds space between items.
.container {
display: flex;
gap: 20px;
}
π Cleaner and better than using margins.
Practical Example (Very Important)
Centering Content Perfectly
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
π This centers content both horizontally and vertically.
Used in:
- Hero sections
- Login forms
- Cards
- Landing pages
πΉ Flex Item Properties (Advanced Basics)
πΈ flex-grow
Defines how much space an item should take.
.box {
flex-grow: 1;
}
π Items expand equally.
πΈ flex-shrink
.box {
flex-shrink: 0;
}
π Prevents shrinking.
πΈ flex-basis
.box {
flex-basis: 100px;
}
π Sets initial size before spacing is distributed.
πΈ flex (Shorthand)
.box {
flex: 1;
}
π Combines grow, shrink, and basis.
Real Use Case for Your Website
You can use Flexbox for:
- Navigation bar
- Cards layout
- Footer columns
- Blog post grids
- About page sections
Example for navbar:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
π This is used in almost every website.
πΉ Complete Example
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
}
.box {
width: 100px;
height: 100px;
background: blue;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
</body>
</html>
πΉ Common Mistakes
- Forgetting
display: flex - Confusing
justify-contentandalign-items - Not using
flex-wrap - Overusing fixed widths
πΉ Best Practices
- Use Flexbox for alignment and layout
- Keep layouts simple
- Use
gapinstead of margins - Combine with responsive design
πΉ Practice Tasks:
Task 1: Create a Flex Container
Create a container with 3 boxes and apply display: flex.
π Output should show items in a row.
Task 2: Change Direction
Use flex-direction: column.
π Items should appear vertically.
Task 3: Center Items
Center all items horizontally and vertically using:
justify-contentalign-items
Task 4: Add Spacing
Add space between items using gap.
π Try different values like 10px, 20px.
Task 5: Use justify-content
Apply different values:
centerspace-betweenspace-around
π Observe alignment changes.
Task 6: Use align-items
Try:
flex-startcenterflex-end
π See vertical alignment differences.
Task 7: Enable Wrapping
Use flex-wrap: wrap.
π Reduce screen width and observe behavior.
Task 8: Create a Navbar
Create a simple navbar using Flexbox.
π Use:
justify-content: space-betweenalign-items: center
Task 9: Equal Width Boxes
Use flex: 1 on all items.
π All boxes should have equal width.
Task 10: Center a Box in Page
Create a full-height container and center a box using Flexbox.
π Use:
height: 100vhjustify-contentalign-items
Conclusion
CSS Flexbox is an essential layout tool for modern web development. By understanding how flex containers and items work, along with alignment properties, you can build responsive and clean layouts easily.
π In the next tutorial, you will learn CSS Flexbox β Advanced, which is used for more complex designs.
