Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › flex

CSS Reference

flex

The one-dimensional layout system. Arranges items in a row or column with total control over spacing and alignment.

Last updated: October 2026

Definition: Flexbox is a one-dimensional layout model. Set display: flex on a parent, and its direct children become flex items. You can then control direction, alignment, distribution, wrapping, and sizing along a single axis. It replaced float-based layouts and table layouts entirely.

🎬 Live Examples

Four common Flexbox patterns — each one is pure CSS, no JavaScript.

Row layout with gap
Item 1
Item 2
Item 3
Column layout
Item 1
Item 2
Item 3
Perfectly centered
Centered
Space between (like a navbar)
Logo
Menu

💡 Every layout above uses only display: flex + one or two alignment properties.

📝

Basic Syntax

/* Parent becomes a flex container */
.container {
    display: flex;
}

/* All direct children become flex items */

The parent controls layout. Children are positioned automatically.

🎯 Container Properties (Parent)

Property Common Values Purpose
displayflex / inline-flexEnable Flexbox.
flex-directionrow / column / row-reverse / column-reverseSet the main axis direction.
justify-contentflex-start / center / flex-end / space-between / space-around / space-evenlyAlign along the main axis.
align-itemsstretch / flex-start / center / flex-end / baselineAlign along the cross axis.
flex-wrapnowrap / wrap / wrap-reverseAllow items to wrap to a new line.
gapany lengthSpace between flex items.
align-contentflex-start / center / space-between etc.Align wrapped rows (only when flex-wrap: wrap).

🎯 Item Properties (Children)

Property Purpose
flex-growHow much the item grows when there's extra space.
flex-shrinkHow much the item shrinks when there isn't enough space.
flex-basisThe item's default size before growing or shrinking.
flexShorthand for grow, shrink, and basis. flex: 1 is common.
align-selfOverride align-items for a single item.
orderChange the visual order of items.
1

The Navbar Pattern

The most common Flexbox layout — logo on the left, menu on the right.

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

.nav-menu {
    display: flex;
    gap: 1.5rem;
    list-style: none;
}
2

Equal-Width Columns with flex: 1

.row {
    display: flex;
    gap: 1rem;
}

.col {
    flex: 1;  /* All columns grow equally */
}

Every column takes equal width. Change one to flex: 2 and it takes twice as much space.

3

Perfect Centering (The Classic)

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

Two properties. Vertical and horizontal centering. Works for any child content — text, card, modal, form.

4

Responsive Stacking

.cards {
    display: flex;
    flex-direction: column;  /* Mobile: stack */
    gap: 1rem;
}

@media (min-width: 768px) {
    .cards {
        flex-direction: row;  /* Desktop: side-by-side */
    }
}
5

Wrapping Flex Items

.tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

By default, flex items squeeze into one line. flex-wrap: wrap lets them flow onto multiple rows — perfect for tags, chips, and card grids.

🎯 Flex vs Grid — Decision Guide

Scenario Use
Navbar layoutFlex
Card component (image + text)Flex
Button with icon + textFlex
Centering contentFlex
Page layout (header, sidebar, main)Grid
Card grid with equal rows/columnsGrid
Photo gallery (aligned rows)Grid

💡 Rule of thumb: If the layout is 1-dimensional (row OR column), use Flex. If it's 2-dimensional (rows AND columns), use Grid. Both are often combined in the same page.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is CSS Flexbox?

Flexbox is a one-dimensional layout system for arranging items in a row or column. Set display: flex on a parent container, and its direct children become flex items that can be aligned, distributed, and resized along a single axis.

What is the difference between justify-content and align-items?

justify-content aligns items along the main axis (horizontal by default). align-items aligns items along the cross axis (vertical by default). For a flex-row container, justify controls horizontal, align controls vertical.

What is the difference between flex and grid?

Flex is one-dimensional — arranges items in a single row OR column. Grid is two-dimensional — arranges items in rows AND columns simultaneously. Use Flex for components, Grid for page layouts.

How do I center a div with Flexbox?

Set display: flex, justify-content: center, and align-items: center on the parent. This centers the child both horizontally and vertically.

What does flex: 1 do?

flex: 1 is shorthand for flex-grow: 1, flex-shrink: 1, flex-basis: 0%. It makes the item grow to fill available space equally with other flex: 1 items. This is the pattern for equal-width columns.

🎯 What's Next?

← All CSS Reference