Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 4: Layouts, Positioning & Modern Flexbox
CSS ⏱ 14 min read

Flexbox Complete Guide: Container Properties

Flexbox revolutionized CSS layout. It makes distributing space, aligning items vertically, and building responsive rows effortless.

What is it?

Flexbox (Flexible Box Layout) is a one-dimensional layout model designed for distributing space among items along either a horizontal row or a vertical column.

Why is it used?

Eliminates cumbersome float hacks and clears. Perfect for navbars, card grids, toolbars, and centering elements perfectly both horizontally and vertically.

Detailed Explanation

Flexbox activates when you set `display: flex` on a parent container. Key Container Properties: • `flex-direction`: `row` (default, left to right), `column` (top to bottom), `row-reverse`, `column-reverse`. • `justify-content` (main axis alignment): `flex-start`, `center`, `flex-end`, `space-between`, `space-around`, `space-evenly`. • `align-items` (cross axis alignment): `stretch` (default), `center`, `flex-start`, `flex-end`. • `flex-wrap`: `nowrap` (default, squishes items) or `wrap` (items drop to next line when space runs out). • `gap`: Sets clean spacing between flex items without annoying margin resets on the first/last item!

Syntax Structure

Syntax Blueprint
.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

Code Example

CSS Code
/* Navigation Bar Layout with Flexbox */
.navbar {
  display: flex;
  justify-content: space-between; /* Pushes logo left, nav items right */
  align-items: center;            /* Vertical centering */
  padding: 16px 24px;
  background-color: #ffffff;
  border-bottom: 1px solid #eceae4;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 20px;                      /* Clean spacing between links */
  list-style: none;
  margin: 0;
  padding: 0;
}

/* Perfect Centering (The holy grail of CSS) */
.center-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
}

Explanation of the Code

• `justify-content: space-between` places max space between logo and nav links. • `gap: 20px` provides consistent gutter spacing between links. • `justify-content: center; align-items: center` centers anything in 3 lines of code.

Expected Output

A modern website navigation header with logo aligned left, navigation links aligned right, and vertical centering.

Real-World Application

Used across 99% of modern web apps for navigation bars, card action button rows, and form input button groups.

Common Mistakes to Avoid

• Confusing `justify-content` (main axis) with `align-items` (cross axis). • Forgetting `flex-wrap: wrap` when building responsive rows that need to stack on smaller screens.

Quick Practice Challenge

Create a flex container that perfectly centers an `<h2>` both horizontally and vertically inside a 300px tall box.

Key Takeaways

• Flexbox is a 1-dimensional layout system. • `justify-content` aligns along the main axis; `align-items` aligns cross axis. • Use `gap` for modern, hassle-free gutter spacing between items.