In this lesson you will
- Turn any element into a flex container and explain the main and cross axis
- Align and centre items with justify-content, align-items and gap
- Build rows that wrap on phones and items that grow with flex
- Predict how a flex row looks in an Arabic right-to-left page
Flex containers, items and the two axes
Flexbox always involves two levels. The parent with display: flex is the flex container, and only its direct children are flex items. Grandchildren are not affected: a paragraph inside a card stays a normal block even when the card is a flex item. This is the first thing to check when a flex rule seems to do nothing.
A flex container has two directions. The main axis is the direction items are laid out in; with the default flex-direction: row it runs along the line of text. The cross axis runs across it, which for a row means top to bottom. Set flex-direction: column and the axes swap: items stack vertically and the cross axis becomes the width. Every alignment property is defined by these axes, not by left, right, top or bottom.
Because a row follows the direction of the text, it flips by itself in Arabic. In a page with dir="rtl" the first item sits on the right and justify-content: flex-start means the right edge. You do not need a separate Arabic stylesheet for a flex menu, which is one reason Flexbox suits bilingual sites so well.
Console
- Console messages, errors, form data and clicked links appear here.
The same CSS makes both rows. In the dir="rtl" row, item 1 starts on the right. Add flex-direction: column to .row to stack the items.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between Flexbox and CSS Grid?
Flexbox lays out items in one direction at a time, a row or a column, and lets their content decide much of the sizing. Grid lays out rows and columns together, so items line up in both directions. Use Flexbox for menus, toolbars and small components, and Grid for page layouts and card grids. Most sites use both.
How do I centre a div horizontally and vertically with Flexbox?
Make its parent a flex container and add justify-content: center and align-items: center. The parent needs a height taller than the child, for example min-height: 100vh for a full-screen section, otherwise there is no vertical space to centre in.
Why are my flex items not wrapping onto a new line?
Flex containers default to flex-wrap: nowrap, so items shrink instead of wrapping. Add flex-wrap: wrap to the container, and give the items a flex-basis or min-width so they know when there is no longer room.
Is gap supported for Flexbox in all browsers?
Yes, in all current versions of Chrome, Edge, Firefox and Safari; Safari added it in 2021. Older guides use margins on the items instead because gap used to work only in Grid, but for new work gap is simpler and right-to-left safe.
Does Flexbox work the same in Arabic pages?
Yes, and it flips for you. Rows follow dir="rtl", so start and end swap sides automatically. Avoid row-reverse or left and right margins to fake the Arabic direction; use dir, gap and logical properties such as margin-inline-start.
Sources
Facts in this lesson were checked against these official pages on 29 October 2026.
- MDN: Flexbox
- MDN: Basic concepts of flexbox
- MDN: Ordering flex items
- W3C: CSS Flexible Box Layout Module Level 1
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.