flex-grow

What it does

Controls how much an item grows compared to others when there is extra space.

Key Idea

  • Default = 0 โ†’ item does NOT grow
  • Bigger number = takes more space

Example 1 (Equal growth)

.item {
  flex-grow: 1;
}

๐Ÿ‘‰ All items grow equally

Example:

  • 3 items โ†’ each gets equal width


Example 2 (Different growth)

.item:nth-child(1) {
  flex-grow: 2;
}

๐Ÿ‘‰ First item gets double space

Example:

  • Item 1 โ†’ 2 parts
  • Others โ†’ 1 part each


flex-shrink

What it does

Controls how items shrink when there is not enough space

Key Idea

  • Default = 1 โ†’ items can shrink
  • 0 โ†’ item will NOT shrink

Example

.item {
  flex-shrink: 0;
}

๐Ÿ‘‰ Items keep their size โ†’ may overflow

๐Ÿ”ฅ Real Use Case

  • Prevent logo or button from shrinking
  • Keep important UI fixed size

flex-basis

๐Ÿ‘‰ What it does

Sets the initial size of an item before growing/shrinking

๐Ÿง  Key Idea

  • Works like width (in row)
  • Works like height (in column)

๐Ÿงช Example

.item {
  flex-basis: 200px;
}

๐Ÿ‘‰ Each item starts at 200px

โš ๏ธ Important

flex-basis overrides width

.item {
  width: 100px;
  flex-basis: 200px;
}

๐Ÿ‘‰ Final size = 200px


flex (MOST IMPORTANT)

๐Ÿ‘‰ What it does

Shortcut for:

flex: grow shrink basis;

๐Ÿงช Example

.item {
  flex: 1 1 200px;
}

๐Ÿ‘‰ Means:

  • grow = 1 โ†’ can expand
  • shrink = 1 โ†’ can shrink
  • basis = 200px โ†’ starting size

Responsive cards (BEST USE)

.item {
  flex: 1 1 250px;
}

๐Ÿ‘‰ Cards:

  • Try to be 250px
  • Grow if space
  • Shrink if needed 

align-self

๐Ÿ‘‰ What it does

Overrides align-items for one specific item

๐Ÿง  Key Idea

  • Works on cross-axis
  • Individual control

๐Ÿงช Example

.item:nth-child(2) {
  align-self: center;
}

๐Ÿ‘‰ Only second item is centered

๐Ÿ”ฅ Values

  • flex-start
  • flex-end
  • center
  • stretch

order

๐Ÿ‘‰ Controls visual order of items (without changing HTML)

.item:nth-child(1) {
  order: 2;
}

.item:nth-child(2) {
  order: 1;
}

๐Ÿ‘‰ Output:

  • Item 2 appears first
  • Item 1 appears later

๐Ÿ”ฅ Use case:

  • Reordering layout in responsive design


Default Values

Most people forget this ๐Ÿ‘‡

.container {
  display: flex;
}

๐Ÿ‘‰ Default behavior:

flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
align-content: stretch;

๐Ÿ‘‰ Important for debugging layouts