Master the one-dimensional layout system for creating flexible and responsive layouts.
CSS Flexbox (Flexible Box Layout) is a one-dimensional layout method for arranging items in rows or columns. It provides a more efficient way to distribute space among items in a container and align them.
Flexbox: One-dimensional layout (either rows OR columns)
CSS Grid: Two-dimensional layout (rows AND columns)
Use Flexbox for components, CSS Grid for page layouts!
To create a flexbox layout, you need to set the display
property to flex
or inline-flex
on a container element.
/* Basic flex container */
.flex-container {
display: flex;
}
/* Inline flex container */
.inline-flex {
display: inline-flex;
}
Default flex layout (row direction, items aligned to start)
The flex-direction
property defines the direction of the flex items.
/* Row direction (default) */
.flex-container {
display: flex;
flex-direction: row;
}
/* Row reverse */
.flex-container {
display: flex;
flex-direction: row-reverse;
}
/* Column direction */
.flex-container {
display: flex;
flex-direction: column;
}
/* Column reverse */
.flex-container {
display: flex;
flex-direction: column-reverse;
}
The flex-wrap
property determines whether flex items wrap to new lines.
/* No wrap (default) */
.flex-container {
display: flex;
flex-wrap: nowrap;
}
/* Wrap to new line */
.flex-container {
display: flex;
flex-wrap: wrap;
}
/* Wrap reverse */
.flex-container {
display: flex;
flex-wrap: wrap-reverse;
}
/* Shorthand */
.flex-container {
display: flex;
flex-flow: row wrap; /* direction wrap */
}
The justify-content
property aligns flex items along the main axis (horizontal in row direction).
/* Start alignment (default) */
.flex-container {
display: flex;
justify-content: flex-start;
}
/* End alignment */
.flex-container {
display: flex;
justify-content: flex-end;
}
/* Center alignment */
.flex-container {
display: flex;
justify-content: center;
}
/* Space between */
.flex-container {
display: flex;
justify-content: space-between;
}
/* Space around */
.flex-container {
display: flex;
justify-content: space-around;
}
/* Space evenly */
.flex-container {
display: flex;
justify-content: space-evenly;
}
The align-items
property aligns flex items along the cross axis (vertical in row direction).
/* Stretch (default) */
.flex-container {
display: flex;
align-items: stretch;
}
/* Start alignment */
.flex-container {
display: flex;
align-items: flex-start;
}
/* End alignment */
.flex-container {
display: flex;
align-items: flex-end;
}
/* Center alignment */
.flex-container {
display: flex;
align-items: center;
}
/* Baseline alignment */
.flex-container {
display: flex;
align-items: baseline;
}
The align-content
property aligns flex lines when there's extra space in the cross axis (only works with multiple lines).
/* Stretch (default) */
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: stretch;
}
/* Start alignment */
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
/* End alignment */
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: flex-end;
}
/* Center alignment */
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: center;
}
/* Space between */
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
/* Space around */
.flex-container {
display: flex;
flex-wrap: wrap;
align-content: space-around;
}
You can control individual flex items using specific properties.
/* Flex grow */
.flex-item {
flex-grow: 1; /* Item can grow */
}
/* Flex shrink */
.flex-item {
flex-shrink: 1; /* Item can shrink */
}
/* Flex basis */
.flex-item {
flex-basis: 200px; /* Initial size */
}
/* Shorthand flex */
.flex-item {
flex: 1 1 200px; /* grow shrink basis */
}
/* Align self */
.flex-item {
align-self: center; /* Override container alignment */
}
/* Order */
.flex-item {
order: 2; /* Change display order */
}
Property | Values | Description |
---|---|---|
flex-grow |
number (default: 0) | How much item can grow relative to others |
flex-shrink |
number (default: 1) | How much item can shrink relative to others |
flex-basis |
length, auto (default: auto) | Initial size of item |
flex |
grow shrink basis | Shorthand for flex-grow, flex-shrink, flex-basis |
align-self |
auto, flex-start, flex-end, center, baseline, stretch | Override container's align-items |
order |
integer (default: 0) | Display order of item |
flex: 1
instead of separate properties/* Centered card */
.card {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
/* Navigation bar */
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
/* Form layout */
.form-group {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
/* Responsive sidebar */
.sidebar {
display: flex;
flex-direction: column;
flex: 0 0 250px; /* Don't grow, don't shrink, 250px basis */
}
/* Equal height columns */
.equal-height {
display: flex;
gap: 1rem;
}
.equal-height > * {
flex: 1;
}