CSS Flexbox: Modern Layout Control
Flexbox is a modern and efficient method for creating responsive layouts in CSS. Unlike classic techniques, Flexbox provides control over both the main and cross axes, precise alignment, and automatic space distribution. In this tutorial, we'll explore step by step how Flexbox works and how we can use it in real projects.
1. display
Activates Flexbox mode on a container.
display: flex– activates Flexbox on the current elementdisplay: inline-flex– activates Flexbox, but the container behaves like an inline element
2. flex-direction
Controls the direction in which elements are arranged inside the container.
row– horizontal, from left to right (default)row-reverse– horizontal, from right to leftcolumn– vertical, from top to bottomcolumn-reverse– vertical, from bottom to top
3. justify-content
Controls the distribution of elements along the main axis (horizontal if
flex-direction: row, vertical if flex-direction: column).
flex-start– aligns items at the start of the axisflex-end– aligns items at the end of the axiscenter– centers the itemsspace-between– equal space between items, no space at edgesspace-around– equal space around each itemspace-evenly– equal space between items and container edges
4. align-items
Controls the alignment of items along the cross axis (vertical if
flex-direction: row, horizontal if flex-direction: column).
stretch– items stretch to fill the cross axis (default)flex-start– aligns items at the start of the cross axisflex-end– aligns items at the end of the cross axiscenter– centers items along the cross axisbaseline– aligns items based on the text baseline
5. flex-wrap
Controls whether the items in the container wrap onto multiple lines when there's no more space on the main axis.
nowrap– all items stay on a single line (default)wrap– items wrap onto new lines when space runs outwrap-reverse– likewrap, but the lines are reversed
6. align-content
Controls the alignment of multiple lines along the cross axis, but only works when there are
multiple rows (so flex-wrap must be active).
flex-start– lines are aligned at the start of the cross axisflex-end– lines are aligned at the end of the cross axiscenter– lines are centeredspace-between– equal space between linesspace-around– equal space around each linestretch– lines stretch to fill the cross axis (default)
7. order
Controls the display order of elements in a Flexbox container without changing their order in the HTML.
order: 0– default valueorder: <number>– the smaller the number, the earlier the element appears
8. flex-grow
Controls how much an element can grow to occupy the available space in the container.
flex-grow: 0– the element does not grow (default)flex-grow: 1– the element grows proportionally with the available spaceflex-grow: >1– the element grows more than the others
9. flex-shrink
Controls how much an element shrinks when space is limited.
flex-shrink: 1– the element shrinks proportionally (default)flex-shrink: 0– the element does not shrinkflex-shrink: >1– the element shrinks more aggressively
10. flex-basis
Defines the base size of an element before the available space is distributed. It's similar to
width, but works within the Flexbox context.
auto– uses the size defined by content orwidth<length>– e.g.flex-basis: 200px, sets a fixed size0– ignores the natural size and distributes space based onflex-grow
11. flex (shorthand)
This is a shorthand for flex-grow, flex-shrink, and flex-basis.
flex: 1– equivalent toflex-grow: 1; flex-shrink: 1; flex-basis: 0%flex: 0 1 auto– default valueflex: <grow> <shrink> <basis>– e.g.flex: 2 0 150px
12. align-self
Allows an element to control its own alignment along the cross axis, overriding the container's
align-items.
auto– inherits the value fromalign-itemsflex-start– aligns at the start of the cross axisflex-end– aligns at the end of the cross axiscenter– centered along the cross axisbaseline– aligns based on the text baselinestretch– stretches to fill the cross axis (default)
Flexbox Cheat Sheet
| Property | Values | Description |
|---|---|---|
display |
flex, inline-flex | Activates Flexbox mode |
flex-direction |
row, row-reverse, column, column-reverse | Direction of items in the container |
justify-content |
flex-start, flex-end, center, space-between, space-around, space-evenly | Alignment on the main axis |
align-items |
stretch, flex-start, flex-end, center, baseline | Alignment on the cross axis |
flex-wrap |
nowrap, wrap, wrap-reverse | Wrapping items onto multiple lines |
align-content |
flex-start, flex-end, center, space-between, space-around, stretch | Alignment of multiple lines |
order |
0, 1, 2, ... | Display order |
flex-grow |
0, 1, 2, ... | Growth based on available space |
flex-shrink |
0, 1, 2, ... | Shrinkage when space is limited |
flex-basis |
auto, 0, px, % | Base size |
flex |
shorthand: grow shrink basis | Shorthand for the three above |
align-self |
auto, flex-start, flex-end, center, baseline, stretch | Individual alignment on the cross axis |
Example: Responsive Card Layout
A card layout that automatically adapts to the screen size using flex-wrap and
flex-basis.
Example: Navbar with Flexbox
A simple horizontally aligned navbar with a logo on the left and links on the right, using
display: flex and justify-content: space-between.
Complete Layout for a Mini Site
A solid example of web structure — with header, navigation,
sidebar, main content, and footer — built with clean
HTML and CSS. Ideal for simple projects, dashboards, or personal websites. Everything
is modular and easy to extend with PHP, JS, or modern frameworks.
Flexbox gave you control over horizontal and vertical alignment, but if you want to orchestrate complex layouts like a web architect, it's time to move on to CSS Grid — where every row and column is under your command. Let's get started.
