OSCSS.004: CSS Flexbox — Main and Cross Axes, Alignment, Wrapping and Flexible Sizing

Three flex items with equal 160 pixel bases and extra space allocated in a one to two to one ratio

CSS Flexbox distributes available space between elements along one primary dimension. Following OSCSS.003 on responsive units, learners now apply sizing rules to navigation, toolbars and adaptive layouts. Prerequisites: basic HTML structure, CSS selectors and the CSS cascade. A browser, plain-text editor and developer tools are sufficient; no installation or production-site edits are required.

Learning objectives

By completion, learners can: (1) distinguish flex containers, direct flex items and main/cross axes; (2) control alignment, gaps and wrapping; (3) calculate proportional growth and shrinking; (4) implement a responsive component and diagnose overflow without disrupting keyboard navigation.

1. Containers and axes

Setting display: flex; on a parent establishes a flex formatting context for its direct, in-flow children. Descendants do not automatically become flex items. In the normal horizontal writing mode, flex-direction: row; arranges items along the horizontal inline axis; column changes the main axis to vertical. The cross axis is perpendicular to the main axis. Writing direction affects which edge is considered the start. Unlike CSS Grid, Flexbox handles one primary dimension at a time, even when items wrap.

Kevin Powell: Learn flexbox the easy way. Watch at source.

2. Alignment, spacing and wrapping

justify-content: space-between; distributes remaining space along the main axis. align-items: center; centers items on the cross axis; the default stretch behavior generally fills the available cross-axis size when that size is automatic. align-self can override alignment for one item. gap: 16px; creates consistent internal spacing without adding outer margins. The default flex-wrap: nowrap; keeps items on one line; flex-wrap: wrap; permits new lines. align-content distributes flex lines when wrapping is enabled and extra cross-axis space exists. After changing the direction to column, remember that justification follows the vertical main axis rather than the horizontal screen edge.

3. Growth, shrinkage and basis

The shorthand flex: 1 1 160px; sets the grow factor, shrink factor and basis respectively. Basis supplies a starting main-axis size before free space distribution. Grow factors determine shares of positive free space; they do not necessarily define ratios of total widths. Shrink factors are weighted by base sizes. Minimum and maximum size constraints can change the final distribution.

Example: an 800 px row contains three items with 160 px bases and two 16 px gaps. Free space = 800 − 480 − 32 = 288 px. Grow factors of 1, 2 and 1 allocate 72, 144 and 72 px. Final widths are 232, 304 and 232 px, assuming no padding, borders or limiting constraints.

Final item sizes 232, 304 and 232 pixels after distributing positive free space
Original instructional chart showing the 160 px bases and proportional growth shares.

For shrinkage, suppose two items have bases of 200 px and 100 px with equal shrink factors of 1. A 240 px container without gaps leaves a 60 px deficit. The scaled weights are 200 and 100, so the items lose 40 and 20 px respectively, resulting in 160 and 80 px if minimum sizes permit. See MDN’s flex sizing reference for the underlying rules.

4. Practical browser lab: responsive resource list

Create a local HTML file containing a parent element with class resources and three child elements with class resource. Give each child a heading and a short description. Add a style element in the document head. For the parent, enter .resources { display: flex; flex-wrap: wrap; gap: 16px; align-items: stretch; }. For the children, enter .resource { flex: 1 1 160px; min-width: 0; overflow-wrap: anywhere; }. Open the file in a browser and resize the viewport. The items should share available width and wrap when their combined bases and gaps no longer fit. Change the second child’s growth factor to 2 using .resource:nth-child(2) { flex-grow: 2; } and inspect the resulting widths. Growth is recalculated independently on each flex line.

Use the browser’s flex inspector to check computed sizes. If long text causes overflow, confirm that the item’s automatic minimum size is not preventing shrinkage, then test min-width: 0 in a row layout and appropriate text wrapping. Use responsive image techniques from OSHTML.007 when adding images. Keep reading and keyboard focus order consistent with the HTML source; do not use order or reversed directions to repair incorrect semantic ordering.

Kevin Powell: How to take control of Flexbox. Watch at source.

Practice and answer key

Exercise 1: In a column-direction container, which property centers items horizontally? Answer: align-items: center in the assumed horizontal writing mode.

Exercise 2: Two 100 px items have grow factors 1 and 3. A 420 px row has one 20 px gap. Find final widths. Answer: 200 px remains, so the items receive 50 and 150 px, ending at 150 and 250 px.

Exercise 3: Explain why a long URL may overflow a shrinking item and propose a correction. Answer: the automatic minimum size can constrain shrinking; inspect the computed width, allow a zero minimum where appropriate and permit long text to wrap.

Exercise 4: Verify that resizing the local resource list preserves visual usability and keyboard navigation. Answer: items wrap without horizontal scrolling; headings and links remain readable, and keyboard focus follows source order. Review the CSS cascade lesson if declarations are unexpectedly overridden. Further reading: MDN Flexbox tutorial.

Leave a Reply