Flexbox: The Complete Guide to Modern CSS Layouts
For decades, CSS layout was a source of frustration for web developers, relying on hacks like floats and table-based structures. Then came Flexbox, a one-dimensional layout model that fundamentally changed how we build user interfaces. It provides a simple, powerful, and (as the name implies) flexible way to arrange, align, and distribute space among items in a container.
The Two Core Concepts: Container & Items
The Flexbox model is built on a parent-child relationship.
- The Flex Container: This is the parent element where you "turn on" flexbox by setting its CSS property to
display: flex;. - Flex Items: These are the **direct children** of the flex container. They are the elements that get arranged by the flexbox properties.
The Main Axis vs. The Cross Axis
This is the single most important concept to grasp. Unlike block layouts (always vertical) or inline layouts (always horizontal), Flexbox is **axis-aware**.
- Main Axis: This is the primary direction your items are laid out. By default, it's horizontal (left-to-right).
- Cross Axis: This is the axis perpendicular to the main axis. By default, it's vertical (top-to-bottom).
Crucially, these axes can be swapped!
Properties for the Flex Container (The Parent)
1. `flex-direction`
This property defines the main axis, establishing the direction items are placed.
row(default): Items flow left-to-right.row-reverse: Items flow right-to-left.column: Items stack top-to-bottom. **This swaps the axes!** The main axis is now vertical, and the cross axis is horizontal.column-reverse: Items stack bottom-to-top.
2. `justify-content`
This aligns items along the main axis. Think of it as distributing empty space.
flex-start(default): Packs items at the start of the axis.flex-end: Packs items at the end of the axis.center: Centers items along the axis.space-between: Distributes items evenly; the first item is at the start, the last at the end.space-around: Distributes items with equal space around them (including half-space at the ends).space-evenly: Distributes items with equal space between them and at the ends.
3. `align-items`
This aligns items along the cross axis.
stretch(default): Stretches items to fill the container's height (or width, if `flex-direction: column`).flex-start: Aligns items to the start of the cross axis.flex-end: Aligns items to the end of the cross axis.center: Centers items on the cross axis.baseline: Aligns items based on their text's baseline.
4. `flex-wrap`
By default, flex items try to fit on one line. This property allows them to wrap.
nowrap(default): All items stay on one line (may cause overflow).wrap: Items will wrap onto multiple lines, from top to bottom.wrap-reverse: Items wrap onto multiple lines from bottom to top.
5. `align-content`
This property is often confused with `align-items`. `align-content` only works when there are **multiple lines** (i.e., `flex-wrap: wrap` is set). It aligns the *lines themselves* within the container.
Properties for Flex Items (The Children)
1. `flex-grow`
A unitless value that dictates how much an item can grow to fill available space. A value of `1` means "take up an equal share of the extra space." A value of `2` would take twice as much as an item with `1`.
2. `flex-shrink`
The opposite of `flex-grow`. It dictates how much an item can shrink if there isn't enough space. The default is `1`.
3. `flex-basis`
This defines the default size of an item *before* any growing or shrinking occurs. It's often better than `width` in a flex context.
4. `flex` (Shorthand)
This is the shorthand for `flex-grow`, `flex-shrink`, and `flex-basis`. It's highly recommended.
flex: 0 1 auto;(default): Won't grow, will shrink, has automatic size.flex: 1;(common): Shorthand for `flex: 1 1 0;`. It means "grow to fill space."flex: auto;: Shorthand for `flex: 1 1 auto;`.flex: none;: Shorthand for `flex: 0 0 auto;`. Won't grow or shrink.
5. `align-self`
This allows a single item to override the container's `align-items` property.
Key Takeaway: Start by settingdisplay: flexon the container. Useflex-directionto set your main axis. Usejustify-contentto align on that axis andalign-itemsto align on the cross axis. Useflex-growon items to create responsive, space-filling layouts.