Lesson 10 / Layout
Flexbox
Use CSS Flexbox to arrange items flexibly within a container for responsive, one-dimensional layouts.
Course Role
Core
Part of the main course path. Prioritize this before moving to optional polish or deeper references.
Teacher Notes / In-Class Use
Demo Live
- Use the playground to contrast justify-content on the main axis with align-items on the cross axis.
- Switch flex-direction from row to column so students see the axes change instead of memorizing property names.
Try In Class
- Have students build a horizontal navigation row with flexbox.
- Have students create a card row where items wrap and spacing remains consistent.
Submit Or Check
- Students should be able to identify the flex container and the flex items.
- Ask students whether the layout problem is one-dimensional before reaching for flexbox.
Watch For
- Applying flex properties to children when the parent is missing display: flex.
- Using margins to fake alignment that flexbox can handle more cleanly.
Learning Goals
- Understand flex containers, flex items, axes, wrapping, alignment, and ordering
- Create a basic flex container and control item direction, wrapping, and spacing
- Use advanced Flexbox properties like grow, shrink, basis, align-content, and order
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Change flex-direction first, then test justify-content and align-items.
- Turn wrapping on and off to see how items respond when space gets tight.
What Changes
- The axis labels and item positions update as the flex settings change.
- The code sample mirrors the layout choices in the playground.
What To Notice
- The main axis changes when flex-direction changes.
- gap handles spacing between items without adding margins to each child.
Apply It
- Use flexbox on one navigation row or card row in your current project.
Interactive Demo
Flexbox Playground
Change the controls and watch how the flex items respond. Pay close attention to the main axis and cross axis when you switch direction.
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
gap: 16px;
} What to notice
- justify-content follows the main axis.
- align-items follows the cross axis.
- flex-direction changes which axis is which.
Try this
- Switch direction from row to column.
- Predict where center alignment will move the items.
- Copy the generated CSS into a practice file.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source.
What This Teaches
This lesson teaches how to use CSS Flexbox to arrange items inside a container.
You will learn how display: flex, flex containers, flex items, axes, wrapping, spacing, alignment, and ordering work together.
Why It Matters
Flexbox is one of the most useful CSS layout tools for everyday interface work.
It is especially good for navigation bars, button rows, card layouts, media objects, and centering content.
Unlike CSS Grid, Flexbox is primarily used for one-dimensional layouts, meaning one row or one column at a time.
Core Concept: Flexbox Vocabulary
Flex container: The parent element withdisplay: flex.Flex item: A direct child of the flex container.flex-direction: Sets the main axis, such asroworcolumn.flex-wrap: Controls whether items stay on one line or wrap onto multiple lines.justify-content: Aligns or distributes items along the main axis.align-items: Aligns items along the cross axis.align-content: Distributes multiple wrapped lines within the container.flex-grow: Defines how much a flex item can grow relative to other items.flex-shrink: Defines how much a flex item can shrink relative to other items.flex-basis: Sets the starting size of a flex item.order: Changes the visual order of flex items.
Core Concept: Container and Items
To begin using Flexbox, set the parent element to display: flex. All direct child elements become flex items.
Flex properties like justify-content, align-items, flex-wrap, and gap usually belong on the container. Properties like flex, flex-grow, and order belong on individual items.
.flex-container {
display: flex;
} Main Axis and Cross Axis
Flexbox is controlled by two axes. The main axis is the direction items flow. The cross axis runs perpendicular to it.
When flex-direction changes, the meaning of justify-content and align-items changes too.
| Direction | Main Axis | Cross Axis |
|---|---|---|
row | Left to right in normal writing modes | Top to bottom |
row-reverse | Right to left in normal writing modes | Top to bottom |
column | Top to bottom | Left to right |
column-reverse | Bottom to top | Left to right |
Code Example: Flex Direction
flex-direction sets the direction of the main axis, defining how flex items are placed in the flex container.
.flex-container {
display: flex;
flex-direction: row; /* Default value */
/* or flex-direction: column; */
} Code Example: Flex Wrap
Use flex-wrap to decide whether flex items should stay on one line or wrap onto multiple lines.
flex-wrap: wrap is useful for responsive card rows, tag lists, galleries, and navigation that needs to survive smaller screens.
.flex-container {
display: flex;
flex-wrap: wrap;
} Code Example: Gap
gap creates consistent spacing between flex items without adding margins to each child.
Use gap for space inside a flex row or column. Use margin when an element needs space outside the whole group.
.flex-container {
display: flex;
gap: 1rem;
} Alignment Properties
The alignment properties are easier to remember when you connect each one to an axis.
| Property | Axis | Use For |
|---|---|---|
justify-content | Main axis | Moving or distributing items across the direction they flow. |
align-items | Cross axis | Aligning items perpendicular to the direction they flow. |
align-content | Cross axis, multiple lines only | Spacing wrapped rows or columns inside the container. |
Code Example: Justify Content
justify-content aligns flex items along the main axis. It controls how free space is distributed in the direction items flow.
.flex-container {
display: flex;
justify-content: space-between;
} Code Example: Align Items
align-items aligns items along the cross axis, perpendicular to the main axis.
.flex-container {
display: flex;
align-items: center;
} Interactive Practice: Flexbox Playground
Use the Flexbox Playground above to test flex-direction, flex-wrap, justify-content, align-items, and gap.
Before changing a control, predict whether the items will move on the main axis or the cross axis. Then compare your prediction to the result.
Common Flex Patterns
| Pattern | Useful CSS |
|---|---|
| Center one item both ways | display: flex; justify-content: center; align-items: center; |
| Horizontal button row | display: flex; gap: 0.75rem; align-items: center; |
| Responsive navigation | display: flex; flex-wrap: wrap; gap: 1rem; justify-content: flex-end; |
| Wrapping card row | display: flex; flex-wrap: wrap; gap: 1.5rem; |
| Equal-width flexible cards | flex: 1 1 16rem; on each card |
| Image and text media object | display: flex; gap: 1rem; align-items: flex-start; |
Practical Example: Responsive Navigation Bar
This pattern keeps navigation items in a row when space allows, then wraps them instead of forcing overflow.
.site-nav ul {
display: flex;
flex-wrap: wrap;
gap: 1rem;
justify-content: flex-end;
list-style: none;
margin: 0;
padding: 0;
} Practical Example: Flexible Card Layout
This pattern lets cards share a row, wrap when they run out of space, and start from a readable minimum size.
.card-list {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card {
flex: 1 1 16rem;
} Core Concept: Advanced Item Control
Flexbox also includes properties for controlling how individual flex items grow, shrink, and appear within a container.
Flex Grow, Shrink, and Basis
The flex shorthand combines flex-grow, flex-shrink, and flex-basis in that order.
- First value:
flex-grow.1means the item is allowed to grow. - Second value:
flex-shrink.1means the item is allowed to shrink. - Third value:
flex-basis.12remmeans the item starts at about12rembefore space is distributed.
.item {
flex: 1 1 12rem;
} Flex Shorthand Examples
| Value | Meaning |
|---|---|
flex: 0 0 auto; | Do not grow, do not shrink, use natural size. |
flex: 1 1 auto; | Grow and shrink from the natural size. |
flex: 1 1 16rem; | Grow and shrink from a starting size of 16rem. Useful for cards. |
flex: 1; | A common shortcut for flexible equal-width items, but less explicit for beginners. |
Core Concept: Align Content
align-content controls the spacing between multiple flex lines.
It only has a visible effect when flex items wrap onto more than one line.
flex-startflex-endcenterspace-betweenspace-aroundstretch
Core Concept: Order Property
The order property controls the visual order in which flex items appear within the container.
Use order carefully. It changes visual order, but it does not change the HTML source order, keyboard order, or screen reader reading order.
Use order for small visual adjustments, not for rewriting the actual reading order of important page content.
.featured-item {
order: -1;
} When Not to Use Flexbox
| Layout Need | Better Choice |
|---|---|
| Simple stacked content | Normal block flow is often enough. |
| Rows and columns controlled together | Use CSS Grid. |
| Page-level two-dimensional layout | Use CSS Grid, then Flexbox inside smaller pieces. |
| Changing source order for meaning | Change the HTML structure instead of relying on order. |
Common Mistakes
- Putting
display: flexon the wrong element. - Forgetting that only direct children become flex items.
- Using
justify-contentwhen the issue is actually cross-axis alignment. - Using
align-itemswhen there are multiple wrapped lines andalign-contentis needed. - Using margin hacks when
gapwould be cleaner. - Using
orderto fix visual layout while creating confusing keyboard or reading order. - Reaching for Flexbox when CSS Grid would handle rows and columns more clearly.
Debugging Checklist
- Inspect the parent and confirm it has
display: flex. - Check which elements are direct children of the flex container.
- Identify the main axis from
flex-direction. - Use
justify-contentfor the main axis andalign-itemsfor the cross axis. - Check whether items are wrapping before using
align-content. - Use
gapbefore adding margins to every child. - Check whether an item has a
flexshorthand,flex-grow,flex-shrink, orflex-basisrule. - Temporarily add outlines or background colors to see each flex item.
Resources
Flexbox Froggy | MDN Web Docs: CSS Flexbox | CSS-Tricks Flexbox Guide
Checkpoint
Before moving on, make sure these feel true.
- I can identify the flex container and flex items.
- I can explain main axis vs cross axis.
- I can use justify-content, align-items, and gap without guessing.
Project Connection
This lesson supports current class projects.
Practice
- Use the Flexbox Playground to test every
flex-directionvalue. - Center one item horizontally and vertically with
justify-contentandalign-items. - Build a responsive navigation bar using
display: flex,flex-wrap, andgap. - Create a row of cards with
flex-wrap: wrapandflex: 1 1 16rem. - Create a row of cards with aligned buttons.
- Build a media object with an image on one side and text on the other.
- Change a layout from
rowtocolumnand explain which axis changed. - Use DevTools to confirm which element is the flex container and which elements are flex items.
- Avoid
orderfor meaningful content. Reorder the HTML instead when reading order matters.