CSS Flexbox Layout Module Complete Guide

CSS Flexible Box Layout (Flexbox) is a one-dimensional layout model designed to distribute space dynamically and align items efficiently along rows or columns, even when their dimensions are unknown or dynamically changing. Flexbox revolutionized web development by permanently ending layout hacks and centering struggles.

CSS Flexbox Layout Architecture Cheat Sheet
Figure 5.2: CSS Flexbox Architecture: Main Axis, Cross Axis & Alignment Properties

The Two Pillars: Flex Container vs. Flex Items

Flexbox operates on a strict parent-child relationship: styling the Flex Container controls macro alignment, while properties on Flex Items control individual sizing and ordering.

1. Properties for the Flex Container (Parent)

Property Standard Values Layout Function
display: flex flex, inline-flex Activates the flexbox formatting context for all direct children.
flex-direction row (default), column, row-reverse, column-reverse Establishes the Main Axis direction (horizontal row or vertical column).
justify-content flex-start, center, flex-end, space-between, space-around, space-evenly Aligns items along the Main Axis (distributes extra horizontal space).
align-items stretch (default), center, flex-start, flex-end, baseline Aligns items along the perpendicular Cross Axis (vertical centering).
flex-wrap nowrap (default), wrap, wrap-reverse Allows items to break into multiple lines when screen width is constrained.
gap 15px, 1rem 2rem Defines clean gutters between items without needing margin hacks!

2. Properties for Flex Items (Children)

The flex shorthand combines three powerful child sizing properties:

/* Shorthand Syntax: flex: [flex-grow] [flex-shrink] [flex-basis]; */
.sidebar {
  flex: 0 0 300px; /* Never grow, never shrink, fixed 300px width */
}
.main-content {
  flex: 1 1 auto;  /* Grows to fill all available spare space */
}

The Holy Grail: Dead-Center an Element in 2 Lines!

Centering an element both horizontally and vertically was the most infamous problem in 1990s and 2000s web development. Flexbox solves it in two simple declarations:

.center-box {
  display: flex;
  justify-content: center;   /* Center along Main Axis */
  align-items: center;       /* Center along Cross Axis */
  height: 300px;
}

BCA Practical Exam: Responsive Navbar with Flexbox

.navbar {
  display: flex;
  justify-content: space-between; /* Brand left, navigation links right */
  align-items: center;
  padding: 15px 30px;
  background-color: #0f172a;
}

.nav-links {
  display: flex;
  gap: 20px;
  list-style: none;
  margin: 0;
  padding: 0;
}

Explore Related CSS Tutorials & Next Steps

Boost your CSS and front-end development proficiency by exploring these closely connected topics:

CSS Box Model & border-box

Understand margins, borders, padding, content and the universal box-sizing fix.

CSS Grid 2D Layout Matrix

Build powerful two-dimensional grid layouts with template areas and fr units.

CSS Specificity & Cascade

Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the difference between space-between and space-evenly?

'space-between' places the first item flush against the start edge and the last item flush against the end edge, distributing remaining space between items. 'space-evenly' ensures equal space before, between, and after every item.

Q2. What is flex-basis?

flex-basis defines the initial ideal size of a flex item before any extra space is distributed by flex-grow or removed by flex-shrink.

Q3. Can you change item display order without touching HTML?

Yes! Use the 'order' property on flex items. An item with 'order: 2' renders after items with 'order: 1', regardless of where it appears in the HTML source code.

Q4. Does margin: auto work inside Flexbox?

Yes! Setting 'margin-left: auto' on a flex item absorbs all remaining space along the main axis, pushing that item and subsequent items flush to the right (popular for logout buttons in navbars).
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!