← Back to all tutorials

Flex Containers

Understand the flex container — display: flex, flex-direction, justify-content, and how flex items behave.

Flex Containers

Everything in Flexbox starts with the flex container — the parent element that holds and controls the layout of its children. Setting display: flex on any element turns it into a flex container.

Creating a Flex Container

.container {
    display: flex;
}

/* Or inline flex (doesn't take full width) */
.container {
    display: inline-flex;
}

flex vs inline-flex

PropertyBehavior
display: flexBlock-level — takes full width of parent
display: inline-flexInline-level — only as wide as its content

What Happens When You Add display: flex

  1. All direct children become flex items
  2. Items sit side by side in a row (default direction)
  3. Items stretch to fill the container height (default alignment)
  4. Items don't wrap — they shrink to fit in one line
<div class="container">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
</div>
.container {
    display: flex;
    background: #f0f4f8;
    padding: 10px;
}

.box {
    background: #3498db;
    color: white;
    padding: 20px 30px;
    margin: 5px;
    font-size: 18px;
    font-weight: bold;
}

The three boxes automatically sit in a horizontal row.

flex-direction

Controls the direction flex items are placed in the container:

ValueItems FlowMain Axis
row (default)Left → RightHorizontal →
row-reverseRight → LeftHorizontal ←
columnTop → BottomVertical ↓
column-reverseBottom → TopVertical ↑
/* Column layout — items stack vertically */
.container {
    display: flex;
    flex-direction: column;
}

/* Reverse row — items go right to left */
.container {
    display: flex;
    flex-direction: row-reverse;
}

justify-content — Main Axis Alignment

Controls how items are distributed along the main axis:

ValueBehavior
flex-start (default)Items packed to the start
flex-endItems packed to the end
centerItems centered
space-betweenEqual space between items (no space at edges)
space-aroundEqual space around each item (half-space at edges)
space-evenlyEqual space between and at edges
/* Center items horizontally */
.container {
    display: flex;
    justify-content: center;
}

/* Space items equally with no edge gaps */
.container {
    display: flex;
    justify-content: space-between;
}

gap — Spacing Between Items

.container {
    display: flex;
    gap: 16px;       /* Same gap in both directions */
    gap: 16px 24px;  /* row-gap | column-gap */
}

The gap property adds space between flex items without affecting the edges. Much cleaner than using margins.

All Container Properties Summary

PropertyWhat It Controls
displayflex or inline-flex
flex-directionDirection of the main axis
justify-contentAlignment along the main axis
align-itemsAlignment along the cross axis
flex-wrapWhether items wrap to the next line
gapSpacing between items
align-contentAlignment of wrapped lines

Key Takeaways

  • display: flex turns a container's children into flex items in a row
  • flex-direction changes the flow: row, column, row-reverse, column-reverse
  • justify-content distributes items along the main axis
  • gap adds clean spacing between items without margin hacks
  • Only direct children become flex items — nested elements are unaffected