How Flexbox Works
Flexbox (Flexible Box Layout) is a one-dimensional CSS layout model designed to provide a more efficient
way to lay out, align and distribute space among items in a container, even when their size is unknown
and/or dynamic.
Its core concepts include two axes: Main Axis and Cross Axis.
flex-direction determines the direction of the main axis, while
justify-content controls alignment along the main axis, and align-items
controls alignment along the cross axis.
Properties Explained
justify-content
Defines how items are aligned along the main axis (usually horizontal).
flex-start: Align to start (default)center: Align to centerspace-between: Distribute evenly, start/end flushspace-around: Distribute evenly with spacing around
align-items
Defines how items are aligned along the cross axis (usually vertical).
stretch: Stretch to fill (default)center: Align to centerflex-start: Align to startbaseline: Align to baseline
flex-direction
Determines the direction of the main axis.
row: Left to right (default)column: Top to bottomrow-reverse: Right to left
flex-wrap
Determines whether items should wrap onto multiple lines.
nowrap: No wrap (default)wrap: Wrap to next line
FAQ
Flexbox vs Grid? ▼
How to center items vertically? ▼
display: flex; and align-items: center; on the container. To
center both vertically and horizontally, add justify-content: center;.
justify-content: space-between vs space-around? ▼
Is gap supported? ▼
gap property
for Flexbox.
Why align-items not working? ▼
align-items: center won't show any effect because the container is already
"tight" around the items.
Does row-reverse change DOM order? ▼
Best use cases for Flexbox? ▼
align-content vs align-items? ▼
align-items aligns items in a single line. align-content aligns
multiple lines of items (when flex-wrap: wrap is on). It has no effect on single-line flex
loops.