You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that makes it easy to align, distribute, and space elements within a container. Before Flexbox, developers relied on floats, tables, and positioning hacks to create layouts. Flexbox replaced all of that with a clean, intuitive system.
Flexbox works on two axes:
Main Axis (default: horizontal) →
┌──────────────────────────────────────────┐
│ ┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │ ↑
│ │ Item │ │ Item │ │ Item │ │ Item │ │ │ Cross Axis
│ │ 1 │ │ 2 │ │ 3 │ │ 4 │ │ │ (default: vertical)
│ └──────┘ └──────┘ └──────┘ └──────┘ │ ↓
└──────────────────────────────────────────┘
Apply display: flex to a container element. Its direct children become flex items:
.container {
display: flex;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
By default, flex items sit side by side on a single line.
These properties are set on the flex container (the parent):
Controls the main axis direction:
| Value | Direction |
|---|---|
row | Left to right (default) |
row-reverse | Right to left |
column | Top to bottom |
column-reverse | Bottom to top |
.container {
display: flex;
flex-direction: column;
}
Controls whether items wrap to new lines:
| Value | Behaviour |
|---|---|
nowrap | All items on one line (default) |
wrap | Items wrap to the next line |
wrap-reverse | Items wrap in reverse order |
.container {
display: flex;
flex-wrap: wrap;
}
Aligns items along the main axis:
| Value | Effect |
|---|---|
flex-start | Pack items at the start (default) |
flex-end | Pack items at the end |
center | Centre items |
space-between | Equal space between items, none at edges |
space-around | Equal space around each item |
space-evenly | Equal space between and at edges |
.container {
display: flex;
justify-content: space-between;
}
Aligns items along the cross axis:
| Value | Effect |
|---|---|
stretch | Items stretch to fill the container (default) |
flex-start | Align to the start of the cross axis |
flex-end | Align to the end of the cross axis |
center | Centre along the cross axis |
baseline | Align by text baseline |
.container {
display: flex;
align-items: center;
height: 300px;
}
Controls the spacing of wrapped lines (only works with flex-wrap: wrap):
.container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
Adds space between flex items (without margins):
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.