Skip to main content
Albero Academy

Flexbox & Grid

The two modern layout engines — Flexbox for one dimension, Grid for two — and when to reach for each.

Chapter 3CSS16 min readBeginner friendly
  • #CSS
  • #Flexbox
  • #Grid

For years, CSS layout meant hacks — floats, clearfixes, tables. Modern CSS has two purpose-built engines: Flexbox for laying out items in a single row or column, and Grid for two-dimensional layouts. Between them they handle almost every layout you'll ever build.

Why flex & grid

Both work by turning a container into a layout context, which then positions its direct children. You style the parent to describe the layout; the children fall into place. No floats, no positioning hacks.

Flexbox — one dimension

Add display: flex to a container and its children become a row. A handful of properties do 90% of the work:

a flex row
.nav {
display: flex;
justify-content: space-between; /* main axis: spread items apart */
align-items: center; /* cross axis: vertically centre */
gap: 16px; /* space between items */
}
/* Make one child expand to fill leftover space */
.nav .spacer { flex: 1; }
PropertyControls
flex-directionrow (default) or column
justify-contentAlignment along the main axis (start, center, space-between…)
align-itemsAlignment along the cross axis
gapSpacing between items (replaces margin hacks)
flex-wrapLet items wrap onto new lines

Grid — two dimensions

Grid lays out rows and columns at once. Define the columns on the container and drop children in:

a responsive card grid
.cards {
display: grid;
/* Three equal columns */
grid-template-columns: repeat(3, 1fr);
gap: 24px;
}
/* Auto-fit: as many ≥240px columns as fit, then wrap.
This one line gives you a responsive grid with no media queries. */
.cards {
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

The fr unit means "a fraction of the free space", so 1fr 2fr makes the second column twice as wide. You can also place an item across several tracks with grid-column: span 2.

Which one when?

  • Flexbox — content in a line: a navbar, a row of buttons, a card's inner layout, centring one thing.
  • Grid — a true 2-D structure: a page layout, a photo gallery, a dashboard of cards.
  • They combine happily — a Grid of cards where each card is a Flex column.

Practice

Build a navbar with Flexbox (logo left, links right, vertically centred) and a responsive gallery of programme cards with grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)). Resize the window and watch the grid reflow with zero media queries.

Key takeaways

  1. 01Flexbox lays out one dimension (a row or a column); Grid lays out two (rows and columns).
  2. 02You style the container; its direct children fall into place. Use gap for spacing.
  3. 03Flex: justify-content (main axis), align-items (cross axis). Centre anything in three lines.
  4. 04Grid: grid-template-columns with the fr unit; repeat(auto-fit, minmax(...)) gives responsive columns free.
  5. 05Reach for Flex for lines of content, Grid for 2-D structure — and combine them.