CSS Flexbox (Flexible Box Layout) is a powerful layout module that makes it easier to design flexible, responsive layouts without using floats or complex positioning. Flexbox arranges items in a row or column and distributes space automatically based on screen size.
Flexbox is used to:
The parent element where Flexbox is applied.
.container {
display: flex;
}
The child elements inside the flex container.
| Property | Description |
|---|---|
display: flex |
Enables Flexbox |
flex-direction |
Sets direction of items (row/column) |
flex-wrap |
Allows items to wrap to the next line |
justify-content |
Aligns items horizontally |
align-items |
Aligns items vertically |
align-content |
Aligns multiple rows (when wrapped) |
gap |
Adds space between items |
| Property | Description |
|---|---|
| flex | Shorthand for grow, shrink, basis |
| flex-grow | How much item should grow |
| flex-shrink | How much item should shrink |
| flex-basis | Initial size of item |
| align-self | Overrides vertical alignment per item |
| order | Changes display order |
flex-direction value |
Behavior |
|---|---|
row (default) |
Items placed left → right |
row-reverse |
Items placed right → left |
column |
Items placed top → bottom |
column-reverse |
Items placed bottom → top |
.container {
display: flex;
justify-content: center;
align-items: center;
}
.item {
flex: 1;
}