Flexbox

One-dimensional layout system for flexible responsive designs

← Back to Reference

What is Flexbox?

Flexbox is a one-dimensional layout method for arranging items in rows or columns. Items flex to fill additional space or shrink to fit smaller spaces. Perfect for navigation bars, card layouts, and centering content.

Key Advantage: Makes alignment and distribution of space incredibly easy, even when item sizes are unknown.

Creating a Flex Container

Turn any element into a flex container to control its children.

.container { display: flex; }

Effect: Direct children become flex items and line up in a row by default

flex-direction

Defines the main axis direction (row or column).

.container { flex-direction: row; }

Values: row (default, left to right), row-reverse, column (top to bottom), column-reverse

justify-content

Aligns items along the main axis (horizontal if row, vertical if column).

.container { justify-content: center; }

Common values: flex-start, center, flex-end, space-between, space-around, space-evenly

align-items

Aligns items along the cross axis (vertical if row, horizontal if column).

.container { align-items: center; }

Common values: stretch (default), flex-start, center, flex-end, baseline

Centering trick: justify-content: center; align-items: center; (perfect center!)

flex-wrap

Controls whether items wrap onto multiple lines.

.container { flex-wrap: wrap; }

Values: nowrap (default, single line), wrap (multiple lines), wrap-reverse

gap

Creates space between flex items.

.container { gap: 20px; }

Example: 20px space between all items

flex (on items)

Controls how flex items grow and shrink. Applied to flex items, not the container.

.item { flex: 1; }

Example: All items grow equally to fill available space

Shorthand for: flex-grow, flex-shrink, flex-basis

← Back to Reference