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:


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:

<div>
  <p>This is a block</p>
  <span>This is inline</span>
</div>

alt text

Pros:

Limitations:

Use normal flow for:


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:

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:

Best use cases:

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-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:

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

Bonus: Firefox has even better Grid inspection tools


Section 6.8: Common Layout Pitfalls

alt text


Summary


Resources


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.