CSS Flexbox

CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that makes it easy to align, distribute, and organize elements inside a container.

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;

}
Flexbox is the most commonly used CSS layout technique in modern web development.

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.
Without display:flex;, Flexbox properties will not work.

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.
The default value of flex-direction is row.

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.
justify-content:center; is commonly used to center items horizontally.

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;

}
Combining justify-content:center; and align-items:center; perfectly centers content both horizontally and vertically.

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.
flex-wrap:wrap; is useful for responsive layouts.

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;

}
The gap property is cleaner than using margins between flex items.

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.
These properties help create flexible and responsive layouts.

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;

}
Flexbox is widely used for menus, galleries, forms, cards, dashboards, and responsive layouts.

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;

}
Mastering these Flexbox properties enables you to build professional and responsive webpage layouts with ease.

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 gap instead of margins for spacing.
  • Use justify-content for horizontal alignment.
  • Use align-items for vertical alignment.
  • Use flex-wrap:wrap; for responsive designs.
  • Avoid unnecessary nesting of flex containers.
  • Test layouts on different screen sizes.
Flexbox makes responsive layouts easier with less CSS code.

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;

}
Most Flexbox problems occur because display:flex; is missing.

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
Flexbox works reliably across all modern web browsers.

14. CSS Flexbox Interview Questions

  1. What is CSS Flexbox?
  2. What is the purpose of display:flex;?
  3. What is the difference between justify-content and align-items?
  4. What does flex-direction do?
  5. What is the default value of flex-direction?
  6. Why is flex-wrap used?
  7. What is the purpose of the gap property?
  8. What is the difference between Flexbox and CSS Grid?
  9. What do flex-grow, flex-shrink, and flex-basis do?
  10. When should you use Flexbox?
These questions are frequently asked in Front-End Developer and Web Designer interviews.

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.
Congratulations! You have successfully completed the CSS Flexbox lesson. You can now build modern, responsive layouts using Flexbox.

Quick Quiz

Which CSS property creates a Flexbox container?