Flexbox and Grid
- 6 min reading
- 6 min activity
0 of 6 tasks doneup to 120 XP
Flexbox and grid put boxes side by side. In the next lesson you use them for your own website.
- Place items in a row or a column with flexbox and control alignment, wrapping and space.
- Size items with
flex-grow,flex-shrink,flex-basisandflex. - Change the order with care and align one item differently.
- Build a grid with columns,
fr,repeat()andgap.
In lesson 3.6 you met block and inline elements.
Remember: block elements
A quick question from lesson 3.6. It is optional.
Flexbox: a container and its items
Flexbox places items along one line: a row or a column. The flex container is the parent box with display: flex. Its direct children are the flex items, and they now sit side by side.
justify-content places the items along the row; align-items places them across it.| Property | Values | What it does |
|---|---|---|
flex-direction | row (default), column | Items side by side, or one below the other. |
justify-content | flex-start, flex-end, center, space-between, space-around | Places the items along the direction. In a row: from left to right. |
align-items | stretch (default), flex-start, flex-end, center | Places the items across the direction. In a row: from top to bottom. |
flex-wrap | nowrap (default), wrap | With wrap, items that do not fit move to a new line. |
gap | 12px | Space between the items. |
- Centre anything
With flexbox, centring in both directions takes three lines on the container (the height only makes the centring visible):
.main-box { display: flex; justify-content: center; /* along the row */ align-items: center; /* across the row */ height: 300px; }Grid can do it too:
display: grid; place-items: center;
Flexbox properties
Tap a declaration, then tap what it does.
Sharing the space
flex-grow shares the free space. The default is 0: the item does not grow. With 1 and 2, one item gets one share, the other two. In the slide's example, a search field takes all the free space between a logo and a profile box:
.container {
display: flex; /* needed: the slide forgot this line */
}
.small-box {
flex-grow: 0; /* stays as it is */
width: 80px;
}
.big-box {
flex-grow: 1; /* takes all the free space */
}
flex-basisis the start size, before the free space is shared:flex-basis: 200px;flex-shrinkdecides how much an item gets smaller when space is short (default 1).flex-shrink: 0;protects a photo, as below.flexis the short form: grow, shrink, start size.flex: 1 1 200px;starts at 200px, grows and shrinks.flex: 0 0 250px;always stays 250px.flex: 1;gives all items equal width.
<div class="container">
<img src="profile.jpg" alt="Profile photo" class="profile-photo">
<p class="text">A long description next to the photo...</p>
</div>
.profile-photo {
width: 80px;
height: 80px;
flex-shrink: 0; /* the photo always stays 80px */
}
.text {
flex-grow: 1; /* the text takes the rest */
}
Who grows more?
Order and one item on its own
order changes where an item appears: smaller numbers come first (default 0). It changes only what you see: screen readers and the Tab key still follow the HTML order. So use it rarely.
align-self aligns one item differently, for example one button at the bottom while the others stay at the top: align-self: flex-end; (bonus exercise below).
CSS Grid: rows and columns
Flexbox works in one direction. CSS Grid works with rows and columns at the same time. The grid container has display: grid, and its children are the grid items.
grid-template-columns: 200px 200px 200px; /* three fixed columns */
grid-template-columns: 1fr 2fr 1fr; /* 4 shares: the middle column is twice as wide */
grid-template-columns: repeat(3, 1fr); /* short form: three equal columns */
grid-template-rows: 100px auto; /* first row 100px, second as high as its content */
gap: 20px; /* space between rows and columns */
fr means a fraction, a share of the free space. One slide calls this layout "Gridbox"; its name is CSS Grid.
Your turn: a header with flexbox
Guess first, then choose the four values.
Header with flexbox
This is the header of a website: the logo AK and a menu. Put them side by side with flexbox: the logo at the start, the menu at the end, both centred across the row, and allowed to wrap on a small phone.
Your turn: a skills gallery with grid
The slide's product gallery, with skills instead of products.
Skills gallery with grid
This gallery comes from the slides, with skills instead of products. Make three equal columns with repeat(), put 15px between the cards, and choose a card colour that is readable with white text.
Bonus: fix the profile buttons
An optional extra for Mission 3. It gives you a mission star.
Fix the profile buttons
Optional extra. This menu comes from the slides about align-self. Make three fixes: the selector .btn profile matches nothing, the menu buttons are div elements instead of links, and white text on #007bff and #28a745 is too light. "My profile" should sit at the bottom.
- Flex container
- The parent box with
display: flex. - Flex item
- A direct child of a flex container.
- justify-content
- Places the items along the row (or column).
- align-items
- Places the items across the row (or column).
- CSS Grid
- A layout with rows and columns:
display: grid. - fr
- A share of the free space in a grid.
In short
display: flexon the parent puts the children in a row;flex-wrap: wrapandgapmake it work on phones.justify-contentworks along the row,align-itemsacross it.flex: 1 1 200px= grow, shrink, start size. Useorderrarely.display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px;makes three equal columns.
Project Step 3: you give your own website the layout of the course sketch.
Sources
- MDN Web Docs (2026). Basic concepts of flexbox (container, items, main axis and cross axis).
- MDN Web Docs (2026). justify-content and align-items.
- MDN Web Docs (2026). flex-wrap.
- MDN Web Docs (2026). flex (flex-grow, flex-shrink, flex-basis).
- MDN Web Docs (2026). order (visual order only; accessibility).
- MDN Web Docs (2026). align-self.
- MDN Web Docs (2026). Basic concepts of grid layout (fr, gap).
- MDN Web Docs (2026). grid-template-columns and repeat().
- W3C WAI (2026). Understanding Success Criterion 1.3.2: Meaningful Sequence (the reading order).
End of the lesson
This lesson: 0 of 130 XP
Code Cards in this lesson: 9
display: flexflex-directionjustify-contentalign-itemsflex-wrapflexorderdisplay: gridgrid-template-columns