liveBook · Manning

Chapter 4: Flexbox

What you will learn in chapter 4 of CSS in Depth, Second Edition

Introduces flexbox as a way to arrange page elements in rows or columns and address common layout problems.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces flexbox as a way to arrange page elements in rows or columns and address common layout problems.

Key terms: flexbox, Flexible Box Layout, vertical centering, equal-height columns

4.1 Flexbox principles

Introduces flex containers, flex items, and the default layout axes before applying flexbox to a page.

Key terms: display: flex, flex container, flex items, main axis, main start, main end, cross axis, cross start, cross end, display: inline-flex

4.1.1 Building a basic flexbox menu

The section sets up a navigation menu as a flex container and identifies the elements to target when styling it.

Key terms: flex container, flex items, display: flex, site-nav, direct descendant combinators (>), list items, anchor tags

4.1.2 Adding padding and spacing

This section makes a flexbox navigation menu easier to click and uses gap and auto margins to space its items.

Key terms: <a>, <li>, padding, display block, inline, line height, gap, flex items, margin: auto, auto margin

4.2 Flex item sizes

This section shows how two flex columns size themselves before and after applying the flex shorthand, then identifies the three properties that shorthand represents.

Key terms: flex layout, column-main, column-sidebar, flex, main axis, flex-grow, flex-shrink, flex-basis

4.2.1 Flex basis

Explains how flex-basis establishes a flex item’s initial size along the main axis before it may grow or shrink.

Key terms: flex basis, flex-basis, initial main size, main axis, auto

4.2.2 Flex grow

Explains how flex-grow distributes space left after flex bases and spacing, and why the flex shorthand is usually preferable to setting flex-grow alone.

Key terms: flex-basis, flex-grow, remainder, flex container, flex basis, flex: 2, flex: 1, flex shorthand, flex-shrink

4.2.3 Flex shrink

Explains how flex-shrink prevents overflow and shows how it can make percentage-based columns fit alongside a gap.

Key terms: flex-shrink, initial main size, flex container, overflow, flex-basis, gap

4.2.4 Some practical examples

Shows practical column layouts made with flex item sizing, including proportional columns and a fixed-sidebar, fluid-center layout.

Key terms: flex property, flex-grow, flex-basis, fluid columns, Holy Grail layout, sidebars, fluid, equal height

4.3 Flex direction

The section explains how flex-direction changes the direction of flex items and identifies why equal-height columns can still contain tiles of unequal height.

Key terms: flex-direction, flex container, main axis, cross axis, row, column, flex items, column-main, right flex item, tiles

4.3.1 Changing the flex direction

Shows how a nested column flexbox makes two sidebar tiles fill the height established by an outer flexbox, while explaining why vertical flex growth needs a constrained container height.

Key terms: nested flexboxes, flex-direction: column, main axis, cross axis, flex-basis, flex-grow, vertical flexbox, flex-shrink, flex: 1, outer flexbox

4.3.2 Styling the login form

This section explains how CSS styles a login form’s heading, input fields, and button without using flexbox.

Key terms: text-transform, HTML, :not(), attribute selectors, [type=checkbox], [type=radio], exclusion-based approach, display block, inline size, size attribute

4.4 Alignment, spacing, and other details

This section gives a practical order for building a flex layout before using additional alignment and spacing options.

Key terms: flex container, flex items, display: flex, gap, flex-direction, flex values

4.4.1 Understanding flex container properties

This section explains how flex-container properties control wrapping and the spacing and alignment of flex items.

Key terms: flex-wrap, nowrap, wrap, wrap-reverse, flex-flow, flex-direction, justify-content, main axis, flex-start, flex-end

4.4.2 Understanding flex item properties

This section explains how individual flex items can override cross-axis alignment or change visual order, and why visual reordering requires care.

Key terms: align-self, align-items, cross axis, auto, place-self, justify-self, grid layout, order, source order, main axis

4.4.3 Using alignment properties

This section uses flexbox alignment to style a price, explains a visual line-height adjustment, and cautions against using flexbox where normal flow suffices.

Key terms: flex container, flex items, justify-content, align-items, align-self, spans, line-height: 0.7, flex item, em height, descenders

4.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Document flow and the box model Chapter 5: Grid layout →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.