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.
- ✓Flexbox works along one direction at a time — a row or a column — called the main axis.
- ✓Container properties (on the parent) control alignment and spacing; item properties (on the children) control individual behavior.
- ✓Flexbox replaced most of the float and inline-block hacks used for layout before it existed.
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).
.row { display: flex; flex-direction: row; /* default — left to right */ } .column { display: flex; flex-direction: column; /* top to bottom */ }
Spacing Items Along the Main Axis
justify-content controls how items are spaced along the main axis — the direction set by flex-direction.
Items bunch at the start of the container (the default).
Items bunch together in the center of the container.
Even space between items; no space at the outer edges.
Even space around every item, including the outer edges.
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.
.row { display: flex; align-items: center; /* vertically centers items */ height: 90px; }
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.
.sidebar { flex: 0 0 220px; /* fixed width, doesn't grow or shrink */ } .content { flex: 1; /* grows to fill remaining space */ }
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.
.card-row { display: flex; gap: 16px; }
Flexbox Is One-Dimensional
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
Flex container & itemsdisplay: flex turns a parent into a flex container; its direct children become flex items.
Directionflex-direction sets the main axis — row (default) or column.
Main-axis spacingjustify-content controls spacing along the main axis: start, center, space-between, space-around.
Cross-axis alignmentalign-items controls alignment perpendicular to the main axis.
Item sizingflex-grow, flex-shrink, and flex-basis (via the flex shorthand) control how items expand or contract.
Try It Yourself
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
Practicing flex container and item properties together
Create day24.html linked to a stylesheet and build the following on the page.
- Build a nav bar with display: flex and justify-content: space-between for the logo and links.
- Build a row of three cards using display: flex and gap: 16px between them.
- Make the middle card grow to fill extra space with flex: 1, keeping the other two fixed-width.
- Center all three cards vertically inside a taller container using align-items: center.
- Switch one flex container to flex-direction: column and confirm the items stack.
Recap
display: flex on a parent turns its children into flex items along a main axis.
justify-content spaces items along the direction set by flex-direction.
align-items aligns items perpendicular to the main axis.
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.