liveBook · Manning

Flexbox

As explained in Tiny CSS Projects

Flexbox is a CSS layout model for aligning and distributing items along a single axis.

Flexbox arranges child elements along one axis: horizontally or vertically. Put `display: flex` on their parent, then choose how the children align and how space is distributed. It is a practical choice for centering a card or lining up items in a row. CSS Grid is another layout option when a page needs regions arranged in both rows and columns. section 6.4 section 2.1

Center a profile card

To center a card in the browser window, make its parent, `body`, a flex container: `body { display: flex; justify-content: center; align-items: center; }`. The first alignment declaration centers the card horizontally; the second centers it vertically. Apply these declarations to the parent, not the card. section 6.4

Once a container uses Flexbox, a few properties handle common layout jobs:

  • `flex-direction` chooses the axis. `justify-content` distributes items and leftover space along it; `align-items` controls their alignment relative to one another and the container. section 6.9
  • `gap` sets space between items without adding space at the container’s outer edges. For profile statistics, `justify-content: space-around` spreads three groups across a row while `gap: 12px` preserves a minimum separation. section 6.7.2
  • `flex-basis` supplies a starting size; `flex-grow` lets an item use leftover space. `flex-shrink` controls whether and how an item can shrink. section 8.5 section 6.9

Even spacing does not guarantee that the middle item sits at the container’s exact center: items with different widths affect where leftover space falls. Browser-default margins can also make aligned content look offset. Check both before adjusting the layout. section 6.7.2

Move checkout sections side by side

For a wider checkout screen, a media query changes the cart-and-summary container to `display: flex` and adds a `20px` gap. The summary gets `flex-basis: 250px`; the cart gets `flex-grow: 1` so it can take the remaining room. A maximum width on the page prevents the layout from stretching too far on very wide screens. section 8.5

Listing 8.29 Placing the two cards side by side on wide screens
@media (min-width: 955px) {
  .section-container {
    display: flex;
    gap: 20px;
  }
  section.my-cart { flex-grow: 1; }
  section.summary { flex-basis: 250px }
}

See the media query and flex sizing declarations together in the checkout layout. section 8.5

Use Flexbox for smaller alignments, too: place a mobile cart footer’s label and total at opposite ends of one line, align form labels with controls, or center an icon inside a share link. It need not replace simpler CSS—right-aligned text alone does not require a flex container. section 8.3.1 section 9.2.3 section 10.5 section 11.5.3

How Tiny CSS Projects covers it

How deep it goes
Works through container alignment, spacing, and item sizing with CSS declarations, without descending into browser layout internals.
Examples
Uses a centered profile card, spaced statistics, a wide-screen checkout cart, and aligned credit-card details.
What it assumes
Starts with existing HTML and CSS rules, then explains `display: flex` and alignment declarations as they are applied.
What you can do after
Operate/configure it to center elements, distribute row contents, and size adjacent sections with CSS Flexbox properties.
How it connects
Contrasts Flexbox with CSS Grid and float, and ties it to media queries and responsive layout.

Also explained in

Compare how 7 books explain flexbox →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.