Introduction
An overview of CSS Flexbox — what it is, why it replaced floats, and how it makes layout simple and powerful.
Introduction
Welcome to the CSS Flexbox Tutorial! Flexbox (Flexible Box Layout) is a CSS layout system designed to distribute space and align items in a container — even when their sizes are unknown or dynamic.
The Problem Before Flexbox
Before Flexbox, developers relied on floats, inline-block, and table display hacks to create layouts. These techniques were:
- ❌ Fragile and unintuitive
- ❌ Required clearfix hacks
- ❌ Difficult to vertically center elements
- ❌ Couldn't easily reorder elements
- ❌ Couldn't distribute remaining space proportionally
What Flexbox Solves
| Problem | Old Solution | Flexbox Solution |
|---|---|---|
| Horizontal layout | float: left + clearfix | display: flex |
| Vertical centering | position: absolute + transform | align-items: center |
| Equal-height columns | JavaScript or table hacks | Automatic with Flexbox |
| Space distribution | Manual percentage widths | flex-grow, justify-content |
| Reordering elements | Change the HTML | order property |
Flexbox in One Line
.container {
display: flex;
}
/* That's it! All direct children become flex items
that sit side by side in a row. */
Key Flexbox Concepts
- Flex Container — the parent element with
display: flex - Flex Items — the direct children of the flex container
- Main Axis — the primary direction items flow (default: horizontal →)
- Cross Axis — the perpendicular direction (default: vertical ↓)
What You'll Learn
- Flex Containers — setting up and configuring flex parents
- Flex Grow — how items expand to fill space
- Flex Shrink — how items contract when space is tight
- Flex Wrap — wrapping items to the next line
- Flex Basis — setting the ideal size of items
- Building menus — practical navigation with Flexbox
- Nested Flexbox menus — dropdown sub-menus
- Axis alignment — main axis and cross axis alignment
- Cross axis alignment —
align-itemsandalign-self - Grid vs stacked layout — switching layouts responsively
- Element order — visual reordering without changing HTML
Browser Support
Flexbox is fully supported in all modern browsers — Chrome, Firefox, Safari, Edge, and mobile browsers. It's been stable since 2015 and is completely safe for production use.
Key Takeaways
- Flexbox is a one-dimensional layout system (row or column at a time)
- It replaces float-based layouts with a simpler, more powerful model
- Just
display: flexon a parent makes children flex items in a row - Two axes: main axis (flow direction) and cross axis (perpendicular)
- You'll learn both the theory and practical projects