liveBook · Manning

Flex

As explained in CSS in Depth, Second Edition

The CSS flex property is a shorthand that controls a flex item's growth, shrinkage, and base size along the flex container's main axis.

The CSS flex property controls a flex item’s size along its container’s main axis, normally its width. It combines flex-grow, flex-shrink, and flex-basis in one declaration. section 4.2 section 4.5

For proportional columns, flex: 2 and flex: 1 give the columns flex bases of 0%, then distribute the available remainder in a two-to-one ratio. Gaps and margins reduce that remainder. section 4.2 section 4.2.2

Two-column page layout

Apply display: flex to the container, then assign relative weights to its children: .flex { display: flex; gap: var(--gap-size); } .column-main { flex: 2; } .column-sidebar { flex: 1; } The main column becomes twice as wide as the sidebar, and both grow to fill the available container width. section 4.2 section 4.2.2

Use flex to express several sizing patterns:

  • Give items different proportions by assigning different flex-grow weights. section 4.2.2
  • Combine fixed-width side columns with a fluid center column. section 4.2.4
  • Build reusable grid systems with preset column classes. section 4.2.4
Listing 4.7 Using the flex property to set column widths
.column-main {
  flex: 2;
}

.column-sidebar {
  flex: 1;
}

Inspect the two-column CSS listing showing how flex weights produce proportional main and sidebar columns. section 4.2

How CSS in Depth, Second Edition covers it

How deep it goes
Explains shorthand components, default values, remainder distribution, and proportional growth through a two-column example.
Examples
Uses a primary-content and sidebar layout, proportional columns, fixed and fluid columns, grid systems, and a Holy Grail layout.
What it assumes
Assumes familiarity with CSS width and height while introducing flex containers, flex items, and shorthand sizing from the layout example.
What you can do after
Build it from scratch by assigning proportional, fixed, or fluid flex-item sizes within a flex container.
How it connects
Connects flex items with flex containers, flex-grow, flex-shrink, flex-basis, gaps, margins, and nested flexbox layouts.

Also explained in

Compare how 3 books explain flex →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.