liveBook · Manning

Flex item

As explained in Web Design Playground, Second Edition

A flex item is a child element of a flex container that participates in flexible sizing and alignment along the container’s axes.

A flex item is a child of a flex container. Give a parent `display: flex`, and its children become flex items automatically—no special declaration needed on each child. A flex container can arrange them in a row or column. section 12.1 section 12.2

Think of the container as setting the group rules. Its main axis runs in the direction of the items; its cross axis runs across that direction. Container properties can position the group along either axis and let items wrap onto additional lines. section 12.2

Then ask what each item needs to do:

  • Fill spare room? Use `flex-grow`. It starts at `0`; positive values divide extra space among growing items. Values of `1`, `2`, and `1` split that space 25%, 50%, and 25%. section 12.4
  • Running short on room? Use `flex-shrink`. Want to suggest a starting size? Use `flex-basis`. The `flex` shorthand can combine growth, shrinkage, and basis. section 12.4
  • Need one item in a different position or alignment? Use `order` to change its order or `align-self` to override the container’s `align-items` setting. section 12.4

Center one item

Put an `h1` inside a `div`. Make the `div` a flex container, set both `justify-content` and `align-items` to `center`, and give it `height: 100vh`. The `h1` becomes the flex item and sits in the center of the browser window. No positioning gymnastics required. section 12.3

main {
    display: flex;         #1
    flex-grow: 1;          #2
}
article {
    flex-grow: 3;          #3
    border: 1px solid black;
}
aside {
    flex-grow: 1;          #4
    border: 1px solid black;
}

This CSS makes `main` a flex container for `article` and `aside`, while `main` itself grows as an item in the body container. The `article` and `aside` use growth values of `3` and `1` to share available horizontal space. section 12.6

How Web Design Playground, Second Edition covers it

How deep it goes
Defines automatic item creation, then explains growth with proportions and introduces shrinkage, basis, ordering, alignment, and shorthand.
Examples
Shows a heading centered in a browser window and a two-column article-and-sidebar layout with proportional growth.
What it assumes
Starts with parent-and-child HTML elements and introduces flex containers and axes before item-level controls.
What you can do after
Build it from scratch: center a flex item or arrange and proportion two columns using CSS Flexbox.
How it connects
Connects flex items to flex containers, main and cross axes, wrapping, and the `flex` sizing shorthand.

Also explained in

Compare how 3 books explain flex item →

Related concepts

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