Skip to content
IT glossary
FrontendUpdated December 3, 2026

Flexbox

This CSS layout model arranges items in a row or a column. It lets you align items, set gaps, and control how they share available space.

Think in one direction at a time

Flexbox is useful when a group of elements needs to line up: links in a navigation bar, buttons in a dialog, or a label next to an input. Set display: flex on their parent to make it a flex container. Its direct children in normal flow become flex items. If one of those children contains several buttons, the parent lays out that child as a single item; it doesn't lay out the buttons inside it.

Start with the axes

flex-direction sets the main axis. In a typical horizontal writing mode, row makes the main axis horizontal, while column makes it vertical. The cross axis runs perpendicular to it. justify-content distributes leftover space along the main axis; align-items aligns items on the cross axis. So if you switch from a row to a column, align-items: center switches from centering items vertically to centering them horizontally.

Sizing happens before leftover space is distributed for alignment. The flex shorthand sets three properties: flex-basis is the starting size along the main axis, flex-grow controls how an item shares extra space, and flex-shrink helps determine how much it gives up when space runs short. Two items with the same shrink factor don't necessarily lose the same number of pixels: their base sizes matter too.

Example: a title and an action button

Suppose .toolbar has two direct children: a title with the class .title and a button with the class .action.

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.action {
  flex: none;
}

The title takes up extra room when the toolbar is wide. As the toolbar narrows, the title can shrink and show an ellipsis, while the button keeps its size. min-width: 0 explicitly permits the title to become narrower than its content-based minimum; the overflow and text properties determine what happens to the text itself.

Wrapping is not a grid

Flex items stay on one line by default, even if they overflow. flex-wrap: wrap lets them move onto additional lines, but each line distributes space independently. That's handy for a group of tags with different widths. If you're laying out cards and need their edges to line up in shared columns across rows, CSS Grid is usually a better fit. Also avoid using order to correct a confusing HTML sequence: it changes the visual order without changing the default keyboard navigation order.

What interviewers look for

A common exercise is to center one item in both directions or build a toolbar that behaves well at narrow widths. Name the axes before picking alignment properties, then explain which item can grow or shrink. If centering appears to do nothing, check whether the container has any extra room along the relevant axis.

“Flexbox” questions from real interviews

Asked at 16 companies

All Frontend questions