Coding/Learn/css/flexbox basics
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnFlexbox Part 1: Container Powers (justify, align, gap)
13 / 20 0%
Lesson 13 of 20
5 mins read

Flexbox Part 1: Container Powers (justify, align, gap)

PreviousNext Lesson
In Plain English

Before Flexbox, centering a div or building a navbar was the most frustrating task in web design. Setting display: flex on a parent container turns all immediate children into flexible items that you can distribute, space, and center with 3 lines of code.

The Mental Model

"Think of display: flex as an intelligent conveyor belt. You turn on the belt (display: flex), tell it where to push boxes (justify-content), how to vertically align them (align-items), and how much space to leave between them (gap)."

Deep Dive: How It Works

display: flex: Enables flexbox mode on the parent container. Children automatically line up side-by-side horizontally.

justify-content (Main Axis / Horizontal): space-between (navbar logo left, links right), center (centered), flex-start, flex-end, space-around.

align-items (Cross Axis / Vertical): center (perfect vertical alignment!), flex-start, flex-end, stretch.

gap: Sets uniform spacing between flex children without having to write annoying margin-right rules on every item!

Syntax Blueprint

/* The Sacred Navbar Formula */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

Pushes brand logo to the left, nav links to the right, and centers vertically.

PointCore Rules to Remember

Point star
The Centering Miracle: justify-content: center; align-items: center; perfectly centers anything in 2 lines.
Point star
The gap Property: Always use gap instead of old margin hacks for spacing flex items.
Point star
Applied to the Parent: Flex container properties must be written on the parent wrapper, not the children.

Common Beginner Traps & How to Fix Them

Mistake:Putting justify-content on the child element

Why it happens: Thinking child elements control their own alignment.

How to fix: justify-content and align-items belong strictly on the parent container with display: flex.

Pro Tip: In modern UI development, over 80% of all components (navbars, card rows, toolbars) are built with Flexbox!

Live Interactive Example

Hit Run Code to see it live
The Universal Navbar Layout
CSS3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: justify-content: space-between pushes the ends apart while align-items: center aligns heights.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Center items with Flexbox

  • PointWrite a CSS rule for .flex-center setting display: flex; justify-content: center; and align-items: center;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates