Design & CSS

CSS Flexbox Complete Guide: Layout Made Easy

10 min read  ·  Updated 2025

CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that lets you control how elements are arranged, aligned, and distributed inside a container. It replaced decades of float-based and table-based layout hacks, making complex layouts achievable with just a few properties.

The Two Actors: Container and Items

Flexbox has two levels: the flex container (the parent with display: flex) and flex items (the direct children). Container properties control the layout; item properties override individual behaviour.

Container Properties

PropertyValuesEffect
displayflex | inline-flexActivates Flexbox on the container
flex-directionrow | column | row-reverse | column-reverseSets the main axis direction
justify-contentflex-start | flex-end | center | space-between | space-around | space-evenlyAligns items along main axis
align-itemsstretch | flex-start | flex-end | center | baselineAligns items along cross axis
flex-wrapnowrap | wrap | wrap-reverseWhether items wrap to new lines
gaplengthSpace between flex items

Item Properties

PropertyDescription
flex-growHow much the item grows relative to others (0 = no growth)
flex-shrinkHow much the item shrinks relative to others (0 = won't shrink)
flex-basisInitial size before growing/shrinking
flexShorthand: flex-grow flex-shrink flex-basis
align-selfOverride align-items for a specific item
orderChange visual order without changing HTML order

Common Patterns

Perfect Centering

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

Navbar with Logo Left + Links Right

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Equal-Width Cards

.card-container {
  display: flex;
  gap: 1rem;
}
.card {
  flex: 1; /* Each card takes equal share of available width */
}

Sticky Footer

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
main {
  flex: 1; /* Pushes footer to bottom */
}

Experiment with all Flexbox properties in BrainBoost's CSS Flexbox Playground. Also try the CSS Grid Playground for two-dimensional layouts.

Frequently Asked Questions

Apply display: flex; justify-content: center; align-items: center; to the parent container. This centres the child both horizontally and vertically.

justify-content controls alignment along the main axis (horizontal by default). align-items controls the cross axis (vertical by default). These axes swap when flex-direction is column.

flex: 1 is shorthand for flex-grow: 1, flex-shrink: 1, flex-basis: 0%. The item will grow to fill available space and shrink if needed. Used to make multiple items share space equally.