Course contentsShow
← Web Development
Lesson 287 of 2,9825. FlexboxFree lesson

Understanding flex-basis vs width/height

Learn when flex-basis takes precedence over explicit sizing properties.

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 explicit width or height
  • flex-basis: <length>: This value overrides width/height for the main axis
  • flex-direction: row: flex-basis controls width; height still works normally
  • flex-direction: column: flex-basis controls height; width still 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.