CSS: layout with flexbox and grid

Lesson 7 of 17 · HTML, CSS and JavaScript

A menu in one row, cards side by side, header–content–footer: today two tools are used to lay out elements. Flexbox arranges elements in one direction (a row or a column), and grid in a mesh of rows and columns.

Before you start: required prior knowledge – the box model.
How to learn: do the tasks at the end on your own in your editor first; open the hint and the solution only after that. Every example has the code and a live result below it; type the code into your own file and change it.

Flexbox: elements in a row

The parent gets display: flex, and its children become "flex items" arranged in a row:

<style>
  .menu {
    display: flex;
    gap: 10px;                       /* the space between the items */
    justify-content: space-between;  /* placement along the main axis (horizontal) */
    align-items: center;             /* alignment along the cross axis (vertical) */
    background: #1f5f91;
    padding: 10px;
  }
  .menu a { color: white; text-decoration: none; padding: 6px 10px; }
  .logo { font-weight: bold; font-size: 1.4rem; color: #ffd166 !important; }
</style>
<nav class="menu">
  <a class="logo" href="#">LOGO</a>
  <div>
    <a href="#">Home</a>
    <a href="#">Projects</a>
    <a href="#">Contact</a>
  </div>
</nav>

Result:

Property (parent)Values
flex-directionrow (a row, the default), column
justify-contentflex-start, center, flex-end, space-between, space-around – along the main axis
align-itemsstretch, center, flex-start, flex-end – along the cross axis
flex-wrap: wrapitems wrap onto a new row when there is no room
gapthe space between the items
Property (child)Meaning
flex: 1the item takes the remaining space; two items with flex: 1 and flex: 2 share the space 1 : 2
flex: 0 0 200pxa fixed width of 200px – it neither grows nor shrinks
A classic problem solved in 3 lines – centring in the middle: display: flex; justify-content: center; align-items: center; on a parent that has a height.

Flexbox: cards that wrap onto a new row

<style>
  .cards { display: flex; flex-wrap: wrap; gap: 12px; }
  .card {
    flex: 1 1 140px;          /* grows, shrinks, starting width 140px */
    background: #eef6fc;
    border-radius: 10px;
    padding: 12px;
  }
</style>
<div class="cards">
  <div class="card">HTML</div>
  <div class="card">CSS</div>
  <div class="card">JavaScript</div>
  <div class="card">Python</div>
  <div class="card">C++</div>
</div>

Result:

Make the browser window narrower: the cards move onto a new row by themselves when they do not fit. This way, without a single extra rule, you get a layout that also works on a phone.

Grid: a mesh of rows and columns

<style>
  .gallery {
    display: grid;
    grid-template-columns: repeat(3, 1fr);   /* 3 equal columns */
    gap: 8px;
  }
  .gallery div { background: #2b7bb9; color: white; padding: 20px; text-align: center; border-radius: 6px; }
  .wide { grid-column: span 2; background: #1f5f91 !important; }   /* takes 2 columns */
</style>
<div class="gallery">
  <div>1</div><div>2</div><div>3</div>
  <div class="wide">4 (two columns)</div><div>5</div>
  <div>6</div><div>7</div><div>8</div>
</div>

Result:

PropertyExample
grid-template-columns200px 1fr – the first column 200px, the second the rest; repeat(4, 1fr) – 4 equal ones
fra share of the free space (fraction)
repeat(auto-fill, minmax(150px, 1fr))as many columns of at least 150px as fit – an automatically adapting gallery
grid-column: span 2the item takes two columns
grid-template-areasa layout given by area names (example below)

Grid: the layout of a whole page

<style>
  .page {
    display: grid;
    grid-template-columns: 1fr 3fr;
    grid-template-areas:
      "header header"
      "menu   content"
      "footer footer";
    gap: 6px;
    min-height: 220px;
  }
  header { grid-area: header;  background: #1f5f91; color: white; }
  nav    { grid-area: menu;    background: #cfe3f3; }
  main   { grid-area: content; background: #eef6fc; }
  footer { grid-area: footer;  background: #ddd; }
  header, nav, main, footer { padding: 10px; }
</style>
<div class="page">
  <header>Header</header>
  <nav>Menu</nav>
  <main>Main content</main>
  <footer>Footer</footer>
</div>

Result:

The areas are "drawn" as text in grid-template-areas, and each element gets its place through grid-area. Changing the layout (e.g. for a phone) comes down to a different drawing.

Flexbox or grid?

SituationTool
a menu, a row of buttons, centring one elementflexbox
an unknown number of cards that wrap onto a new rowflexbox (wrap) or grid (auto-fill)
a gallery with equal columnsgrid
the layout of a whole page (header, menu, content, footer)grid

They are often combined: grid for the whole page, flexbox inside the header. For practice there are the games Flexbox Froggy and Grid Garden.

Practice tasks

Each task has an example, a hint and a solution. The solutions have been checked in the browser.

TASK 01Difficulty: medium

An adaptive gallery

Make a gallery of 8 images that has 4 columns on a wide screen and reduces the number of columns by itself on narrower ones (the smallest image width is 160px). The images have the same height (150px) and are not stretched.

Common mistakes and tips
  • display: flex or grid is written on the parent, not on the elements you are arranging.
  • In flexbox justify-content works along the main axis – with flex-direction: column that is the vertical.
  • For the space between elements use gap instead of a margin on each element.
  • Firefox and Chrome have a tool in F12 that draws the grid lines and the flexbox axes – click the "grid"/"flex" label in Elements.

Check your knowledge

Open the quiz

Flexbox and grid

1. Which property makes flexbox items wrap onto a new row?




2. How do you centre an element horizontally and vertically in a flex parent?




3. What does grid-template-columns: 1fr 2fr mean?




4. What is better for the layout of a whole page with a header, a menu and content?