Note Setting display: flex turns direct children into flex items. The default direction is row (horizontal). Items shrink to fit by default but do not wrap.
Note justify-content distributes items along the main axis (horizontal in row, vertical in column). space-between puts the first and last items at the edges. space-evenly distributes equal space around every item including edges.
align-items: stretch | flex-start | center | flex-end | baseline;
example
/* Vertically center items in a row */
.toolbar {
display: flex;
align-items: center;
height: 64px;
}
/* Align text baselines across different font sizes */
.price-row {
display: flex;
align-items: baseline;
}
Note align-items controls alignment on the cross axis (vertical in row, horizontal in column). The default is stretch, which makes items fill the container height. Use baseline to align items by their text baseline regardless of size.
Note By default, flex items squeeze onto one line (nowrap). With wrap, items flow to the next line when they run out of space. This is essential for responsive layouts without media queries.
flex wrapwrap itemsmulti-line flexwrap to next line
Note gap creates space between flex items without adding margins to outer edges. It works in both flexbox and grid. This eliminates the need for margin hacks on the first or last child.
.sidebar {
flex: 00280px; /* fixed width, no grow or shrink */
}
.main-content {
flex: 1; /* grow to fill remaining space */
}
.equal-columns > * {
flex: 110%; /* equal width columns */
}
Note flex: 1 is shorthand for flex: 1 1 0%, meaning the item can grow and shrink and starts from zero base size. flex-basis sets the initial size before growing/shrinking. Using 0% basis makes items share space equally.
Note align-self overrides the container's align-items value for a single flex item. It applies on the cross axis. There is no justify-self in flexbox since the main axis is controlled by the container.
Note The default order is 0. Lower values appear first. This changes visual order only, not tab order or screen reader order, so use sparingly to avoid accessibility confusion.
/* Center a single element both horizontally and vertically */
.page-center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Shorter alternative using place properties */
.quick-center {
display: flex;
place-content: center;
place-items: center;
}
Note This three-line pattern is the simplest way to center anything vertically and horizontally. Add min-height: 100vh to center within the full viewport.
center divcenter elementvertical centerhorizontal centercenter anything
Flex Spacer Pattern (Push Items Apart)
syntax
margin-left: auto; /* or margin-right: auto */
example
/* Push the last item to the far right */
.navbar {
display: flex;
align-items: center;
gap: 16px;
}
.navbar .user-menu {
margin-left: auto;
}
Note Auto margins in flexbox consume all available space in that direction. This pushes elements apart without justify-content: space-between, which is useful when you need only one item pushed to the end.
push to rightauto marginflex spacerpush item endflex margin auto