MODULE 04 · LAYOUT & RESPONSIVENESS

Flexbox Layout

Everything before today has been about styling individual elements. Flexbox is the first tool that arranges several elements as a group — rows and columns that align, space out, and reorder without a single float or magic number.

Day 24 of 30 Intermediate ~22 min

A New display Value, A New Way to Think

display: flex turns an element into a flex container, and every direct child inside it becomes a flex item. From that moment, a whole new set of properties becomes available — properties that describe how those items should be arranged as a group, not just how each one looks on its own.

display: flex & flex-direction

Setting display: flex on a parent is step one. flex-direction then decides whether items line up left-to-right (row, the default) or top-to-bottom (column).

style.css
.row {
  display: flex;
  flex-direction: row;   /* default — left to right */
}

.column {
  display: flex;
  flex-direction: column;   /* top to bottom */
}
ROW VS COLUMN
1
2
3
flex-direction: row
1
2
3
flex-direction: column

Spacing Items Along the Main Axis

justify-content controls how items are spaced along the main axis — the direction set by flex-direction.

VALUE EFFECT
flex-start

Items bunch at the start of the container (the default).

center

Items bunch together in the center of the container.

space-between

Even space between items; no space at the outer edges.

space-around

Even space around every item, including the outer edges.

justify-content IN ACTION
1
2
3
flex-start
1
2
3
center
1
2
3
space-between
1
2
3
space-around

Aligning Items on the Cross Axis

While justify-content works along the main axis, align-items works along the perpendicular cross axis — for a row, that means vertical alignment.

style.css
.row {
  display: flex;
  align-items: center;   /* vertically centers items */
  height: 90px;
}
align-items IN A TALLER CONTAINER
1
2
3
flex-start
1
2
3
center
1
2
3
flex-end
1
2
3
stretch (default)
MEMORY TRICK

justify-content works with the flow of items (the main axis); align-items works across it (the cross axis). Flip flex-direction to column and the two swap which direction they control.

flex-grow, flex-shrink & flex-basis

Flex items can grow to fill extra space or shrink to fit a tight container. The shorthand flex combines all three: flex-grow, flex-shrink, and flex-basis in one declaration.

style.css
.sidebar {
  flex: 0 0 220px;   /* fixed width, doesn't grow or shrink */
}

.content {
  flex: 1;   /* grows to fill remaining space */
}
ONE FIXED ITEM, ONE THAT GROWS TO FILL THE REST
sidebar
content — flex: 1

Spacing Between Items, No Margins Needed

The gap property adds space between flex items without the extra-margin-on-every-item-except-the-last trick developers used to rely on.

style.css
.card-row {
  display: flex;
  gap: 16px;
}

Flexbox Is One-Dimensional

FLEXBOX VS GRID
FLEXBOX STRUGGLES WITH Aligning items across both rows and columns at once — like a photo gallery grid where every cell needs to line up in two directions.
FLEXBOX IS GREAT FOR Navigation bars, button groups, card rows, and any layout that flows in a single row or column.

Tomorrow's lesson covers CSS Grid, which handles true two-dimensional layouts — the two work well together rather than competing.

Breaking Down What You Just Learned

1

Flex container & itemsdisplay: flex turns a parent into a flex container; its direct children become flex items.

2

Directionflex-direction sets the main axis — row (default) or column.

3

Main-axis spacingjustify-content controls spacing along the main axis: start, center, space-between, space-around.

4

Cross-axis alignmentalign-items controls alignment perpendicular to the main axis.

5

Item sizingflex-grow, flex-shrink, and flex-basis (via the flex shorthand) control how items expand or contract.

Try It Yourself

EXERCISE

Take the header/nav from your Day 15 project and rebuild it with display: flex, justify-content: space-between, and align-items: center so the logo and links sit perfectly in one row.

Build a Flexbox Card Row

HANDS-ON EXERCISE

Practicing flex container and item properties together

Create day24.html linked to a stylesheet and build the following on the page.

  1. Build a nav bar with display: flex and justify-content: space-between for the logo and links.
  2. Build a row of three cards using display: flex and gap: 16px between them.
  3. Make the middle card grow to fill extra space with flex: 1, keeping the other two fixed-width.
  4. Center all three cards vertically inside a taller container using align-items: center.
  5. Switch one flex container to flex-direction: column and confirm the items stack.

Recap

FLEX BASICS

display: flex on a parent turns its children into flex items along a main axis.

MAIN AXIS

justify-content spaces items along the direction set by flex-direction.

CROSS AXIS

align-items aligns items perpendicular to the main axis.

ITEM SIZING

flex-grow, flex-shrink, and flex-basis control how items expand or shrink to fill space.

Key Takeaways

  • display: flex turns a parent into a flex container and its direct children into flex items.
  • flex-direction sets the main axis — row for horizontal, column for vertical.
  • justify-content spaces items along the main axis; align-items aligns them along the cross axis.
  • The flex shorthand (flex-grow, flex-shrink, flex-basis) controls how an item expands or shrinks relative to its siblings.
  • gap adds space between flex items without extra margin rules on individual children.
Course Overview