Learn the concept
Flexbox Layout
Flexbox uses a main axis (direction items are laid out) and a cross axis (perpendicular). justify-content aligns items along the main axis, align-items along the cross axis. The flex shorthand controls how items grow, shrink, and their base size.
CSS Flexbox is a one-dimensional layout system for distributing space and aligning items within a container. Understanding the axis model is the key to mastering Flexbox — everything revolves around the main axis and cross axis.
Main axis — the direction items are laid out, controlled by flex-direction:
row (default): left → rightrow-reverse: right → leftcolumn: top → bottomcolumn-reverse: bottom → topCross axis — perpendicular to the main axis. If main axis is horizontal (row), cross axis is vertical, and vice versa.
| Property | Axis | Applied to | Controls |
|----------|------|-----------|----------|
| justify-content | Main | Container | Distribution of items along main axis |
| align-items | Cross | Container | Alignment of items along cross axis |
| align-self | Cross | Individual item | Override align-items for one item |
| align-content | Cross | Container | Distribution of wrapped lines (multi-line only) |
| gap | Both | Container | Space between items |
justify-content values: flex-start, flex-end, center, space-between, space-around, space-evenly
align-items values: stretch (default), flex-start, flex-end, center, baseline
flex is shorthand for flex-grow, flex-shrink, and flex-basis:
flex-grow — How much an item should grow relative to siblings when extra space is available (default: 0 = don't grow)flex-shrink — How much an item should shrink when space is insufficient (default: 1 = shrink equally)flex-basis — The initial size before growing/shrinking (default: auto = use content size or width/height)Common patterns:
flex: 1 = flex: 1 1 0% — Item takes equal share of available spaceflex: auto = flex: 1 1 auto — Item grows/shrinks from its content sizeflex: none = flex: 0 0 auto — Item doesn't grow or shrink (fixed size)flex-wrap: wrap allows items to flow onto multiple lines when they don't fit on one line. Combined with flex-basis, this creates responsive grid-like layouts without media queries:
.container { display: flex; flex-wrap: wrap; gap: 1rem; }
.item { flex: 1 1 300px; } /* Each item is at least 300px, grows to fill */display: flex; justify-content: center; align-items: center;flex-direction: column; min-height: 100vh; with flex: 1 on main contentalign-items: stretch (the default)justify-content: space-betweenflex-wrap: wrap with flex-basis for minimum card widthUse Flexbox for components and linear arrangements. Use Grid for page-level structure and 2D layouts.
/* 1. Perfect centering */
.center-content {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* 2. Navigation bar — logo left, links right */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 1rem;
}
/* 3. Sticky footer */
.page {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-content {
flex: 1; /* Takes all available space, pushing footer down */
}
/* 4. Responsive card grid (no media queries needed) */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 300px; /* Min 300px, grows to fill row */
}
/* 5. Equal-width columns */
.columns {
display: flex;
gap: 1rem;
}
.col {
flex: 1; /* Each column gets equal space */
}E-commerce sites use Flexbox with flex-wrap and flex-basis to create responsive product grids that adapt from 4 columns on desktop to 1 column on mobile without media queries.
Navigation bars use justify-content: space-between for logo/links separation, and align-items: center for vertical alignment of varied-height elements.
Build a dashboard with a sidebar, main content, and header using only Flexbox. The sidebar should collapse on mobile and the content cards should reflow responsively.