CSS Flexbox
1. Introduction to CSS Flexbox
Flexbox is a CSS layout module designed to create flexible, responsive layouts without using floats or positioning.
Advantages of Flexbox
- Easy horizontal alignment.
- Easy vertical alignment.
- Responsive layouts.
- Equal spacing between items.
- Simple ordering of elements.
.container{
display:flex;
}
2. Flex Container (display: flex)
To create a Flexbox layout, apply display:flex; to the parent element. All child elements automatically become flex items.
.container{
display:flex;
}
| Property | Description |
|---|---|
| display:flex | Creates a flex container. |
| Flex Item | Every direct child becomes a flex item. |
3. Flex Direction (flex-direction)
The flex-direction property determines the direction in which flex items are displayed.
.container{
display:flex;
flex-direction:row;
}
| Value | Description |
|---|---|
| row | Items appear horizontally. |
| row-reverse | Horizontal reverse order. |
| column | Items appear vertically. |
| column-reverse | Vertical reverse order. |
4. Justify Content (justify-content)
The justify-content property aligns flex items along the main axis (usually horizontally).
.container{
display:flex;
justify-content:center;
}
| Value | Description |
|---|---|
| flex-start | Aligns items at the beginning. |
| center | Centers items. |
| flex-end | Aligns items at the end. |
| space-between | Equal space between items. |
| space-around | Equal space around items. |
5. Align Items (align-items)
The align-items property aligns flex items along the cross axis (usually vertically).
.container{
display:flex;
align-items:center;
height:300px;
}
| Value | Description |
|---|---|
| stretch | Default value. Items stretch to fill the container. |
| flex-start | Aligns items at the top. |
| center | Centers items vertically. |
| flex-end | Aligns items at the bottom. |
.container{
display:flex;
justify-content:center;
align-items:center;
}
6. Flex Wrap (flex-wrap)
By default, all flex items stay on a single line. The flex-wrap property allows items to move onto multiple lines when there isn't enough space.
.container{
display:flex;
flex-wrap:wrap;
}
| Value | Description |
|---|---|
| nowrap | Default. All items remain on one line. |
| wrap | Items move to the next line if needed. |
| wrap-reverse | Items wrap in reverse order. |
7. Gap Property (gap)
The gap property creates space between flex items without using margins.
.container{
display:flex;
gap:20px;
}
| Property | Purpose |
|---|---|
| gap | Adds space between rows and columns. |
| row-gap | Controls vertical spacing. |
| column-gap | Controls horizontal spacing. |
.gallery{
display:flex;
gap:15px;
}
8. Flex Grow, Shrink & Basis
Flex items can automatically grow or shrink according to the available space using flex-grow, flex-shrink, and flex-basis.
.item{
flex-grow:1;
flex-shrink:1;
flex-basis:200px;
}
| Property | Description |
|---|---|
| flex-grow | Allows an item to grow. |
| flex-shrink | Allows an item to shrink. |
| flex-basis | Defines the initial size. |
9. Practical Flexbox Examples
Example 1: Navigation Menu
nav{
display:flex;
justify-content:space-between;
}
Example 2: Center a Login Form
.container{
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
Example 3: Responsive Cards
.cards{
display:flex;
flex-wrap:wrap;
gap:20px;
}
10. Flexbox Properties Comparison Table
| Property | Purpose |
|---|---|
| display:flex | Creates a flex container. |
| flex-direction | Changes item direction. |
| justify-content | Horizontal alignment. |
| align-items | Vertical alignment. |
| flex-wrap | Allows items to wrap. |
| gap | Adds spacing between items. |
| flex-grow | Allows items to grow. |
| flex-shrink | Allows items to shrink. |
| flex-basis | Sets the initial size. |
.container{
display:flex;
justify-content:center;
align-items:center;
gap:20px;
}
11. CSS Flexbox Best Practices
Flexbox is one of the most powerful CSS layout techniques. Following best practices helps create clean, responsive, and maintainable layouts.
- Use Flexbox for one-dimensional layouts.
- Apply
display:flex;only to the parent container. - Use
gapinstead of margins for spacing. - Use
justify-contentfor horizontal alignment. - Use
align-itemsfor vertical alignment. - Use
flex-wrap:wrap;for responsive designs. - Avoid unnecessary nesting of flex containers.
- Test layouts on different screen sizes.
12. Common Flexbox Mistakes
| Mistake | Correct Practice |
|---|---|
| Using justify-content without display:flex. | Apply display:flex; first. |
| Forgetting flex-wrap. | Use flex-wrap:wrap; for responsive layouts. |
| Using margins instead of gap. | Prefer the gap property. |
| Confusing justify-content and align-items. | Remember their different alignment directions. |
| Using Flexbox for complex two-dimensional layouts. | Use CSS Grid when appropriate. |
❌ Incorrect
.container{
justify-content:center;
}
✔ Correct
.container{
display:flex;
justify-content:center;
}
13. Browser Support
CSS Flexbox is fully supported by all modern browsers, making it the preferred layout system for responsive websites.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Flexbox Interview Questions
- What is CSS Flexbox?
- What is the purpose of
display:flex;? - What is the difference between
justify-contentandalign-items? - What does
flex-directiondo? - What is the default value of
flex-direction? - Why is
flex-wrapused? - What is the purpose of the
gapproperty? - What is the difference between Flexbox and CSS Grid?
- What do
flex-grow,flex-shrink, andflex-basisdo? - When should you use Flexbox?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Flexbox.
- ✔ display:flex.
- ✔ flex-direction.
- ✔ justify-content.
- ✔ align-items.
- ✔ flex-wrap.
- ✔ gap.
- ✔ flex-grow, flex-shrink, and flex-basis.
- ✔ Practical Flexbox examples.
- ✔ Best practices and common mistakes.
Quick Quiz
Which CSS property creates a Flexbox container?