Lesson 29 of 50
cssFlexbox – Fundamentals
12 mins
Course progress58%
What you'll learn
Keep this idea in mind as you work through the lesson.
Lesson 29 of 50
css12 mins
Keep this idea in mind as you work through the lesson.
Flexbox is a one‑dimensional layout model (row or column) that makes aligning items a breeze.
.container {
display:flex; /* flex‑row by default */
gap:1rem; /* modern spacing, replaces margins */
flex-wrap:nowrap; /* keep everything on a single line */
}
justify-content – distributes free space along the main axis (flex-start, center, space-between, space-around, space-evenly).flex-direction – row (default) or column; can be reversed with row-reverse / column-reverse.flex-wrap – nowrap (default), wrap, or wrap-reverse.align-items – aligns all flex items on the cross axis (stretch, flex-start, center, baseline).align-content – distributes space between lines when there are multiple rows (requires flex-wrap:wrap).flex-grow – how much the item will expand relative to siblings.flex-shrink – how much the item will contract when space is scarce.flex-basis – default size before growing/shrinking (can be auto, 0, or a length).order – changes visual order without touching the DOM.<ul class="nav">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
.nav {
display:flex;
justify-content:center; /* horizontally center */
gap:2rem; /* automatic spacing */
list-style:none;
padding:0;
}
.nav a { text-decoration:none; color:#0066cc; }
gap on Flexboxgap on Flexbox, but all modern browsers do. It’s far cleaner than fiddling with side‑margin on the children.When using order to rearrange items for visual purposes, do not rely on it to change the reading order for assistive technology. Keep the DOM order logical.