CSS Tutorial

CSS Flexbox: Flexible Box Layout, Flex Container, justify-content & Align Explained (2026-27)

By Pramod Behera  ·  Updated: July 2026  ·  15 min read
✅ In this CSS Tutorial – CSS Flexbox: Complete Guide to the Flexible Box Layout, Flex Container, justify-content And Alignment

Today we are discuss topic CSS Flexbox. CSS Flexbox (formally the Flexible Box Layout) is the single most useful layout tool for arranging items in a row or a column -navbars, card rows, button groups, sidebars, and perfectly centered content all rely on it. In this complete guide you will master the flex container (display: flex), flex-direction, CSS Flexbox justify-content for main-axis alignment, CSS Flexbox align-items, align-content and align-self for cross-axis alignment, flex-wrap, the flex item properties flex-grow, flex-shrink and flex-basis, the order property, and gap. Includes live clickable demos for every alignment value, a full interactive Flexbox playground, a "Try It Yourself" code editor with real layout presets, reference tables, common mistakes, a quiz, and FAQ. This tutorial or document breaks down the process step by step, using simple language and real-world examples to help you master the skill.

📋 Table of Contents

  1. What Is CSS Flexbox (Flexible Box Layout)?
  2. The Flex Container – display: flex
  3. flex-direction – row, column & Reverse
  4. CSS Flexbox justify-content – Main Axis Alignment
  5. CSS Flexbox Align – align-items, align-content & align-self
  6. flex-wrap – Wrapping Flex Items
  7. Flex Item Properties – grow, shrink & basis
  8. order & gap
  9. Reference Table – All Flexbox Properties
  10. 🎨 Interactive Flexbox Playground
  11. Try It Yourself – Interactive Editor
  12. Best Practices
  13. Common Mistakes to Avoid
  14. Practice Quiz
  15. Frequently Asked Questions (FAQ)

✅ What Is CSS Flexbox (Flexible Box Layout)?

CSS Flexbox, officially the Flexible Box Layout, is a one-dimensional layout model built specifically for arranging items along a single row or column. Instead of manually calculating widths, floats, or margins, you turn a parent into a flex container and Flexbox automatically handles distributing space, aligning items, and reordering -with just a handful of properties.

📦
Flex Container
The parent with display: flex.
↔️
Main Axis
justify-content controls this.
↕️
Cross Axis
align-items controls this.
🧩
Flex Items
The direct children being arranged.
💡 The single most important idea: Flexbox has two axes -the main axis (the direction items flow in) and the cross axis (perpendicular to it). justify-content always aligns along the main axis, align-items always aligns along the cross axis -and flex-direction decides which physical direction each one actually is.

✅ The Flex Container – display: flex

Adding display: flex; to any element turns it into a flex container, and every direct child instantly becomes a flex item arranged in a row by default -no floats, no manual widths, no clearfix hacks required.

display: flex – Live Demo
.container {
  display: flex;
  gap: 10px;
}

/* children become flex items automatically */
👁 Live Preview
1
2
3
ℹ️ inline-flex: Use display: inline-flex; instead of flex when you want the container itself to behave like an inline element and sit alongside other inline content, while its children still follow all Flexbox rules.

✅ flex-direction – row, column & Reverse

flex-direction sets the main axis. Click each value to watch the same three items rearrange live:

flex-direction: row;
1
2
3

✅ CSS Flexbox justify-content – Main Axis Alignment

justify-content distributes extra space along the main axis. This is the property most people reach for first when centering or spacing out items in a row.

justify-content: flex-start;
1
2
3
ValueEffect
flex-startItems packed at the start of the main axis (default).
centerItems packed toward the center of the main axis.
flex-endItems packed at the end of the main axis.
space-betweenEqual space between items; no space at the outer edges.
space-aroundEqual space around each item, so edge gaps are half of between-item gaps.
space-evenlyPerfectly equal space between items AND at both outer edges.

✅ CSS Flexbox Align – align-items, align-content And align-self

align-items is the cross-axis counterpart to justify-content -it controls how items line up perpendicular to the main axis. Click each value below (the demo has extra height so you can see the vertical effect):

align-items: stretch;
1
2
3
PropertyControls
align-itemsCross-axis alignment of items within a single flex line.
align-contentSpacing between multiple flex lines (only visible when flex-wrap: wrap causes wrapping).
align-selfOverrides align-items for one specific item only.
💡 Perfect centering: display: flex; justify-content: center; align-items: center; on a parent centers a child both horizontally and vertically -one of the most reused three-line patterns in all of CSS.

✅ flex-wrap – Wrapping Flex Items

By default, flex items try to shrink to fit on one line. flex-wrap: wrap; instead lets them flow onto additional lines once they run out of room -essential for responsive card grids and tag lists.

flex-wrap: nowrap;
1
2
3
4
5
6

✅ Flex Item Properties – flex-grow, flex-shrink & flex-basis

These three properties are almost always used together via the flex shorthand, which controls how an individual item grows, shrinks, and what size it starts from before that growing or shrinking happens.

PropertyDefaultWhat It Does
flex-grow0How much of the extra available space this item takes, relative to siblings.
flex-shrink1How much this item shrinks, relative to siblings, when space is too tight.
flex-basisautoThe item's starting size before grow/shrink is applied.
flex (shorthand)0 1 autoSets grow, shrink and basis together -e.g. flex: 1 1 200px;
flex: 1 – Equal Growing Columns
.col {
  flex: 1;
}

/* all three columns share space equally */
👁 Live Preview
flex: 1
flex: 1
flex: 1

✅ order & gap

order changes an item's visual position without touching the HTML source order -lower numbers appear earlier, and the default order value for every item is 0. gap adds consistent spacing between flex items without needing margin hacks or extra wrapper elements.

.first-in-html { order: 2; } /* pushed later visually */
.last-in-html  { order: -1; } /* pulled to the front visually */
.container     { gap: 16px; } /* space between every item */
⚠️ order is visual only: Changing order does not change tab order or screen-reader reading order, which still follow the HTML source -avoid using order to fix accessibility issues; fix the HTML order instead where reading order matters.

✅ Reference Table – All Flexbox Properties

PropertyApplied ToPurpose
display: flexContainerCreates the flex container.
flex-directionContainerSets the main axis: row, column, and reverses.
flex-wrapContainerAllows items to wrap onto multiple lines.
justify-contentContainerAligns items along the main axis.
align-itemsContainerAligns items along the cross axis (single line).
align-contentContainerAligns multiple wrapped lines on the cross axis.
gapContainerSets spacing between items and rows.
flex-grow / flex-shrink / flex-basisItemControls how one item grows, shrinks and its base size.
align-selfItemOverrides align-items for a single item.
orderItemChanges visual position without changing HTML order.

✅ 🎨 Interactive Flexbox Playground

Change every major Flexbox container property at once and watch six items respond live.

🎛 Flexbox Playground
CSS Output

✅ Try It Yourself – Interactive Editor

Load a real Flexbox layout preset below and edit the HTML/CSS directly -the preview updates automatically as you type.

🖥 Interactive Flexbox Editor
👁 Live Preview

✅ Best Practices

✔️ 1) Use gap instead of margin for spacing
gap on the flex container spaces every item consistently without the classic "extra margin on the last child" cleanup problem.

✔️ 2) Reach for flex: 1 for equal-width columns
Applying flex: 1 to every column in a row is the simplest way to make them share available space equally, regardless of how many there are.

✔️ 3) Remember flex-direction swaps the axes
Switching to flex-direction: column means justify-content now controls vertical spacing and align-items now controls horizontal -always double-check which axis you actually mean to target.

✔️ 4) Combine Flexbox with Grid, not against it
Use CSS Grid for the overall two-dimensional page structure and Flexbox for the one-dimensional components inside it (navbars, button groups, card content) -they complement each other well.

✅ Common Mistakes to Avoid

❌ Mistake 1 – Confusing justify-content and align-items
justify-content always targets the main axis, align-items always targets the cross axis -which one is "horizontal" depends entirely on flex-direction.
❌ Mistake 2 – Forgetting flex-wrap when items overflow
Without flex-wrap: wrap;, flex items shrink (sometimes awkwardly) to fit one line instead of flowing onto a new row.
❌ Mistake 3 – Using order to fix reading/accessibility order
order only changes visual position -screen readers and keyboard tab order still follow the HTML source, which can create a confusing mismatch.
❌ Mistake 4 – Expecting align-content to work without wrapping
align-content only has a visible effect when there are multiple flex lines from flex-wrap: wrap -on a single line it does nothing.
❌ Mistake 5 – Reaching for Flexbox for a full two-dimensional grid
Flexbox is one-dimensional; when you need precise alignment across both rows and columns at once, CSS Grid is usually the better tool.

✅ Practice – Yes / No Quiz

1. Does justify-content align items along the main axis?

2. Does flex-direction: column make align-items control horizontal alignment instead of vertical?

3. Does align-content have a visible effect when there is only one flex line (no wrapping)?

4. Does the order property change an element's position in the actual HTML source?

5. Is flex: 1 shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%;?

0/5
Your Score – Keep Practising! 🎯

✅ Frequently Asked Questions (FAQ)

What is CSS Flexbox?
CSS Flexbox, formally the Flexible Box Layout, is a one-dimensional layout system for arranging items in a row or a column inside a flex container. It automatically handles distributing extra space, aligning items, and wrapping content, making it the standard tool for navbars, card rows, centering content, and many other common UI patterns.
What is the difference between justify-content and align-items?
justify-content aligns items along the main axis -the direction items flow in, which is horizontal by default with flex-direction: row. align-items aligns items along the cross axis, perpendicular to the main axis, which is vertical by default in a row layout. Switching flex-direction to column swaps which axis each property controls.
How do I center something perfectly with Flexbox?
Set display: flex; justify-content: center; align-items: center; on the parent container. This centers the child both horizontally and vertically in a single, well-known three-line pattern that works regardless of the child's size.
What does flex: 1 mean?
flex: 1 is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%;. It tells an item to grow and fill any available extra space in the container, and when applied equally to multiple items, they end up sharing the remaining space equally.
What is the difference between align-items and align-content?
align-items aligns items within a single flex line on the cross axis. align-content controls the spacing between multiple flex lines when flex-wrap: wrap; causes items to wrap onto more than one row -it has no visible effect if there is only one line of items.
Does Flexbox work for two-dimensional layouts like a full page grid?
Flexbox is a one-dimensional layout model -it excels at arranging items in a single row or column. For true two-dimensional layouts that need to align content in both rows and columns simultaneously, CSS Grid is generally the better tool; many real interfaces combine both, using Grid for the page structure and Flexbox for components within it.
✍️ About the Author – Pramod Behera

Pramod Behera is the founder of LearnToSAP.com and an experienced web development educator. He creates beginner-friendly tutorials on HTML, CSS, SAP SD/MM, and frontend development, helping thousands of learners worldwide build practical skills.