CHAPTER 07
Beginner
CSS Flexbox Complete Guide
Updated: May 10, 2026
5 min read
# CSS Flexbox Complete Guide
1. Introduction
Flexbox (Flexible Box Layout) was a revolution in web design. It is a one-dimensional layout model designed to lay out items in rows or columns, and distribute space dynamically. If you want to align things perfectly, Flexbox is the answer.2. Learning Objectives
-
Create a flex container with
display: flex.
-
Align items horizontally with
justify-content.
-
Align items vertically with
align-items.
-
Handle wrapping and spacing with
gap.
3. Detailed Explanations
Activating Flexbox
You applydisplay: flex to the parent container. All direct children instantly become "flex items" and will sit side-by-side in a row by default.
css
Flex Direction
You can change whether items flow in a row or a column.
css
Justify Content (Main Axis Alignment)
Ifflex-direction is row, justify-content aligns items horizontally.
Values: flex-start, center, flex-end, space-between, space-around.
css
Align Items (Cross Axis Alignment)
Ifflex-direction is row, align-items aligns items vertically.
Values: flex-start, center, flex-end, stretch.
css
The Ultimate Centering Trick
Centering a div used to be the hardest thing in CSS. With Flexbox, it's 3 lines:
css
Gap and Wrapping
Usegap to put space between items without dealing with margins. Use flex-wrap to allow items to flow to the next line if they run out of space.
css
4. Mini Project: Perfect Card Layout
html
css