Skip to content
Tutorials Center
My learning

Topics

Lesson 71 of 139 ยท Flexbox

Flex Item Properties

Control individual flex items with flex-grow, flex-shrink, flex-basis and the flex shorthand, and learn how order and align-self change single items.

The container decides the overall direction and alignment, but the items decide how big they are. Should the sidebar stay 240px wide while the main column takes the rest? Should three cards share a row equally? Should the search box shrink before the logo does? Those are questions for the flex item properties.

In this lesson you will learn the three sizing properties (flex-basis, flex-grow and flex-shrink), the flex shorthand that you will actually write day to day, and two properties for breaking ranks: align-self and order.

How Flexbox sizes an item

Flexbox sizes items in three steps along the main axis:

  1. Start from a base size. Each item gets a starting size from its flex-basis.
  2. Measure the difference. The browser adds up all the base sizes plus the gaps and compares the total with the container. The result is either positive free space (room left over) or negative free space (the items are too big).
  3. Share it out. Positive space is handed out according to flex-grow. Negative space is taken away according to flex-shrink.

Each of the three properties controls one step. Let's take them in order.

flex-basis: the starting size

flex-basis sets an item's size along the main axis before any growing or shrinking. In a row it acts like a starting width; in a column, a starting height.

CSS
.item {
  flex-basis: auto;    /* default: use width (or height), else the content size */
  flex-basis: 200px;   /* start at 200px */
  flex-basis: 30%;     /* 30% of the container */
  flex-basis: 0;       /* start from nothing and rely on flex-grow */
  flex-basis: content; /* size from the content, ignoring width */
}

With the default auto, the browser looks at the item's width (in a row). If that is also auto, it uses the item's content size. That's why, in a plain flex row, short items are narrow and long items are wide.

When flex-basis and width are both set, flex-basis wins for the main axis. min-width and max-width still apply, though: they clamp the final size whatever the flex calculation says.

flex-grow: sharing extra space

flex-grow is a number that says how many shares of the positive free space an item receives. The default is 0, so items do not grow.

If the container has 300px of leftover space and three items have flex-grow: 1, 1 and 1, each gets 100px added to its base size. If they have 2, 1 and 1, there are four shares: the first item gets 150px and the others get 75px each.

Growing into free space

All items start at 60px. In the last row, the first item does not end up twice as wide as the others. It gets twice as much of the extra space. Widths only come out in exact proportion when every item starts from flex-basis: 0, which is exactly what the flex: 1 shorthand does, as you will see shortly.

flex-shrink: giving up space

flex-shrink controls what happens when the items are too big for the container. The default is 1, so every item shrinks.

Shrinking is weighted by the base size: a 400px item with flex-shrink: 1 gives up twice as much as a 200px item with flex-shrink: 1. That keeps big items from swallowing small ones. A higher number makes an item shrink faster; 0 stops it from shrinking at all.

CSS
.logo  { flex-shrink: 0; } /* never squash the logo */
.title { flex-shrink: 1; } /* the default */

The automatic minimum size

There is one more rule that surprises everyone sooner or later. Flex items have min-width: auto (or min-height: auto in a column), which in Flexbox means "never shrink below the content's minimum size". For text, that's the longest word. For an image, its width. For a <pre> block or a long URL, the whole line.

So an item can refuse to shrink and overflow the container, even with flex-shrink: 1. The fix is to allow it to get smaller by setting min-width: 0 (or overflow to anything other than visible):

When items refuse to shrink

In the first card, the text block's minimum size is the full unwrapped line, so it bursts out of the card and the ellipsis never appears. In the second, min-width: 0 lets the block shrink to the space available, and text-overflow: ellipsis finally has something to do. The flex-shrink: 0 on the avatar keeps the circle from being squashed into an oval. You will see this pair of fixes in nearly every list or chat interface.

The flex shorthand

In practice you rarely write the three properties separately. The flex shorthand sets all three: flex: <grow> <shrink> <basis>. It also has a few keyword and single-number forms that are worth memorizing:

You write Means Use it for
flex: initial 0 1 auto the default: content size, can shrink, does not grow
flex: auto 1 1 auto grow and shrink from the content size
flex: none 0 0 auto fixed at content size, never grows or shrinks
flex: 1 1 1 0% equal shares of all the space, regardless of content
flex: 2 2 1 0% twice the share of a flex: 1 item
flex: 0 0 200px exact a fixed 200px item

Notice the important detail: a single number sets the basis to zero. That is why flex: 1 on every item gives truly equal columns, while flex: auto gives columns that start from their content size and differ in width.

flex: 1 vs flex: auto

Prefer the shorthand. The specification itself recommends flex over the separate properties, because the shorthand resets the others to sensible values. Writing flex-grow: 1 alone keeps flex-basis: auto, which is often not what you meant.

align-self: one item, different alignment

align-self overrides the container's align-items for a single item. It accepts the same values: stretch, flex-start, flex-end, center and baseline, plus auto (the default, meaning "use the container's value").

CSS
.toolbar {
  display: flex;
  align-items: flex-start;
}
.toolbar .badge {
  align-self: center;
}

There is no justify-self in Flexbox. To push a single item along the main axis, use an auto margin: margin-left: auto on an item absorbs all the free space to its left and pushes it (and everything after it) to the end. That trick appears in almost every navbar; you will use it in Flexbox Patterns.

order: visual reordering

order is an integer, 0 by default. Items are laid out from the lowest order to the highest; items with the same value keep their source order.

Moving one item with order

An order of -1 moves the third item to the front, because -1 sorts before the default 0.

Order is only visual. Screen readers read and the Tab key moves in source order, not in order order. A focused link that jumps around the screen is confusing and fails accessibility guidelines. Use order for small presentational tweaks at certain screen sizes, never to fix markup that is in the wrong order to begin with.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does flex: 1 expand to?

    A single number sets flex-grow and resets the basis to zero, so the item's final size comes entirely from its share of the space.
  2. 2. Three items with the same flex-basis have flex-grow values of 3, 1 and 1. There are 200px of free space. How much does the first item grow?

    There are 3 + 1 + 1 = 5 shares, so each share is 40px. The first item gets three shares, 120px.
  3. 3. A text block in a flex row overflows the container even though it has flex-shrink: 1. What is the usual fix?

    Flex items default to min-width: auto, which stops them shrinking below their content's minimum size. min-width: 0 removes that floor.
  4. 4. How do you push one item to the far end of a flex row while the others stay at the start?

    There is no justify-self in Flexbox. An auto margin soaks up the free space before the item, pushing it to the end. align-self works on the cross axis.
  5. 5. Which statement about order is true?

    order is purely visual. Negative values are allowed, and assistive technology still follows the HTML source.

Summary

  • Flexbox sizes items in three steps: start at flex-basis, measure the free space, then grow with flex-grow or shrink with flex-shrink.
  • flex-grow shares out extra space in proportion to its number; flex-shrink takes space away, weighted by each item's base size.
  • Flex items will not shrink below their content's minimum size unless you set min-width: 0 (or min-height: 0 in a column).
  • Use the flex shorthand: flex: 1 for equal shares, flex: auto for content plus a share, flex: none for fixed items.
  • align-self overrides cross-axis alignment for one item; an auto margin moves one item along the main axis.
  • order changes only the visual order, so keep your HTML in a logical reading order.

Next: Flexbox Patterns, where you put it all together into real components.