flex

Term side of the Card.
Topic: Flexboxes
Unit title: flex
Categories: Property
Explanation side of the Card

Explanation: The shorthand property that sets the `flex-grow`, `flex-shrink` and `flex-basis` properties for a flex item. It defines how a flex item should grow, shrink and be sized within a flex container, making it key to creating responsive layouts.

Browser compatibility:
  • The chrome browser supports this feature starting from version 29.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 20.
  • The safari browser supports this feature starting from version 9.
Availability: 

Example

Wrongcss
.item {
  flex-grow: 1;
}
Rightcss
.item {
  flex: 1;
  min-width: 0;
}

flex: 1 also sets flex-basis: 0, so items share space equally, and min-width: 0 lets them shrink below their content.

Common mistakes

  • Setting flex: 1 on the container instead of its children.
  • Items with flex: 1 still overflow because of long words and no min-width: 0.

Key points

  • Equal-width items use flex: 1 together with min-width: 0 when they hold long text or code.
  • The shorthand flex is used instead of setting flex-grow, flex-shrink and flex-basis one by one.
  • flex is set on the flex items, not on the container with display: flex.

Interview questions

flex: 1 vs flex: auto: what is the difference?

flex: 1 means 1 1 0, so items get equal widths. flex: auto means 1 1 auto, so items start from their content size and share the remaining space.

What is the default value of flex?

The initial value is 0 1 auto: items do not grow, can shrink, and start from their own size.

What does flex: none do?

It sets 0 0 auto, so the item keeps its size and neither grows nor shrinks.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy