Chapter 4: Flexbox
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
- Flexbox layout purpose. Explain what flexbox is primarily used for and name the layout problems the introduction says it can help solve.
- Flex containers and items. Explain what applying display: flex does to an element and its direct children, and how this differs from display values that affect only the element itself.
- Default flex layout and axes. Describe the default arrangement of flex items and identify the main and cross axes, including their default start and end directions.
- Menu flex container and items. Explain why the menu’s unordered list is the flex container and its list items are the flex items, and describe applying display: flex to the list.
- Clickable menu link padding. Explain why the menu item padding belongs on the internal <a> elements rather than the <li> elements.
- Flex item gap spacing. Describe how gap adds space between the menu’s flex items.
- Auto margin item positioning. Explain how an auto margin pushes a menu item right, and what changes when it is applied to an earlier item.
- Flex shorthand and column proportions. Explain what flex controls in this example, how it changes the two columns, and which sizing properties its shorthand represents.
- Flex basis and initial size. Explain how flex-basis sets a flex item’s initial main size, how auto uses a declared width or the content’s natural size, and why a non-auto flex basis overrides width.
- Remaining space and growth. Explain how flex-grow uses the space left after flex bases and gaps or margins are accounted for, including what happens when a value is zero or nonzero.
- Proportional growth shares. Describe how different flex-grow values divide the remainder, using the section’s two-column example.
- Flex item shrink behavior. Explain when flex items shrink and how their flex-shrink values affect the amount of shrinking.
- Flexible column sizing patterns. Describe how flex sizing can produce proportional columns, fixed-width columns, and fluid columns that scale with the viewport.
- Flex direction and axes. Explain how setting flex-direction on a flex container changes the direction in which its items flow, including the row, column, and reverse options.
- Equal columns versus inner tiles. Describe why the right-hand tiles do not reach the bottom of a taller main tile, despite the outer flex items being equal height, and what the desired layout would look like.
- Column direction and nested flexboxes. Explain how the sidebar can be both an item in an outer flexbox and a container for an inner flexbox, and how setting the inner flex direction to column changes the axis on which its items grow.
- Vertical growth needs constrained height. Explain why flex-grow on the sidebar tiles can fill vertical space in this example, but would not do so in a column flex container whose height is determined only by its contents.
- Exclusion-based input selection. Describe how the form styles relevant inputs without applying the same rules to checkboxes and radio buttons, and why it uses that approach.
- Block input sizing. Explain why the input styles set both display: block and an inline size of 100%.
- Basic flexbox workflow. Describe the section’s suggested order for building a flex layout: identify the container and items, apply display: flex to the container, set gap or flex-direction if needed, and use flex values to control item sizes. Explain that other flexbox properties can be added after the elements are roughly in place.
- Flex wrapping and flow. Explain how flex-wrap moves overflowing items to a new row or column, and describe what flex-flow combines.
- Main-axis spacing choices. Describe how justify-content positions items along the main axis and distinguish its three space-distribution values.
- Wrapped-line cross-axis spacing. Describe what align-content spaces when wrapping is enabled, and state what place-content combines.
- Individual item cross-axis alignment. Explain how align-items controls flex items along the cross axis, contrasting its default stretch behavior with natural-sized alignment.
- Individual cross-axis alignment. Explain how align-self changes one flex item’s cross-axis alignment relative to the container’s align-items setting.
- Flex item visual order. Explain how order determines the visual sequence of flex items, including default values and ties.
- Price Tile Flex Alignment. Describe how the $20.00 price is divided into flex items and which properties center and vertically align them. Explain why spans were chosen for the price parts.
What it builds on
- Normal document flow (chapter 3) · Introduces normal document flow as the browser’s default layout.
- Element height (chapter 3) · the distinction between an element’s content and its height
- Unitless numbers and line-height (chapter 2) · Unitless line-height values
Section by section
Introduction
Introduces flexbox as a way to arrange page elements in rows or columns and address common layout problems.
- Flexbox layout purpose: Explain what flexbox is primarily used for and name the layout problems the introduction says it can help solve.
Why it matters: This gives you a reason to consider flexbox before learning its individual properties.
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.
- Flex containers and items: Explain what applying display: flex does to an element and its direct children, and how this differs from display values that affect only the element itself.
Why it matters: This parent-and-child relationship is the starting point for using flexbox. - Default flex layout and axes: Describe the default arrangement of flex items and identify the main and cross axes, including their default start and end directions.
Why it matters: The axes give you a way to describe where items go before changing their layout. - Inline flex container behavior: Distinguish display: inline-flex from display: flex in how the container participates in the surrounding page, while describing how their items behave.
Why it matters: It separates the container’s behavior among surrounding elements from the flex layout inside it.
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.
- Menu flex container and items: Explain why the menu’s unordered list is the flex container and its list items are the flex items, and describe applying display: flex to the list.
Why it matters: Choosing the right container determines which elements flexbox lays out. - Menu element targeting: Distinguish the list, list items, and links as three levels of the menu, and explain why the styles use direct descendant combinators.
Why it matters: Keeping the element levels distinct helps menu styles target the intended elements.
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.
- Clickable menu link padding: Explain why the menu item padding belongs on the internal <a> elements rather than the <li> elements.
Why it matters: Padding the link prevents a menu item from looking clickable beyond its actual click target. - Block link height contribution: Explain why the padded menu links are made display blocks instead of remaining inline.
Why it matters: The display choice lets the padded links give the menu the intended height. - Flex item gap spacing: Describe how gap adds space between the menu’s flex items.
Why it matters: Gap provides a direct way to separate the menu items. - Auto margin item positioning: Explain how an auto margin pushes a menu item right, and what changes when it is applied to an earlier item.
Why it matters: The placement of the auto margin determines which menu items move to the far side.
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.
- Natural flex column sizing: Describe how the main area’s two columns size themselves after flex layout is applied but before their widths are specified.
Why it matters: This gives you a baseline for seeing what the flex property changes. - Flex shorthand and column proportions: Explain what flex controls in this example, how it changes the two columns, and which sizing properties its shorthand represents.
Why it matters: It explains both the visible change in the example and how to read the shorthand used to produce it.
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.
- Flex basis and initial size: Explain how flex-basis sets a flex item’s initial main size, how auto uses a declared width or the content’s natural size, and why a non-auto flex basis overrides width.
Why it matters: It helps explain why a flex item may not end up at its declared width or its flex-basis size.
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.
- Remaining space and growth: Explain how flex-grow uses the space left after flex bases and gaps or margins are accounted for, including what happens when a value is zero or nonzero.
Why it matters: This explains why flex items can expand to fill their container. - Proportional growth shares: Describe how different flex-grow values divide the remainder, using the section’s two-column example.
Why it matters: Relative grow values let you predict how much of the available width each column receives. - Flex shorthand defaults: Justify using the flex shorthand instead of declaring only flex-grow, stating the values the shorthand supplies when omitted.
Why it matters: Those shorthand defaults help produce the sizing behavior used in the column example.
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.
- Flex item shrink behavior: Explain when flex items shrink and how their flex-shrink values affect the amount of shrinking.
Why it matters: It explains why columns can fit even when their initial sizes exceed the container. - Percentage columns with a gap: Describe how the section uses flex-shrink to fit two percentage-based columns alongside a gap.
Why it matters: The example turns the shrink rule into a way to size columns without letting their gap cause overflow.
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.
- Flexible column sizing patterns: Describe how flex sizing can produce proportional columns, fixed-width columns, and fluid columns that scale with the viewport.
Why it matters: These sizing patterns let you choose how each column responds to available space. - Holy Grail layout example: Describe the three-column “Holy Grail” example, including which columns are fixed or fluid and what happens to their heights.
Why it matters: It makes the fixed-width and fluid-column pattern concrete.
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.
- Flex direction and axes: Explain how setting flex-direction on a flex container changes the direction in which its items flow, including the row, column, and reverse options.
Why it matters: Changing direction lets a flex container arrange items in a row or a vertical stack. - Equal columns versus inner tiles: Describe why the right-hand tiles do not reach the bottom of a taller main tile, despite the outer flex items being equal height, and what the desired layout would look like.
Why it matters: It distinguishes a problem with the tiles inside a column from a problem with the column heights themselves.
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.
- Column direction and nested flexboxes: Explain how the sidebar can be both an item in an outer flexbox and a container for an inner flexbox, and how setting the inner flex direction to column changes the axis on which its items grow.
Why it matters: The axis change explains why flex sizing can distribute vertical space between the sidebar tiles. - Vertical growth needs constrained height: Explain why flex-grow on the sidebar tiles can fill vertical space in this example, but would not do so in a column flex container whose height is determined only by its contents.
Why it matters: It prevents the mistaken expectation that flex-grow alone will make a column flexbox taller.
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.
- Presentation-only heading capitalization: Explain why the heading’s uppercase appearance is applied with CSS rather than written in uppercase in the HTML.
Why it matters: Keeping presentation in CSS lets you change the heading’s appearance without rewriting its HTML text. - Exclusion-based input selection: Describe how the form styles relevant inputs without applying the same rules to checkboxes and radio buttons, and why it uses that approach.
Why it matters: A selector that accounts for other input types is less likely to style a future checkbox or radio button like a text field. - Block input sizing: Explain why the input styles set both display: block and an inline size of 100%.
Why it matters: Making an input block-level does not by itself give it the intended full width. - Clickable button cursor: Describe what cursor: pointer does for the Login button and what it communicates.
Why it matters: The cursor provides a small visual cue that the button is interactive.
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.
- Basic flexbox workflow: Describe the section’s suggested order for building a flex layout: identify the container and items, apply display: flex to the container, set gap or flex-direction if needed, and use flex values to control item sizes. Explain that other flexbox properties can be added after the elements are roughly in place.
Why it matters: This order gives you a manageable starting point without memorizing every flexbox option.
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.
- Flex wrapping and flow: Explain how flex-wrap moves overflowing items to a new row or column, and describe what flex-flow combines.
Why it matters: Wrapping changes how a flex layout handles items that cannot fit on one line. - Main-axis spacing choices: Describe how justify-content positions items along the main axis and distinguish its three space-distribution values.
Why it matters: These values let you choose where spare space appears in a flex row or column. - Limits of content justification: Explain why justify-content may have no visible effect when flex growth or an auto main-axis margin is present.
Why it matters: It helps explain why changing justify-content sometimes does not change a layout. - Wrapped-line cross-axis spacing: Describe what align-content spaces when wrapping is enabled, and state what place-content combines.
Why it matters: It distinguishes spacing wrapped lines from spacing the items within a line. - Individual item cross-axis alignment: Explain how align-items controls flex items along the cross axis, contrasting its default stretch behavior with natural-sized alignment.
Why it matters: This explains both equal-height flex items and how to stop items from stretching across the container. - Alternative item alignment values: Distinguish positional, logical, and baseline align-items values using the examples given in the section.
Why it matters: These choices let items line up by position or by their text rather than all stretching to the same size.
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.
- Individual cross-axis alignment: Explain how align-self changes one flex item’s cross-axis alignment relative to the container’s align-items setting.
Why it matters: It lets you align one item differently without changing the rest of the flex container. - Place-self flexbox limitation: Describe what place-self abbreviates and which part has no effect in flexbox.
Why it matters: It prevents you from expecting place-self to control both axes in flexbox. - Flex item visual order: Explain how order determines the visual sequence of flex items, including default values and ties.
Why it matters: It explains how to rearrange the displayed sequence without changing the HTML. - Reordering accessibility risk: Justify why a flex layout whose visual order differs greatly from source order can be confusing.
Why it matters: A visually convenient rearrangement can create a confusing experience for keyboard and screen-reader users.
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.
- Price Tile Flex Alignment: Describe how the $20.00 price is divided into flex items and which properties center and vertically align them. Explain why spans were chosen for the price parts.
Why it matters: It connects the price’s markup to the alignment each flexbox property provides. - Visual Line Height Adjustment: Explain why the price styling uses line-height: 0.7 and how that value was chosen.
Why it matters: It explains why a numeric line-height adjustment affects the price’s visible alignment. - Selective Flexbox Use: Justify the section’s advice to keep normal document flow unless a part of the page needs flexbox.
Why it matters: It keeps a useful layout tool from becoming the default for every element.
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
- (chapter intro) (chapter 5) · The preceding chapter introduces flexbox as a layout tool.
- Anatomy of a grid (chapter 5) · Provides the earlier page built using flexbox.
- Building a basic grid (chapter 5) · The analogous container-and-items relationship introduced for flexbox.
- Working together with flexbox (chapter 5) · Flex item sizes can respond to available space and content.
- Adding responsive columns (chapter 7) · Understanding flex-basis.
- Fluid layouts (chapter 7) · The earlier discussion of flex shrink provides context for allowing flex items to fit when space decreases.
- Adjusting grid items to fill the grid track (chapter 5) · The effect of a column flex direction.
- Using ems vs. px (chapter 12) · Covers spacing in flex layouts.
Concepts explained in this chapter
- flex itemA flex item is a child of a flex container that participates in flexible one-dimensional layout.
- flex containerA flex container is an element whose children participate in layout using the CSS flexible box formatting context.
- cross axisThe cross axis is the layout direction perpendicular to a flex container's main axis.
- main axisThe main axis is the primary direction in which a flex container lays out and distributes its flex items.
- flex-basisThe CSS flex-basis property specifies a flex item’s initial size along the flex container’s main axis before free-space distribution.
- flexboxFlex is a CSS display value that makes an element a flex container for its children.
- shrinkThe CSS flex-shrink property specifies a flex item's shrink factor when the flex items' initial sizes exceed the available space in the flex container.
- align-itemsThe CSS align-items property specifies the default cross-axis alignment of items within a flex or grid container.
- flex-growThe CSS flex-grow property specifies a nonnegative factor that determines how a flex item receives remaining space in its flex container.
- justify-contentThe CSS justify-content property distributes grid tracks or flex items along the container's inline or main axis when free space is available.
- flexThe CSS flex property is a shorthand that controls a flex item's growth, shrinkage, and base size along the flex container's main axis.
- flex-directionThe CSS flex-direction property specifies the main-axis direction and ordering of flex items in a flex container.
- align-contentThe CSS align-content property distributes grid tracks or flex lines along the container's block or cross axis when free space is available.
- flex-flowThe CSS flex-flow property is a shorthand that sets a flex container's flex-direction and flex-wrap properties.
- flex-wrapThe CSS flex-wrap property specifies whether flex items remain on one line or wrap onto additional lines or columns.
- align-selfThe CSS align-self property specifies the block-axis or cross-axis alignment of an individual item within its layout container.
- gapThe CSS gap property specifies the spacing between rows and columns in layout containers such as flex and grid containers.
- block-level elementA block-level element generates block-level content that starts on its own line and normally fills the available width of its container.
- buttonAn HTML button is an interactive form control that can be activated to perform an action.
- inline-sizeThe CSS inline-size property specifies an element's size along its inline axis, adapting to the writing mode.
- paddingThe CSS shorthand property that specifies the space between an element's content and its border.
- gridA CSS layout system that arranges elements in rows and columns using a two-dimensional grid.
- place-contentThe CSS place-content shorthand property sets the align-content and justify-content properties together.
- text-transformThe text-transform property controls the capitalization and letter-case transformation of text.
- normal flowNormal flow is the CSS layout model that positions elements according to their order and formatting contexts without out-of-flow positioning.
- shorthand propertyA CSS property syntax that sets multiple related properties, including omitted subvalues that receive their initial values.
- line wrappingLine wrapping is the flow behavior in which text or an inline-level element moves onto a subsequent line when it exceeds the available inline space.
- spacingDesign spacing is the arrangement of empty space between and around interface elements.
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.
