flex-direction & flex-wrap
flex-direction controls the main axis direction (row or column). flex-wrap controls whether items wrap to new lines or stay on one line. Together they define the overall flow of flex containers.
Flex Direction
Value | Direction | Main Axis |
|---|---|---|
row | Left to right | Horizontal (default) |
row-reverse | Right to left | Horizontal reversed |
column | Top to bottom | Vertical |
column-reverse | Bottom to top | Vertical reversed |
CSS
/* Row (default) */
.container {
display: flex;
flex-direction: row;
/* Items flow left to right */
}
/* Row reverse */
.container {
display: flex;
flex-direction: row-reverse;
/* Items flow right to left */
}
/* Column */
.container {
display: flex;
flex-direction: column;
/* Items stack vertically */
}
/* Column reverse */
.container {
display: flex;
flex-direction: column-reverse;
/* Items stack bottom to top */
}Flex Wrap
CSS
/* No wrap (default) */
.container {
display: flex;
flex-wrap: nowrap;
/* All items on one line, shrink if needed */
}
/* Wrap to multiple lines */
.container {
display: flex;
flex-wrap: wrap;
/* Items wrap to new line if no space */
}
/* Wrap reverse */
.container {
display: flex;
flex-wrap: wrap-reverse;
/* Wraps but reverses line order */
}
/* Combination */
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 20px;
}Practical Examples
CSS
/* Responsive grid that wraps */
.gallery {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.gallery-item {
flex: 1;
min-width: 200px;
/* Grows equally, wraps when needed */
}
/* Vertical form layout */
.form {
display: flex;
flex-direction: column;
gap: 15px;
}
form label, form input {
/* Stack vertically */
}
/* Reverse order for mobile */
.two-column {
display: flex;
flex-direction: column;
/* Column on mobile */
}
@media (min-width: 768px) {
.two-column {
flex-direction: row;
/* Row on desktop */
}
}Note
flex-direction controls main axis. flex-wrap controls wrapping. Combine for responsive layouts without media queries.
Next
Justify content: [justify-content](/css/justify-content).