What is the difference between justify-content and align-items?▾
justify-content controls spacing along the main axis (horizontal in row direction, vertical in column). align-items controls alignment on the cross axis (vertical in row, horizontal in column). The axis each controls flips when you change flex-direction.
When should I use flexbox vs grid?▾
Use flexbox for one-dimensional layouts — a row of navigation items, a toolbar, a centered card. Use CSS grid for two-dimensional layouts where you need explicit rows and columns to align content across both axes simultaneously, like a page scaffold or a card grid with aligned cell heights.
How do main axis and cross axis change with flex-direction?▾
In flex-direction: row (default), the main axis runs horizontally left-to-right and the cross axis runs vertically. In flex-direction: column, the main axis runs vertically top-to-bottom and the cross axis runs horizontally. justify-content always applies to the main axis regardless of direction.
What does flex: 1 actually mean?▾
flex: 1 is shorthand for flex-grow: 1, flex-shrink: 1, flex-basis: 0. It means the item grows to fill available space, shrinks when space is tight, and starts from zero basis. Multiple items with flex: 1 share the available space equally.
How do I center a single item with flexbox?▾
On the flex container, set justify-content: center to center on the main axis and align-items: center to center on the cross axis. If you also set the container to height: 100% or a specific height, the single child will be centered both horizontally and vertically.
Why doesn't flex-grow work without flex-basis?▾
flex-grow distributes the remaining space after all items have taken their base size. If items have content-driven sizes without an explicit flex-basis, the base sizes consume most space and flex-grow distributes the leftover. flex: 1 sets flex-basis: 0 to give flex-grow maximum control over size distribution.
How is gap different from margin between flex items?▾
gap applies spacing between all adjacent flex items consistently without adding margin at the container edges. Margin requires edge-trimming workarounds (negative margins or :first-child/:last-child rules) to avoid double spacing at edges. gap is cleaner and handles all cases including wrapped rows.
Can I use flex inside flex (nested layouts)?▾
Yes. Any flex item can itself be a flex container by setting display: flex on it. Nested flex is common for complex UIs — a row container with an item that uses column flex internally. Each flex context is independent; properties on an outer container do not affect inner container behavior.