Chapter 6: Layout Engines: Flexbox, Grid, and Normal Flow
How browsers position elements, and when to use Flexbox, Grid, or just let it flow
As a front-end developer, mastering layout is one of the most valuable things you can do.
And I don’t mean memorizing Flexbox utilities or copy-pasting Grid templates. I mean truly understanding how the browser lays things out, what tools it offers, and how to choose the right one for what you’re building.
This chapter walks you through:
- The difference between flow, Flexbox, and Grid
- When to use what (and when not to)
- Real examples, debugging tips, and layout gotchas
Section 6.1: Normal Flow, The Browser’s Default
Before CSS layout systems evolved, we had normal flow and honestly, it’s still a solid foundation.
In Normal Flow:
- Block elements stack vertically, taking up the full width of their parent
- Inline elements flow horizontally, wrapping when needed
<div>
<p>This is a block</p>
<span>This is inline</span>
</div>
Pros:
- Simple, predictable
- Perfect for text content and long-form documents
Limitations:
- No control over alignment
- Can’t place things side by side without floating or hacking
Use normal flow for:
- Text-heavy content
- Long articles
- Default layouts before enhancement
Section 6.2: Flexbox: One Axis to Rule Them All
Flexbox is a one-dimensional layout system. You pick either a row or a column to control.
.container {
display: flex;
}
Main axis vs cross axis:
flex-direction: row
→ main axis is horizontalflex-direction: column
→ main axis is vertical
Use justify-content
to control alignment along the main axis
Use align-items
for the cross axis
Common properties:
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 1rem;
For children:
flex-grow
: allow to expandflex-shrink
: allow to shrinkflex-basis
: default sizeflex
: shorthand for all three
Best use cases:
- Navbars
- Cards in a row
- Sidebar + content split
- Vertical centering
Flexbox makes a lot of old layout hacks (like floating divs) obsolete.
Section 6.3: Grid: When You Care About Structure
Grid is two-dimensional: it gives you full control over rows and columns.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 1rem;
}
Each 1fr
means “take equal part of the available space”.
You can mix fixed and flexible units:
grid-template-columns: 200px 1fr;
Now column 1 is fixed, column 2 fills the rest.
Powerful grid features:
grid-template-areas
: name your layoutgrid-auto-flow
: row vs column directionauto-fit
andauto-fill
: responsive layout magic
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
This builds a responsive grid that wraps automatically.
Section 6.4: Which Layout Tool Should You Use?
Scenario | Use |
---|---|
Aligning items in a row | Flexbox |
Equal height cards in a row | Flexbox |
Sidebar + content | Grid |
Full-page layout | Grid |
Simple list of items | Normal Flow |
Navbar with spacing | Flexbox |
Product grid with wrapping | Grid |
The core rule:
- Flexbox is for distribution along one axis
- Grid is for full-blown layout with rows + columns
- Normal flow is the fallback, useful until layout is needed
You can (and should) mix them.
Section 6.5: Nesting Flex and Grid
Example:
.main-layout {
display: grid;
grid-template-columns: 250px 1fr;
height: 100vh;
}
.sidebar {
background: #f4f4f4;
}
.content {
display: flex;
flex-direction: column;
gap: 1rem;
}
Here, Grid lays out the overall page. Flexbox controls content spacing inside.
This is a common real-world combo.
Section 6.6: Responsive Techniques
Grid with auto-fit:
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
Items fill space and wrap automatically.
Flexbox with wrapping:
display: flex;
flex-wrap: wrap;
gap: 1rem;
Each item should have a defined width or flex-basis
.
Use media queries or container queries to adjust breakpoints.
Section 6.7: Debugging Layout with DevTools
- Right-click → Inspect
- In Chrome or Firefox, enable Grid or Flex overlays
- Chrome DevTools shows:
- Flex direction arrows
- Item alignment
- Gaps and track sizes
Bonus: Firefox has even better Grid inspection tools
Section 6.8: Common Layout Pitfalls
- Using Flexbox to fake a grid → use
grid
instead - Forgetting to use
gap
→ don’t space withmargin
flex: 1
doesn’t mean “fill all space” unless other items allow it- Nested
display: flex
for everything → hurts readability - Relying too much on media queries → try
auto-fit
first
Summary
- Normal flow is still useful for content, don’t overengineer
- Flexbox is perfect for rows or columns especially when you don’t need track-based layout
- Grid is best for full-page structures, cards, dashboards, and any 2D layout
- Combine layout models wisely
- Use DevTools to inspect, always debug visually
Resources
- MDN Web Docs: Flexbox
- MDN Web Docs: Grid
- CSS Tricks: A Complete Guide to Flexbox
- CSS Tricks: A Complete Guide to Grid
Coming Up Next
In Chapter 7, we’ll cover modern CSS selectors like :is()
, :has()
, :focus-visible
, and :where()
and how they let you write smarter, more accessible, and more maintainable CSS.