CSS Tutorial

Flexbox

Mastering the Flexible Box layout for 1D designs

What is Flexbox?

Flexbox (Flexible Box Layout) is a 1-dimensional layout method for arranging items in rows or columns. It is the go-to tool for building navigation bars, centering items, and creating flexible grids.
css
.container {
  display: flex;
}

The Main Axis vs. Cross Axis

  • Main Axis: The primary direction items are laid out (defined by flex-direction).
  • Cross Axis: The perpendicular direction.

Alignment Verbs

  1. justify-content: Aligns items along the Main Axis (start, end, center, space-between).
  2. align-items: Aligns items along the Cross Axis (start, end, center, stretch).
Wait! Want to perfectly center something both horizontally and vertically? It used to be impossible. With Flexbox, it's just 3 lines:
css
.center-me {
  display: flex;
  justify-content: center;
  align-items: center;
}

Flex Growth and Shrinking

  • flex-grow: How much an item should grow relative to others if there is extra space.
  • flex-shrink: How much it should shrink if space is tight.
  • flex-basis: The starting size before growing or shrinking.

Summary

PropertyDescription
display: flexEnable flexbox on a parent
flex-directionRow or Column
justify-cont.Horizonal alignment (usually)
align-itemsVertical alignment (usually)
gapThe space between flex items
flex-wrapAllow items to wrap to a new line
Best ForComponents, Navbars, and 1D layouts
Logic"Fluid and flexible distribution of space"
SafetyExtremely well supported in all browsers
ModernThe industry standard for UI components
StandardUse gap instead of margin for spacing flex items
IdentityThe most popular layout tool in modern web development