Battle for Size
This is one of the most common questions. Why sometimes width works and sometimes you need flex-basis?
Main Differences:
- Direction:
widthalways handles horizontal.flex-basishandles the main axis. If you haveflex-direction: column, thenflex-basisbecomes height! - Priority: If both properties are set,
flex-basishas priority overwidth(orheight). - Minimum threshold:
flex-basis: 0is not the same aswidth: 0. With flexes, this means " collapse to minimum possible content size " .
Important
If you want to strictly set flex element size, use flex: 0 0 200px. This shorthand says: " don't grow, don't shrink, always be 200px " .
Tip
Use flex-basis: auto. In this case, browser will first look at element's width/height, and if none — at content size.