CSS Flexbox

Master the one-dimensional layout system for creating flexible and responsive layouts.

What is CSS Flexbox?

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.

Key Features:

  • One-dimensional: Control either rows OR columns (not both)
  • Flexible: Items can grow and shrink to fill available space
  • Responsive: Easy to create responsive layouts
  • Powerful alignment: Precise control over item alignment
  • Widely supported: Excellent browser support

Flexbox vs Grid

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!

Flex Container

Creating a Flex Container

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;
}

Basic Flexbox Demo

1
2
3
4

Default flex layout (row direction, items aligned to start)

Flex Direction

Controlling Flex Direction

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;
}

Flex Direction Examples

Row (Default)
1
2
3
Row Reverse
1
2
3
Column
1
2
3
Column Reverse
1
2
3

Flex Wrap

Controlling Flex Wrap

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 */
}

Flex Wrap Examples

No Wrap (Default)
Item 1
Item 2
Item 3
Item 4
Wrap
Item 1
Item 2
Item 3
Item 4
Wrap Reverse
Item 1
Item 2
Item 3
Item 4

Justify Content

Horizontal Alignment

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;
}

Justify Content Examples

Flex Start (Default)
1
2
3
Center
1
2
3
Flex End
1
2
3
Space Between
1
2
3
Space Around
1
2
3
Space Evenly
1
2
3

Align Items

Cross-Axis Alignment

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;
}

Align Items Examples

Stretch (Default)
Short
Taller Item
Medium
Flex Start
Short
Taller Item
Medium
Center
Short
Taller Item
Medium
Flex End
Short
Taller Item
Medium
Baseline
Short
Taller Item
Medium

Align Content

Multi-Line Alignment

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;
}

Align Content Examples (Multi-line)

Flex Start
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Center
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Space Between
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Flex Item Properties

Individual Item Control

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 */
}

Flex Item Properties Reference

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 Item Examples

Flex Grow
Grow 1
Grow 2
Grow 1
Order
Order 3
Order 1
Order 2
Align Self
Start
Center
End

Flexbox Best Practices

Flexbox Guidelines

  • Use for components: Flexbox is perfect for navigation, cards, and form layouts
  • Combine with Grid: Use Flexbox within Grid areas for complex layouts
  • Plan your layout: Consider the main and cross axes before coding
  • Use shorthand properties: flex: 1 instead of separate properties
  • Consider accessibility: Ensure logical tab order and screen reader support
  • Test responsiveness: Verify layouts work on all screen sizes
  • Use flex-basis: Set initial sizes for better control
  • Avoid fixed widths: Let flexbox handle sizing when possible

Common Flexbox Patterns

/* 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;
}
Back to CSS Grid Back to Advanced CSS