On this page12 sections
  1. Learning Objectives
  2. 1. Turn It On
  3. 2. Direction
  4. 3. justify-content: Along the Main Direction
  5. 4. align-items: Across the Main Direction
  6. 5. gap and flex-wrap
  7. 6. flex: 1: Share the Space
  8. Code Examples
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 049: Flexbox: The Container

Category8: Flexbox & Grid
Lecture049 of 057
Estimated video length16 min
TargetCSS3 · any modern browser
LevelIntermediate

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 Row and Column for the web.


Learning Objectives

  • Turn on Flexbox with display: flex
  • Set the direction with flex-direction
  • Align items with justify-content and align-items
  • Space and wrap items with gap and flex-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

html
<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

ValueItems go…Flutter equivalent
row (default)left to rightRow
columntop to bottomColumn
row-reverse / column-reversebackwards-
css
.stack { display: flex; flex-direction: column; }

3. justify-content: Along the Main Direction

Example: each row uses a different value:

html
<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

html
<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

html
<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

html
<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

html
<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

html
<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

  1. Which element gets display: flex: the parent or the children?
  2. How do you stack flex items vertically?
  3. Which property spreads items so the first is at the start and the last at the end?
  4. Write the CSS to centre one item both horizontally and vertically in a 200px-tall box.
  5. What does gap: 12px do?
  6. In a search bar with an input and a button, how do you make the input fill the remaining space?

Practice Answers

  1. The parent (the container).
  2. flex-direction: column;
  3. justify-content: space-between;
  4. .box { display: flex; justify-content: center; align-items: center; height: 200px; }
  5. It adds 12px of space between the flex items.
  6. Give the input flex: 1;

Key Takeaways

  • display: flex on the parent lines its children up in a row (or a column).
  • justify-content aligns along the direction; align-items across it.
  • gap spaces items; flex-wrap: wrap lets them flow onto new lines.
  • flex: 1 makes an item fill the leftover space.