Understanding the flex Shorthand Property in Flexbox
The flex shorthand property in CSS defines how a flex item grows, shrinks, and sets its base size within a flex container. It combines three individual properties — flex-grow, flex-shrink, and flex-basis — into a single, concise declaration.
flex: <flex-grow> <flex-shrink> <flex-basis>;
Each value is optional, and omitted values fall back to defaults.
flex-grow: Defines how much a flex item will grow relative to the rest when space is available (default: 0).
flex-shrink: Defines how much a flex item will shrink relative to others when space is limited (default: 1).
flex-basis: Defines the initial size of the item before space distribution (default: auto).
In this example, Item 2 grows twice as fast as Items 1 and 3 because its flex-grow value is 2. All items start with a base width of 150px and shrink equally if necessary.
flex: 1; → Equivalent to flex: 1 1 0; (items grow and shrink equally).
flex: auto; → Equivalent to flex: 1 1 auto;.
flex: none; → Equivalent to flex: 0 0 auto; (fixed size, no growth or shrink).
flex: initial; → Equivalent to flex: 0 1 auto; (default behavior).
Use flex: 1 to evenly distribute available space among items.
Use flex: none for fixed-size elements like buttons or logos.
Combine flex-grow and flex-basis strategically for responsive layouts.
Avoid mixing width and flex properties unless necessary for layout control.
The flex shorthand lets you control item growth, shrinkage, and base size in one line. Understanding flex-grow, flex-shrink, and flex-basis helps create flexible, responsive layouts efficiently.
You're building a two-column layout where the left column should take up 70% of the width and the right column 30%. You set flex: 7 3 on the left and flex: 3 7 on the right, but both columns are equal width. What’s wrong?
A button inside a flex container is overflowing its parent. You set flex: 1 on the button. Why isn’t it shrinking to fit, and how would you fix it?
A responsive card grid breaks on mobile: items wrap but stretch too wide. You used flex: 1 1 100% on each card. What’s the likely issue, and how would you adjust the flex values to fix it without changing the HTML?
Your team’s header component uses flex: 1 on the title and flex: 0 0 auto on the buttons. On tablets, the title gets cut off. How would you diagnose and fix this without hardcoding widths?
You’re designing a dynamic dashboard with collapsible panels. Each panel uses flex: 1 1 auto. When users collapse panels, the remaining ones don’t redistribute space smoothly. How would you redesign the flex configuration to handle dynamic sizing without layout thrashing?
A legacy component uses flex: 1 on all children in a row. When one child has a very long text node, it breaks the layout on small screens. How would you refactor this to be resilient without changing the component’s structure?
Your company’s design system uses flex: 1 across hundreds of components. As the UI scales, performance degrades on low-end devices due to constant flex recalculations. How would you architect a migration strategy to reduce layout thrash while maintaining backward compatibility?
You’re leading a cross-team effort to unify layout patterns. One team uses flex: 1 1 0%, another uses flex: 1 1 auto. How do you evaluate which approach is more sustainable long-term for a global component library, and how would you enforce consistency without breaking existing features?