Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Flexbox – Fundamentals | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 29 of 50

·css

Flexbox – Fundamentals

12 mins

Course progress58%

What you'll learn

Keep this idea in mind as you work through the lesson.

1 point
  • `flex:1` is shorthand for `flex-grow:1; flex-shrink:1; flex-basis:0%`. * `gap` works on Flexbox (supported in modern browsers) and is preferred over margins. * Flexbox is ideal for one‑dimensional layout, e.g., nav bars, button groups.

Flexbox is a one‑dimensional layout model (row or column) that makes aligning items a breeze.

Turning an Element into a Flex Container

.container {
  display:flex;            /* flex‑row by default */
  gap:1rem;                /* modern spacing, replaces margins */
  flex-wrap:nowrap;        /* keep everything on a single line */
}

Main‑Axis Properties (horizontal by default)

  • 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.

Cross‑Axis Properties

  • 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 Item Properties

  • 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.

Example – Centered Navigation

<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; }

Pro Tip – Use gap on Flexbox

Older browsers didn’t support gap on Flexbox, but all modern browsers do. It’s far cleaner than fiddling with side‑margin on the children.

Accessibility Note

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.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonAdvanced Grid – Named Areas & Sub‑GridsContinue to lesson 30Advanced Flexbox – Wrapping & AlignmentBack to CSS Basics to Advanced curriculum