On this page12 sections
Lecture 049: Flexbox: The Container
| Category | 8: Flexbox & Grid |
| Lecture | 049 of 057 |
| Estimated video length | 16 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Flexbox lines things up in a row or a column, spaces them out, and centres them. A navigation bar, a row of cards, a perfectly centred box: all of it takes a few lines. If you know Flutter, it's
RowandColumnfor the web.
Learning Objectives
- Turn on Flexbox with
display: flex - Set the direction with
flex-direction - Align items with
justify-contentandalign-items - Space and wrap items with
gapandflex-wrap - Size items with
flex: 1
1. Turn It On
Set display: flex on the parent (the flex container). Its direct children become flex
items and line up in a row.
Example
<style>
.row { display: flex; background: #f1f3f5; padding: 8px; }
.row div { background: #4c6ef5; color: white; padding: 12px 18px; margin: 4px; }
</style>
<div class="row">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>2. Direction
| Value | Items go… | Flutter equivalent |
|---|---|---|
row (default) | left to right | Row |
column | top to bottom | Column |
row-reverse / column-reverse | backwards | - |
.stack { display: flex; flex-direction: column; }3. justify-content: Along the Main Direction
Example: each row uses a different value:
<style>
.row { display: flex; background: #f1f3f5; margin-bottom: 8px; padding: 4px; }
.row span { background: #12b886; color: white; padding: 8px 14px; margin: 2px; }
</style>
<div class="row" style="justify-content: flex-start;"><span>A</span><span>B</span><span>flex-start</span></div>
<div class="row" style="justify-content: center;"><span>A</span><span>B</span><span>center</span></div>
<div class="row" style="justify-content: flex-end;"><span>A</span><span>B</span><span>flex-end</span></div>
<div class="row" style="justify-content: space-between;"><span>A</span><span>B</span><span>space-between</span></div>
<div class="row" style="justify-content: space-around;"><span>A</span><span>B</span><span>space-around</span></div>4. align-items: Across the Main Direction
In a row, this is the vertical alignment.
Example
<style>
.row { display: flex; height: 90px; background: #f1f3f5; margin-bottom: 8px; gap: 6px; }
.row span { background: #f76707; color: white; padding: 8px 14px; }
</style>
<div class="row" style="align-items: flex-start;"><span>flex-start</span><span>top</span></div>
<div class="row" style="align-items: center;"><span>center</span><span>middle</span></div>
<div class="row" style="align-items: stretch;"><span>stretch</span><span>(default) full height</span></div>Perfect centring
<style>
.center { display: flex; justify-content: center; align-items: center; height: 160px; background: #e7f5ff; }
</style>
<div class="center"><strong>I am exactly in the middle</strong></div>5. gap and flex-wrap
gap adds space between items (no margin tricks). flex-wrap: wrap lets items move to a
new line when they run out of room.
Example
<style>
.tags { display: flex; flex-wrap: wrap; gap: 8px; max-width: 320px; }
.tags span { background: #e5dbff; padding: 6px 12px; border-radius: 999px; }
</style>
<div class="tags">
<span>HTML</span><span>CSS</span><span>JavaScript</span><span>Flutter</span>
<span>Dart</span><span>Python</span><span>Firebase</span>
</div>6. flex: 1: Share the Space
On a child, flex: 1 means "take a share of the leftover space". It's like Expanded in Flutter.
Example
<style>
.bar { display: flex; gap: 8px; }
.bar input { flex: 1; padding: 8px; }
.bar button { padding: 8px 16px; }
</style>
<div class="bar">
<input placeholder="Search courses…">
<button>Search</button>
</div>The input grows to fill the row; the button keeps its natural size.
Code Examples
Example 1: A Navigation Bar
<style>
nav { display: flex; align-items: center; gap: 16px; padding: 12px 16px; background: #1c1f26; }
nav a { color: #dee2e6; text-decoration: none; }
nav .logo { font-weight: 800; color: white; margin-right: auto; } /* pushes the rest right */
</style>
<nav>
<a class="logo" href="#">CodeShop</a>
<a href="#">Courses</a>
<a href="#">Pricing</a>
<a href="#">Log in</a>
</nav>margin-right: auto on the logo eats all the spare space, pushing the other links to the right.
Common Beginner Mistakes
1. Putting display: flex on the children. It goes on the parent.
2. Mixing up the two alignments. justify-content = along the direction; align-items =
across it. In a column they swap roles visually.
3. Vertical centring with no height. align-items: center needs the container to be taller
than its items.
4. Margins for spacing. gap is simpler and doesn't add space at the edges.
Practice Questions
- Which element gets
display: flex: the parent or the children? - How do you stack flex items vertically?
- Which property spreads items so the first is at the start and the last at the end?
- Write the CSS to centre one item both horizontally and vertically in a 200px-tall box.
- What does
gap: 12pxdo? - In a search bar with an input and a button, how do you make the input fill the remaining space?
Practice Answers
- The parent (the container).
flex-direction: column;justify-content: space-between;.box { display: flex; justify-content: center; align-items: center; height: 200px; }- It adds 12px of space between the flex items.
- Give the input
flex: 1;
Key Takeaways
display: flexon the parent lines its children up in a row (or a column).justify-contentaligns along the direction;align-itemsacross it.gapspaces items;flex-wrap: wraplets them flow onto new lines.flex: 1makes an item fill the leftover space.