Understanding flex-basis vs width/height
You'll learn when flex-basis overrides explicit width or height properties in flex layouts.
When you're working with flex items, you might set both a width (or height) and a flex-basis on the same element. So which one wins?
The Precedence Rule
In a flex container, flex-basis takes precedence over width and height when determining an item's initial main-axis size. If you set both properties, the browser essentially ignores the explicit sizing property in favor of flex-basis.
Think of It This Way
Imagine you're packing a suitcase (the flex container). You have a travel bag (flex item) that normally holds 10 liters (width: 100px). But you specifically tell the packer, "For this trip, treat this bag as if it holds 15 liters" (flex-basis: 150px). The packer follows your specific flex instruction and ignores the bag's standard size.
How It Works
flex-basis: auto(default): The browser does use your explicitwidthorheightflex-basis: <length>: This value overrideswidth/heightfor the main axisflex-direction: row:flex-basiscontrols width;heightstill works normallyflex-direction: column:flex-basiscontrols height;widthstill works normally
The Cross Axis Exception
Remember: flex-basis only affects the main axis. For the cross axis, your width or height property still applies normally.
Key Takeaway: When both are set, flex-basis wins over width/height for main-axis sizing, but you can use flex-basis: auto to defer back to explicit sizing properties when needed.