# Flex Items: Sizing & Order

> Give individual items superpowers. Learn how to make an item consume extra space, stubbornly refuse to shrink, or visually swap places.

Subject: CSS | Lesson 22 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/flex-item-sizing
Previous: [Flex Alignment](https://codewithanimation.in/css/flex-alignment.md) | Next: [Grid: Columns, Rows & Placement](https://codewithanimation.in/css/grid-fundamentals.md)

## What it is

While properties like `justify-content` apply to the **Parent Container**, Flexbox also provides properties that apply directly to the **Children** (the Flex Items).

These properties—`flex-grow`, `flex-shrink`, `flex-basis`, and `order`—allow individual items to negotiate how much space they consume along the Main Axis.

## Why it matters

Imagine a navigation bar with a Logo on the left, a Profile Picture on the right, and a Search Input in the middle. How do you make the Search Input stretch to fill all the available space between them? You give it `flex-grow: 1`.

## How it works

1. **flex-basis**: The *ideal* starting size of the item along the Main Axis before any extra space is distributed or removed. Think of it as a flexible `width` (in a row).

2. **flex-grow**: If there is free space left over in the container, items with a grow value > 0 will stretch to consume it. A value of `1` means 'take 1 share of the extra space'.

3. **flex-shrink**: If there is not enough space, items with a shrink value > 0 will squish. By default, all items have `flex-shrink: 1`.

4. **order**: Visually rearranges items without changing the HTML markup. The default order is 0.

## Common mistake: The 'flex' Shorthand Trap

Developers rarely write out all three sizing properties. Instead, they use the shorthand: `flex: [grow] [shrink] [basis]` (e.g., `flex: 1 0 auto`).

The trap? If you write just `flex: 1`, Flexbox implicitly sets it to `flex: 1 1 0%`. By setting the basis to 0%, the item completely ignores its content size and shares space purely mathematically. This is often what you want, but it can cause unexpected squishing if you aren't aware of it!

## Tip: Accessibility Warning with Order

Be extremely careful using the `order` property to visually rearrange items. Screen readers and keyboard navigation (Tab key) strictly follow the original HTML source code, **not** your CSS visual order. If you drastically reorder elements with CSS, you will create a chaotic and confusing experience for users navigating via keyboard.

## Try it: Try it

Select a specific item and adjust its grow, shrink, basis, and order properties. Notice how a single item's flex properties influence the entire layout.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/flex-item-sizing

## Check yourself

### Question 1

If you want a search bar to expand and fill all available horizontal space between a logo and a profile picture, what property do you apply to the search bar?

- A. flex-grow: 1
- B. width: 100%
- C. justify-content: stretch

Correct answer: A

By applying `flex-grow: 1` to the search bar, it tells Flexbox to give all the leftover free space to that specific item.

- Why not B: width: 100% will try to make the search bar as wide as the entire parent, causing overflow or aggressive squishing of the other items.
- Why not C: justify-content goes on the parent and does not have a 'stretch' value.

### Question 2

What is `flex-basis`?

- A. The ideal starting size of an item along the main axis before growing or shrinking occurs
- B. The minimum height of the flex container
- C. A property that forces items to wrap to a new line

Correct answer: A

Flex-basis defines the item's baseline size. From there, flex-grow adds space, and flex-shrink removes space.

- Why not B: It applies to the child item, not the container.
- Why not C: That is flex-wrap, which applies to the parent.

### Question 3

By default, flex items have `flex-shrink: 1`. What does this mean?

- A. They will squish to fit inside their container if space runs out
- B. They will automatically wrap to the next line
- C. They will overflow the container

Correct answer: A

Because flex-shrink is 1 by default, flex items will gracefully compress themselves rather than breaking out of the container when space gets tight.

- Why not B: Wrapping is controlled by flex-wrap on the parent.
- Why not C: Because flex-shrink defaults to 1, they actively try NOT to overflow by squishing.

## Remember this

- `flex-grow: 1` lets an item consume leftover space.
- `flex-shrink: 0` stubbornly prevents an item from squishing.
- `flex-basis` is the item's ideal starting size.
- `order` visually swaps items, but breaks keyboard navigation flow.
